6

我使用https://stackoverflow.com/a/14353076/1327384来更新我的 android 程序,但是在更新我的程序后它将被关闭,所以我想在完成更新过程后重新打开它,我该怎么做?

我用过这个类

package services;
    public class PackageChangeReceiver extends BroadcastReceiver {
         @Override
            public void onReceive(Context ctx, Intent intent) {
            Uri data = intent.getData();
            boolean replacing = intent.getBooleanExtra(Intent.EXTRA_REPLACING, false);

            Intent intent1 = new Intent(ctx, service.class);
            ctx.startService(intent1);
            Log.d("service", "Action: " + intent.getAction());
            Log.d("service", "The DATA: " + data);
            }

    }

和这个主要的

 <receiver android:name="services.PackageChangeReceiver" >
            <intent-filter>
                <action android:name="android.intent.action.PACKAGE_REMOVED" />
                <action android:name="android.intent.action.PACKAGE_REPLACED" />
                <action android:name="android.intent.action.PACKAGE_ADDED" />

                <data android:scheme="package" />
            </intent-filter>
        </receiver>

但我手动启动应用程序后仍然收到意图

4

3 回答 3

3

a) 看看android.intent.action.PACKAGE_REPLACED

b)我相信如果您的应用程序具有粘性服务,那么该服务会在包更新后重新启动。

于 2013-02-23T21:59:26.250 回答
1

可能AlarmManager可以提供帮助吗?您可以设置任务以启动应用程序的活动,例如在您的 apk 下载并用户单击安装后 40 秒内。

于 2013-02-23T21:52:06.930 回答
0

For API Level 12 & above

Simply register a broadcast receiver to receive a call only when your application package is updated.

public class PackageUpdateReceiver extends BroadcastReceiver {

   @Override
   public void onReceive(Context context, Intent intent) {
       //get the launch activity or any activity you want to open 
       Intent i = getBaseContext().getPackageManager().
               getLaunchIntentForPackage(getBaseContext().getPackageName());
       i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
       i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
       context.startActivity(i);
   }
}

Make sure to register the broadcast receiverin the manifest!

    <receiver android:name=".receivers.PackageUpdateReceiver">
        <intent-filter>
            <action android:name="android.intent.action.MY_PACKAGE_REPLACED" />
        </intent-filter>
    </receiver>

Now for the API Level < 12

You have to register for a generic package update receiver by replacing action name in the manifest as below,

MY_PACKAGE_REPLACED with PACKAGE_REPLACED

And in the onReceive() method of the BroadcastReceiver check if it's your application's package name,

if (intent.getDataString().contains("com.your.app")){
...
}
于 2019-09-04T15:10:00.110 回答