Forum Xamarin.Android

Announcement:

The Xamarin Forums have officially moved to the new Microsoft Q&A experience. Microsoft Q&A is the home for technical questions and answers at across all products at Microsoft now including Xamarin!

To create new threads and ask questions head over to Microsoft Q&A for .NET and get involved today.

How to install and run an apk file?

Hello,

I am implementing auto update my application. 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

Regards
Leigh

Posts

  • LeighWansteadLeighWanstead AUMember

    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);
    
  • LeighWansteadLeighWanstead AUMember
    edited June 2013

    I found a method to install the shortcut on desktop.

    Add this line into AndroidManifest.xml file.

    remove double quote
    "<"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);
    
  • christophedmchristophedm BEMember ✭✭

    Hi @LeighWanstead

    Thanks so much for your examples i got it to work, only the shortcut part is not working.
    Where do u put the shortcut code ?

    Thanks.

  • PSchimmelPSchimmel DEMember ✭✭

    Hi!
    I was just trying to start a .apk file from my Xamarin.Forms app following the instructions above. However when I start the activity I get the error message "Package installer has stopped".
    Here's my code:

    Android.Net.Uri fileURI = FileProvider.GetUriForFile(Context, "com.gs_cms.cmsmobile", file);
    var intent = new Intent(Intent.ActionView);
    intent.SetDataAndType(fileURI, "application/vnd.android.package-archive");
    intent.SetFlags(ActivityFlags.GrantReadUriPermission);
    intent.SetFlags(ActivityFlags.GrantWriteUriPermission);
    intent.SetFlags(ActivityFlags.NewTask);
    intent.SetFlags(ActivityFlags.ClearWhenTaskReset);
    Context.StartActivity(intent);
    

    Here's what I addded to AndroidManifest.xml:

    <provider android:name="android.support.v4.content.FileProvider" android:authorities="com.gs_cms.cmsmobile" android:exported="false" android:grantUriPermissions="true">
      <meta-data android:name="android.support.FILE_PROVIDER_PATHS" android:resource="@xml/file_paths" />
    </provider>
    
Sign In or Register to comment.