Android application update issue

2019-04-24 11:27发布

Recently I have been facing the problem of my android app update process.

In brief, app is able to check if update with higher version code was uploaded on server. If so, user decides whether to update. After that app is loaded and standard installation begins:

final Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(new File(PATH_TO_APK)), "application/vnd.android.package-archive");
startActivity(intent)

The problem is that when android Intent finishes installation, "theoretically" activity with information "Application was installed" and 2 buttons "Done", "Open". I wrote "theoretically" because so far I have come across scenarios below:

  1. App is installed, activity with message "Application was installed" is shown, user clicks "Open" but nothing happens (Android 2.3.*) or app opens itself correctly indeed - this behaviour is random.

  2. App is installed, activity with message "Application was installed" is shown but suddenly disappears.

Trying to circumvent this bug(?) I found http://developer.android.com/reference/android/content/Intent.html#ACTION_PACKAGE_REPLACED. BroadcastReceiver which I implemented, started Launch Activity and let's say it was a proper solution somehow.

         <receiver android:name=\".MyReceiver\" >
          <intent-filter>
              <action android:name="android.intent.action.ACTION_PACKAGE_REPLACED" />
              //Or from API 12 <action android:name="android.intent.action.ACTION_MY_PACKAGE_REPLACED" />
           </intent-filter>
         </receiver>

This solution had to be modified because applications with lower API (lower than 12) could not handle ACTION_MY_PACKAGE_REPLACED so I implemented API-dependent behaviour which:

  • allowed installing udpate app normally and launch app from Activity with "Done"/"Open" button ( API < 12)

  • launched update app via MyReceiver after ACTION_MY_PACKAGE_REPLACED noting.

This is my current solution.

My questions are:

  • why updated app opens randomly after clicking "Open" after installation in android with API lower than 12?

  • why activity with "Done"/"Open" buttons disappears on devices with higher API?

I tried to finish applicattion before installing but it did not help.

My explanation is that after installation process, a new package has to overwrite the old one so the old package has to be simply removed and this is the main cause of disappearing the launching activity.

As I wrote, this is my current solution and I am not satisfied. If anyone could clarify the matter I would be very grateful.

Thanks for reading.

Edit:

Ok, solution is very simple: to successful update you need to launch the Intent as new task (arrrgh...):

final Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(new File(PATH_TO_APK)), "application/vnd.android.package-archive");
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);

1条回答
放我归山
2楼-- · 2019-04-24 12:11

First, add 'intent-filter' at Manifest like as following:

`<activity android:name="com.package.MainActivity">
    <intent-filter>
        <action android:name="android.intent.action.VIEW" />
        <data android:scheme="file" />
        <data android:mimeType="application/vnd.android.package-archive" />
    </intent-filter>
</activity>`

Then, set your intent flags for new task:

intentAPK.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

查看更多
登录 后发表回答