您所能做的就是使用意图过滤器注册一个接收器,android.intent.action.PACKAGE_INSTALL
或者android.intent.action.PACKAGE_REPLACED
您可以从中重新启动您的应用程序。
<receiver android:enabled="true" android:exported="true" android:label="BootService" android:name="com.project.services.BootService">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
<data android:scheme="package"/>
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.PACKAGE_ADDED"/>
<data android:scheme="package"/>
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.PACKAGE_INSTALL"/>
<data android:scheme="package"/>
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.PACKAGE_CHANGED"/>
<data android:scheme="package"/>
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.PACKAGE_REPLACED"/>
<data android:scheme="package"/>
</intent-filter>
</receiver>
</application>
和
public class BootService extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(Intent.ACTION_PACKAGE_ADDED)) {
Intent serviceIntent = new Intent();
serviceIntent.setClass(context,Controller.class);
serviceIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(serviceIntent);
} else if (intent.getAction().equals(Intent.ACTION_PACKAGE_REPLACED)) {
Intent serviceIntent = new Intent();
serviceIntent.setClass(context, Controller.class);
serviceIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(serviceIntent);
}
}
}