I have the following code that is executed when the user accepts the update of the application within it.
I already provide code for compatibility with versions 6.0 + and versions 6.0 -, the problem arises that some devices accept the code execution that I show below, and some others do not., usually the devices with versions 6.0 - do not cause problems execute correctly, the problem is in the versions 6.0 + that some executions and in some others do not. -
File fileToUpdate = new File(getExternalFilesDir(null) + File.separator + "nameAppV" + (currentVersionCode + 1) + ".apk");
ComponentName comp;
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(fileToUpdate), "application/vnd.android.package-archive");
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
intent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
if (android.os.Build.VERSION.SDK_INT <= Build.VERSION_CODES.LOLLIPOP_MR1)
{
comp = new ComponentName("com.android.packageinstaller", "com.android.packageinstaller.PackageInstallerActivity");
}
else{
comp = new ComponentName("com.google.android.packageinstaller", "com.android.packageinstaller.PackageInstallerActivity");
}
intent.setComponent(comp);
startActivity(intent);
This is the error that throws me when trying the execution.
Summary:
1.- I have implemented the code to perform an installation of the application when there are new updates.
2.- Support for versions 6.0+ and earlier versions is provided.
3.- Previous versions (OS) runs smoothly.
4.- Versions 6.0 + runs in some correctly.
5.- In some versions of 6.0 + the code is not executed and it throws me the error.
SOLVED !!!
Apparently the some versions of android 6.0 still bring the package ( com.android.packageinstaller ) installed, that later send an update to solve this problem because others with the same phone, system, applications already have the updated package ( com.google.android.packageinstaller ) installed.
To check the package they have installed I had to buy a phone from those who were causing problems with code execution, run the following command in the console to verify the package.
Phone, system, applications, etc. Exactly the same, but different package.
INPUT -> adb -s ZY22342FG3 exec-out pm list packages -f install
package:/system/app/CertInstaller/CertInstaller.apk=com.android.certinstaller
package:/system/priv-app/GooglePackageInstaller/GooglePackageInstaller.apk=com.android.packageinstaller
INPUT -> adb -s ZY2233Z2S2 exec-out pm list packages -f install
package:/system/app/CertInstaller/CertInstaller.apk=com.android.certinstaller
package:/system/priv-app/GooglePackageInstaller/GooglePackageInstaller.apk=com.google.android.packageinstaller
Method to verify if the package exists on the mobile device
public boolean isPackageExisted(String targetPackage){
List<ApplicationInfo> packages;
PackageManager pm;
pm = getPackageManager();
packages = pm.getInstalledApplications(0);
for (ApplicationInfo packageInfo : packages)
{
if(packageInfo.packageName.equals(targetPackage))
return true;
}
return false;
}
Final code:
Intent intent = new Intent(Intent.ACTION_VIEW);
File fileToUpdate = new File(getExternalFilesDir(null) + File.separator + "lecturasV" + (currentVersionCode + 1) + ".apk");
ComponentName comp;
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
intent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
//Soporte para 5.1 -
if(Build.VERSION.SDK_INT <= Build.VERSION_CODES.LOLLIPOP_MR1 || isPackageExisted("com.android.packageinstaller"))
{
Log.e("VERSION: ", "OPT 1 "+ Build.VERSION.SDK_INT);
intent.setDataAndType(Uri.fromFile(fileToUpdate), "application/vnd.android.package-archive");
comp = new ComponentName("com.android.packageinstaller", "com.android.packageinstaller.PackageInstallerActivity");
}
//Soporte para 6.0+
else
{
//Soporte para 7.0 +
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N)
{
Uri uriToUpdate = FileProvider.getUriForFile(context, context.getApplicationContext().getPackageName() + ".provider", fileToUpdate);
intent.setDataAndType(uriToUpdate, "application/vnd.android.package-archive");
}else{
//Soporte para 7.0 -
intent.setDataAndType(Uri.fromFile(fileToUpdate), "application/vnd.android.package-archive");
}
Log.e("VERSION: ", "OPT 2 "+ Build.VERSION.SDK_INT);
comp = new ComponentName("com.google.android.packageinstaller", "com.android.packageinstaller.PackageInstallerActivity");
}
intent.setComponent(comp);
startActivity(intent);