install and update apk - targetSdkVersion 29 (Android 10)

495 views Asked by At

since I updated targetSdkVersion to 29 I am facing many troubles, this one realted to downloading - installing - updating my app. The apk with the new version is downloaded successfully (because I find it through the file explorer in my phone) but I do not think is installed properly. Once the process finishes it looks like the screen is restarted because it blinks, but the app is not updated (and I know it because in the main screen it shows the version). My code is:

    public static boolean openFile(String intentAction) { // in this case it is Intent.ACTION_INSTALL_PACKAGE
    try {

        File file = new File(Environment.getExternalStorageDirectory().toString() + "/" + "myApp.apk");
        if (file.exists()) {

            Uri uri;

            if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.M) {
                uri = Uri.fromFile(file);
            } else {
                uri = FileProvider.getUriForFile(appContext, appContext.getPackageName() + ".provider", file);
            }

                return installPackage(file, uri);

        }

    } catch (Exception e) {
        generateExceptionLog(e);
    }
    return false;
}


private static boolean installPackage(File file, Uri uri) {

    PackageInstaller packageInstaller = appContext.getPackageManager().getPackageInstaller();
    PackageInstaller.SessionParams params = new PackageInstaller.SessionParams(PackageInstaller.SessionParams.MODE_FULL_INSTALL);
    params.setAppPackageName(file.getAbsolutePath());

    try {
        int sessionId = packageInstaller.createSession(params);
        PackageInstaller.Session session = packageInstaller.openSession(sessionId);

        OutputStream packageInSession = session.openWrite("package", 0, -1);
        InputStream input;
        input = appContext.getContentResolver().openInputStream(uri);

        if (input != null) {
            generateInfoLog("input.available: " + input.available());
            byte[] buffer = new byte[16384];
            int n;
            while ((n = input.read(buffer)) >= 0) {
                packageInSession.write(buffer, 0, n);
            }
        } else {
            generateExceptionLog("INSTALLATION FAILED");
        }
        packageInSession.close();
        input.close();


        //Intent intent2 = new Intent(appContext, ActMain.class);
        Intent intent2 = new Intent(appContext, LauncherReceiver.class);
        intent2.setAction(ACTION_PACKAGE_INSTALED);
        //intent.setAction(Intent.PACKAGE);
        PendingIntent pendingIntent = PendingIntent.getBroadcast(
                appContext,
                sessionId,
                intent2,
                PendingIntent.FLAG_UPDATE_CURRENT);
        IntentSender statusReceiver = pendingIntent.getIntentSender();
        session.commit(statusReceiver);

    } catch (Exception e) {
        generateExceptionLog(e);
        return false;
    }

    return true;
}

My Receiver:

public class LauncherReceiver extends BroadcastReceiver {
public static final String ACTION_PACKAGE_INSTALED = "com.gim.androidv2.action.PACKAGE_INSTALED";

@Override
public void onReceive(Context context, Intent intent) {
    Intent startIntent = new Intent(context, ActMain.class);
    intent.setAction(ACTION_PACKAGE_INSTALED);
    startIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_CLEAR_TOP);
    context.startActivity(startIntent);
}

}

And the manifest:

        <receiver android:name=".LauncherReceiver" android:enabled="true" android:exported="true">
        <intent-filter>
            <action android:name="com.aaa.aaa.action.START"></action>
            <action android:name="android.intent.action.MY_PACKAGE_REPLACED" />
            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </receiver>
0

There are 0 answers