MAUI: Update own application (Android) - android

How can I check my own app if there is a newer version available and then update itselfs automatically? The idea is to request an API for a new version and receives the apk file. Then the APK should be stored and the installation/update should start. I don't know how to create this. The request to the API is no big deal, but how can I store and execute the APK file on an Android device?
I only found this but there were too many compiling errors (Context, Java, Buld, Intent, FileProvider could not be found).
Can you gave me some hints how to solve this "problem"?
THANK YOU!

You can uninstall you app by the following code:
var name = this.ApplicationContext.PackageName;
Intent intent = new Intent(Intent.ActionDelete);
intent.SetData(Android.Net.Uri.Parse("package:" + name));
StartActivity(intent);
In addition, don't forget to add the permissions in the AndroidManifest.xml:
<uses-permission android:name="android.permission.DELETE_PACKAGES" />
<uses-permission android:name="android.permission.REQUEST_DELETE_PACKAGES" />

It is not necessary to uninstall the app. If it is build with the same keystore and signature, it is possible to install one version directly to overwrite the existing.
var context = Android.App.Application.Context;
var path = Path.Combine(Android.App.Application.Context.GetExternalFilesDir("").AbsolutePath, "com.companyname.Testproject.apk");
Java.IO.File file = new Java.IO.File(path);
using (Android.Content.Intent install = new Android.Content.Intent(Android.Content.Intent.ActionView))
{
Android.Net.Uri apkURI = AndroidX.Core.Content.FileProvider.GetUriForFile(context, context.ApplicationContext.PackageName + ".provider", file);
install.SetDataAndType(apkURI, "application/vnd.android.package-archive");
install.AddFlags(Android.Content.ActivityFlags.NewTask);
install.AddFlags(Android.Content.ActivityFlags.GrantReadUriPermission);
install.AddFlags(Android.Content.ActivityFlags.ClearTop);
install.PutExtra(Android.Content.Intent.ExtraNotUnknownSource, true);
Platform.CurrentActivity.StartActivity(install);
}
In the AndroidManifext.xml i added:
<uses-permission android:name="android.permission.INSTALL_PACKAGES" />
<uses-permission android:name="android.permission.REQUEST_INSTALL_PACKAGES" />
<uses-permission android:name="android.permission.REQUEST_DELETE_PACKAGES" />
<uses-permission android:name="android.permission.DELETE_PACKAGES" />

Related

My android application rejected for using MANAGE_EXTERNAL_STORAGE permission. How to fix it?

My app targetting API 30. My app is integrated with com.github.barteksc:android-pdf-viewer:2.8.2 which uses InputStream & OutputStream to download & display file in pdfview.
Using Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOCUMENTS) to store downloaded files.
I have included these permission in my AndroidManifest.xml
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.MANAGE_EXTERNAL_STORAGE" />
if I remove MANAGE_EXTERNAL_STORAGE, my pdf doesn't gets loaded in android 10 & above.
If I include MANAGE_EXTERNAL_STORAGE permission google play rejecting my app.
How to fix this issue. I'm stuck here no where I cannot find proper troubleshooting for this issue. Please help me.
I tried not to use DIRECTORY_DOCUMENTS but instead used scoped storage which requires only
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
Replaced this line
Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOCUMENTS)
with this
File file = new File(getExternalFilesDir(filepath), filename);
App approved by Google Play also working fine.

Prompting user to install APK

I'm building my own update mechanism for my Android app (I'm not using Play Store). I can successfully download a new apk using DownloadManager. Once the download is finished, I would like to prompt the user to install the apk. I have tried the following approach:
File file = context.getApplicationContext().getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS + "/app-debug.apk");
String test = file.getAbsolutePath();
Intent promptInstall = new Intent(Intent.ACTION_VIEW)
.setDataAndType(Uri.parse("content://" + file.getAbsolutePath()),
"application/vnd.android.package-archive");
context.startActivity(promptInstall);
Unfortunately, this does not work at all. The file is there but there is no installation prompt. What is going wrong? Do I also have to adapt the second argument to setDataAndType? It should work on SDK 23 to 29.
Edit:
I have now tried the following approach:
File file = context.getApplicationContext().getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS + "/app-debug.apk");
Uri fileUri = Uri.fromFile(file); //for Build.VERSION.SDK_INT <= 24
if (Build.VERSION.SDK_INT >= 24) {
fileUri = FileProvider.getUriForFile(context, BuildConfig.APPLICATION_ID + ".provider", file);
}
Intent promptInstall = new Intent(Intent.ACTION_VIEW, fileUri);
promptInstall.putExtra(Intent.EXTRA_NOT_UNKNOWN_SOURCE, true);
promptInstall.setDataAndType(fileUri, "application/vnd.android.package-archive");
promptInstall.setFlags( Intent.FLAG_ACTIVITY_NEW_TASK);
promptInstall.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
context.startActivity(promptInstall);
In manifest I have now the following:
<uses-permission android:name="android.permission.REQUEST_INSTALL_PACKAGES"/>
<provider
android:name="androidx.core.content.FileProvider"
android:authorities="ir.greencode"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="#xml/path" />
</provider>
And I have created the path.xml file:
<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-path name="external_files" path="." /></paths>
But now I'm getting the error message:
java.lang.IllegalArgumentException: Couldn't find meta-data for
provider with authority com.testapp.provider
My downloaded APK is located in /storage/emulated/0/Android/data/com.testapp/files/Download/app-debug.apk. That means file is pointing to that directory. In the end, I would like to store the apk either on internal storage or SD storage (depending on where more space is available).
How can I resolve the problem?
Possible duplicate of Install Application programmatically on Android
I think this answer from that thread should work:
https://stackoverflow.com/a/54424223/9490453
<uses-permission android:name="android.permission.REQUEST_INSTALL_PACKAGES"/>

Can't create folder and file

I'm trying to create folder in internal storage of the device, and have some problems with that. I can't create directory using :
String rootPath=Environment.getExternalStorageDirectory().getPath()+"/test";
File file=new File(rootPath);
if(!file.exists()){
file.mkdir();
}
file.mkdir return false
I have such permissions:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_INTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_INTERNAL_STORAGE" />
Thank you very much for answers !
So really, as you all guys said in Android Marshmallow (API 23) or higher we need to ask runtime permissions.
To solve this issue use call :
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1);
in your onCreate() method
Thanks all for your help !
You can use
File sampleDir = new File(Environment.getExternalStorageDirectory(), "/test");
if (!sampleDir.exists()) {
sampleDir.mkdirs();
}

Install apk from java code without user confirmation

I want to install an apk file from java code. I want to do this silently without any action from user. I try this code ..
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(new File(files[i].toString())), "application/vnd.android.package-archive");
startActivity(intent);
It works but wants the user to confirm the installation.
I try
Process p;
p = Runtime.getRuntime().exec("pm -s install "+files[i].toString()+"\"");
but doesn't work
i have given these permissions
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.INSTALL_PACKAGES" />
<uses-permission android:name="android.permission.DELETE_PACKAGES" />

I want update my android app when new version find in my web server from my web server but its not installing

Inside my android application I have created a service that will check on my web server, if new version application will found it will update my application.
but I cannot install or my installation is unsuccessfull untill i uninstall the privious version.
my new version installation code is:
Intent promptInstall = new Intent(Intent.ACTION_VIEW);
promptInstall.setDataAndType(Uri.fromFile(new File(Environment.getExternalStorageDirectory() +"/apks/PHS_TKS_v3.apk")), "application/vnd.android.package-archive");
promptInstall.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(promptInstall);
and user permission in manifest is:
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_OWNER_DATA" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.INSTALL_PACKAGES"/>
<uses-permission android:name="android.permission.RESTART_PACKAGES"/>
I am trying to install new version apk not same version
If i install manually its not installing if older version is installed.
The error msg "an existing package by the same name with a conflicting signature is already installed" means, that you have the application installed already, but it was built with a different signature.
If you were building and debugging in eclipse, then it pushed the apk to your phone with the debug or test keys.
You need to uninstall the app, then installed a version that was built using your custom keystore

Categories

Resources