Issue
I'm trying to update an android APK automatically by downloading it from a local web server and using Intent from a Receiver class to install it.
So the download which works fine:
string mime = "application/vnd.android.package-archive";
string file = "com.fips.sorter.apk";
string apkurl = "http://myserver/downloads/" + file;
string path = Path.Combine(ExternalStorageDirectory.Path, DirectoryDownloads, file);
long id = downloadAPK(file, apkurl, mime);
And then instantiate a concrete BroadcastReceiver to update the app.
class Receiver : BroadcastReceiver
{
private Activity activity;
private long downloadId;
public Receiver(Activity activity, long id)
{
this.activity = activity;
this.downloadId = id;
}
public override void OnReceive(Context context, Intent intent)
{
DownloadManager manager = activity.GetSystemService(Context.DownloadService);
Intent install = new Intent(Intent.ActionView);
install.SetFlags(ActivityFlags.ClearTop);
string PATH = $"{ExternalStorageDirectory.Path}/{DirectoryDownloads}/";
string file = "com.fips.sorter.apk";
intent.SetDataAndType(Android.Net.Uri.FromFile(new Java.IO.File(PATH + file)), manager.GetMimeTypeForDownloadedFile(downloadId));
activity.StartActivity(install);
activity.UnregisterReceiver(this);
activity.Finish();
}
}
.....
BroadcastReceiver onComplete = new Receiver(this, id);
RegisterReceiver(onComplete, new IntentFilter(DownloadManager.ActionDownloadComplete));
When activity.StartActivity is called the screen asks to select the app on which to operate on and it doesnt matter what is chosen, the app just stop and the next time the app is not updated.
Is it still possible to update apps programmatically like this? What to do to make this work?
Solution
You could programmatically install .apk
in your application by using the following code
For app in market
Intent goToMarket = new Intent(Intent.ActionView).SetData(Uri.Parse("xxx"));
StartActivity(goToMarket);
For app in custom web service
Intent promptInstall = new Intent(Intent.ActionView)
.SetDataAndType(Uri.Parse("file:///path/to/your.apk"),
"application/vnd.android.package-archive");
StartActivity(promptInstall);
But please be aware that we couldn't install applications without the user permissions. So we need to request the dynamic request using android.permission.REQUEST_INSTALL_PACKAGES:
For more details you could check this doc .
Answered By - Lucas Zhang - MSFT
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.