Hello,
Welcome to our Microsoft Q&A platform!
You could check the file path, If there is a apk in SDCard, you can run and install it. You could have a try with the folling steps:
1. Add FileProvider configuration in AndroidManifest
<application android:label="AndroidRunApkDemo.Android" android:theme="@style/MainTheme">
<provider android:name="androidx.core.content.FileProvider" android:authorities="com.companyname.androidrunapkdemo.fileprovider" android:exported="false" android:grantUriPermissions="true">
<meta-data android:name="android.support.FILE_PROVIDER_PATHS" android:resource="@xml/filepaths" />
</provider>
</application>
2. Create a new xml/filepaths.xml
in the Resources
directory
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-path name="external" path=""/>“
</paths>
3. Use FileProvider
to install apk compatible
public void ApkInstall()
{
Intent install = new Intent(Intent.ActionView);
String sdcardRoot = Android.OS.Environment.ExternalStorageDirectory.AbsolutePath;
String apkSavePath = sdcardRoot + "/com.companyname.app41.apk";
Java.IO.File apkFile = new Java.IO.File(apkSavePath);
install.SetFlags(ActivityFlags.NewTask);
if (Build.VERSION.SdkInt >= BuildVersionCodes.N)
{
var flags = ActivityFlags.ClearTop | ActivityFlags.NewTask;
install.SetFlags(flags);
Android.Net.Uri uri = FileProvider.GetUriForFile(Android.App.Application.Context, Android.App.Application.Context.PackageName + ".fileprovider", apkFile);
install.SetDataAndType(uri, "application/vnd.android.package-archive");
}
else
{
install.SetDataAndType(Android.Net.Uri.FromFile(apkFile), "application/vnd.android.package-archive");
}
Forms.Context.StartActivity(install);
}
I use DependencyService to test, you can refer to the follwing code
XAML
private void Button_Clicked(object sender, EventArgs e)
{
DependencyService.Get<ISDCardService>().ApkInstall();
}
interface
namespace AndroidRunApkDemo
{
public interface ISDCardService
{
void ApkInstall();
}
}
Android
[assembly: Dependency(typeof(SDCardService))]
namespace AndroidRunApkDemo.Droid
{
public class SDCardService : ISDCardService
{
public void ApkInstall()
{
.......
}
}
In addition, you may need this permission <uses-permission android:name="android.permission.REQUEST_INSTALL_PACKAGES" />
after Android 8.
Best Regards,
Wenyan Zhang
If the response is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.