FreeOZ论坛

标题: How to install and run an apk file in android? [打印本页]

作者: DDD888    时间: 28-6-2013 08:54
标题: How to install and run an apk file in android?
Good morning,

I am implementing auto update my application in Android using xamarin C#. I can install the application from sd card fine using this code.

        Intent intent = new Intent(Intent.ActionView);
        intent.SetDataAndType(Uri.FromFile(new Java.IO.File(localFileName)), "application/vnd.android.package-archive");
        HomeContext.StartActivity(intent);

But I can not do like google play store once install and allow the end user to run the installed application. How can I do it? Another question is how to add a shortcut to desktop by code?

TIA
作者: DDD888    时间: 28-6-2013 09:47
I managed to get open application to run button shown after install. Here is the code.

        Intent intent = new Intent(Intent.ActionView);
        intent.SetDataAndType(Uri.FromFile(new Java.IO.File(localFileName)), "application/vnd.android.package-archive");
        intent.SetFlags(ActivityFlags.NewTask);
        HomeContext.StartActivity(intent);
作者: DDD888    时间: 28-6-2013 11:58
本帖最后由 DDD888 于 28-6-2013 12:15 编辑

I found a method to install the shortcut on desktop.

Add this line into AndroidManifest.xml file.
<uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT"></uses-permission>

Here is the code.

        var shortcutIntent = new Intent();
        shortcutIntent.SetComponent(new ComponentName(this, Java.Lang.Class.FromType(typeof(ActivityHomeScreen))));

        shortcutIntent.AddFlags(ActivityFlags.NewTask);
        shortcutIntent.AddFlags(ActivityFlags.ClearTop);

        Intent putShortCutIntent = new Intent();
        putShortCutIntent.PutExtra(Intent.ExtraShortcutIntent, shortcutIntent);

        // Sets the custom shortcut's title
        putShortCutIntent.PutExtra(Intent.ExtraShortcutName,  "test");
        putShortCutIntent.PutExtra("duplicate", false);
        putShortCutIntent.PutExtra(Intent.ExtraShortcutIconResource, Intent.ShortcutIconResource.FromContext(this, Resource.Drawable.Icon));
        putShortCutIntent.SetAction("com.android.launcher.action.INSTALL_SHORTCUT");
        SendBroadcast(putShortCutIntent);




欢迎光临 FreeOZ论坛 (https://www.freeoz.org/bbs/) Powered by Discuz! X3.2