Android 无法监听安装,Android关于apk安装成功广播监听

  • Post author:
  • Post category:其他


有这样的应用场景,apk安装成功后需要重启APP,就需要监听完成安装的回调或者广播

public class UpdateReceiver extends BroadcastReceiver {

private static final String TAG = “UpdateReceiver”;

@Override public void onReceive(Context context, Intent intent) {

String action = intent.getAction();

if (Intent.ACTION_PACKAGE_REPLACED.equals(action) || Intent.ACTION_MY_PACKAGE_REPLACED.equals(

action)) {

try {

String scheme = intent.getScheme();

String packageName = intent.getData().getSchemeSpecificPart();

if (context.getPackageName().equals(packageName)) {

Toast.makeText(context, “成功升级新版本!”, Toast.LENGTH_SHORT).show();

Intent intent1 = new Intent(context, FlashActivity.class);

intent1.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

context.startActivity(intent1);

}

LogMgr