How to install Apk from app using Xamarin android - android

I am trying to update my app programmatically.
I am able to download the new version of the app apk.
But when the installation code runs nothing happens except an activity that is opened and then closed returning to my app
The installation code is
Java.IO.File apkFile = new Java.IO.File(Android.OS.Environment.ExternalStorageDirectory.AbsolutePath + "/download/" + "com.tri.GOT.apk");
var context = Android.App.Application.Context;
var downloadUri = FileProvider.GetUriForFile(context, "com.tri.GOT.fileprovider", apkFile);
Intent install = new Intent(Intent.ActionView);
install.AddFlags(ActivityFlags.NewTask);
install.AddFlags(ActivityFlags.GrantReadUriPermission);
install.AddFlags(ActivityFlags.GrantWriteUriPermission);
install.AddFlags(ActivityFlags.GrantPersistableUriPermission);
install.SetDataAndType(downloadUri, "application/vnd.android.package-archive");
context.StartActivity(install);

Related

Problem apk Installation in android devices programmatically

private void installNewApk(String outputFile) {
Uri uri = FileProvider.getUriForFile(getApplicationContext(), BuildConfig.APPLICATION_ID + ".provider", new File(outputFile + "app-demo-debug.apk"));
Intent install = new Intent(Intent.ACTION_VIEW);
install.setDataAndType(uri, "application/vnd.android.package-archive");
install.putExtra(Intent.ACTION_INSTALL_PACKAGE, true);
install.putExtra(Intent.EXTRA_NOT_UNKNOWN_SOURCE, true);
install.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
install.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
install.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(install);
}
My apk is not corrupted file because manually my apk install but programmatically not install.
Unknown Source already allow
please provide the answer

Open external SD card path in filemanager android

In my app, I'm saving my application exported pdf file in SD-CARD/myapp directory. I want to open that directory from my app in any filemanager installed in android device. So that user can easily move that directory easily to get those files exported by my app.
I have tried this code from Android: How to open a specific folder via Intent and show its content in a file browser? (Accepted answer)
private void openDirectory() {
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
Uri uri = Uri.parse(Environment.getExternalStorageDirectory().getPath()
+ File.separator + "myFolder" + File.separator);
intent.setDataAndType(uri, "text/csv");
startActivity(Intent.createChooser(intent, "Open folder"));
}
but it's showing me error on startActivity Expected 3 arguments but found 1.
Tried also this code but everywhere it shows startActivity needs 3 arguments but found 1.
Uri selectedUri = Uri.parse(Environment.getExternalStorageDirectory() + "/myFolder/");
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(selectedUri, "resource/folder");
if (intent.resolveActivityInfo(getPackageManager(), 0) != null)
{
startActivity(intent);
}
else
{
// if you reach this place, it means there is no any file
// explorer app installed on your device
}

Installation dialog not shown in oreo when done programatically if device is not rooted

I am trying to update app after showing the alert dialog in my app. It is working fine below Android Oreo. Here is what I have tried till now
String ANDROID_PACKAGE = "application/vnd.android.package-archive";
File update_apk = new File(context.getFilesDir(), intent.getStringExtra("update_file"));
Intent notificationIntent;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
notificationIntent = new Intent(Intent.ACTION_INSTALL_PACKAGE);
notificationIntent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
notificationIntent.setDataAndType(
FileProvider.getUriForFile(context, BuildConfig.APPLICATION_ID + ".fileprovider", update_apk),
ANDROID_PACKAGE);
} else {
notificationIntent = new Intent(Intent.ACTION_VIEW);
notificationIntent.setDataAndType(
Uri.parse("file://" + update_apk.getAbsolutePath()),
ANDROID_PACKAGE);
}
startActivity(notificationIntent);
How to show this app update dialog in the devices which are not rooted. Any help would be appreciated. Thanks in aadvance.
Check below permission in your manifest.xml file.
<uses-permission android:name="android.permission.REQUEST_INSTALL_PACKAGES" />
Also check your file provider for Intent.VIEW_ACTION.
More reference check this page Android install apk with Intent.VIEW_ACTION not working with File provider
enjoy code.

How to correct update Android .apk?

Intent openIntent;
File file = new File(mApkPath);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
String auth = mContext.getPackageName() + ".file.provider";
Uri apkUri = FileProvider.getUriForFile(mContext, auth, file);
openIntent = new Intent(Intent.ACTION_INSTALL_PACKAGE);
openIntent.setData(apkUri);
openIntent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
} else {
openIntent = new Intent(Intent.ACTION_VIEW);
openIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
openIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
openIntent.setDataAndType(Uri.fromFile(file), "application/vnd.android.package-archive");
}
return openIntent;
I found this way can open an Activity which is mean "Install Success" in low SDK version Android device, but in hight SDK version, when install finished, the app will like crash, even if the new .apk was installed.
Have any way to open the activity which is mean "Install Success", so that let user can choose install 'Finished' or 'Open'?
By the way, I got resultCode == RESULT_CANCELED, actually, new .apk was installed. And I notice PackageInstaller can user update apk, but how to use it? I read ApiDemos but got result == PackageInstaller.STATUS_PENDING_USER_ACTION by int result = intent.getIntExtra(PackageInstaller.EXTRA_STATUS, PackageInstaller.STATUS_FAILURE), but .apk was Installed and no any 'Finished' or 'Open' button.
Thanks!

In App Upgrade for Android Oreo (API Version 26)

My App is distributed outside of Play Store and it is upgraded within the App when there is a new version available. I am using the below code to start the install (after the file was downloaded using DownloadManager)
String destination = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS) + "/app.apk";
For Android N (24):
Uri contentUri = FileProvider.getUriForFile(ctxt, BuildConfig.APPLICATION_ID + ".provider", new File(destination));
Intent openFileIntent = new Intent(Intent.ACTION_VIEW);
openFileIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
openFileIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
openFileIntent.setData(contentUri);
startActivity(openFileIntent);
For Android 23 and below:
final Uri uri = Uri.parse("file://" + destination);
Intent install = new Intent(Intent.ACTION_VIEW);
install.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
install.setDataAndType(uri,"application/vnd.android.package-archive");
startActivity(install);
When I use the same code for "Android N", it is not showing the system install activity in Android Oreo. Anyone who has solved this problem?

Categories

Resources