How to install and run an apk file in SD card in Xamarin forms?

judsonabrahamjudsonabraham USMember ✭✭✭

I want to install and run apk file in my SD card. I have code through which I'm trying to fetch the apk file but I have no clue how to install and open it. I know I can use Broadcast receiver but how? This is my code so far

MainPage.Xaml.cs

 private void Button_Clicked(object sender, EventArgs e)
        {
            SDCard device = DependencyService.Get<SDCard>();



        }

SDCardInstall.cs in native android

 public class SDCardInstall: SDCard
    {
        public void ApkInstall(string filename)
        {
  filename = "xyz.apk";
         var path = "SD card/Install apk/";
        var filePath = Path.Combine(path.ToString(), filename);
        Intent intent = new Intent(Intent.ActionView);
     }
    }

I have mentioned the following permission

    <uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT" />
        <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
        <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
        <uses-permission android:name="android.permission.INSTALL_PACKAGES" />
        <uses-permission android:name="android.permission.INSTANT_APP_FOREGROUND_SERVICE" />

Best Answer

Answers

  • AlessandroCaliaroAlessandroCaliaro ITMember ✭✭✭✭✭

    In Java it should be something like

            final Uri uri = Uri.parse("file://" + destination);
                    Intent install = new Intent(Intent.ACTION_VIEW);
                    install.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                    //install.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    install.setDataAndType(uri, "application/vnd.android.package-archive");
                    startActivity(install);
    
  • judsonabrahamjudsonabraham USMember ✭✭✭

    how will I write this in C# @AlessandroCaliaro

  • judsonabrahamjudsonabraham USMember ✭✭✭

    I'm getting lots of error when I'm trying to convert the code u have provided in Java to c# . It will be really useful @AlessandroCaliaro if u provide me the c# code.

  • judsonabrahamjudsonabraham USMember ✭✭✭

    @AlessandroCaliaro the StartActivity(install); gives me error what should I write instaed of that? Sorry I'm a newbie.

  • judsonabrahamjudsonabraham USMember ✭✭✭

    still the apk is not installing. And I'm not able to view that. I think I have to make changes in xaml.cs. I'm providing
    private void StartActivity(Intent install)
    {
    throw new NotImplementedException();
    }
    for StartActivity is that Okay?

  • judsonabrahamjudsonabraham USMember ✭✭✭
    edited May 2018

    what change should I do in mainpage.xaml.cs @AlessandroCaliaro

     private void Button_Clicked(object sender, EventArgs e)
            {
                SDCard device = DependencyService.Get<SDCard>();
    
    
    
            }
    

    to make it go inside the SDCardInstall.cs class. Right now my debugger is not going in SDCardInstall.cs.
    this is my interface code
    public interface SDCard
    {
    void ApkInstall();
    }

  • judsonabrahamjudsonabraham USMember ✭✭✭
    edited May 2018

    The code works but it doesn't install. It just shows the installation page. Actually my app should Install directly instead of showing the installation page.
    var filename = "Xyz.apk";
    System.Diagnostics.Debug.WriteLine("The file name is"+filename);
    var path = "Install apk/";
    var destination = Path.Combine(Android.OS.Environment.ExternalStorageDirectory.AbsolutePath.ToString(),path+filename);
    System.Diagnostics.Debug.WriteLine("The destination is"+destination);
    //Android.Net.Uri uri = Android.Net.Uri.("file://" + destination);
    Intent install = new Intent(Intent.ActionView);
    install.AddFlags(ActivityFlags.ClearTask);

                    install.SetFlags(ActivityFlags.NewTask);
                    install.SetDataAndType(Android.Net.Uri.FromFile(new Java.IO.File(Android.OS.Environment.ExternalStorageDirectory.AbsolutePath.ToString()+ "/Install apk/"+ filename)), "application/vnd.android.package-archive");
                     Forms.Context.StartActivity(install);
    
  • AlessandroCaliaroAlessandroCaliaro ITMember ✭✭✭✭✭

    What does it mean "show the installation page"?

  • judsonabrahamjudsonabraham USMember ✭✭✭

    This is the page I'm getting

  • judsonabrahamjudsonabraham USMember ✭✭✭
    edited May 2018

    Actually it should start installing the App on its own.But now I have to manually install the App.

Sign In or Register to comment.