首页  编辑  

安卓当中如何在安装之后重启应用

Tags: /Android/   Date Created:
如何在安卓当中,安装之后,立即重启应用。
How to launch application in Android after reinstall/upgrade/update application?
首先添加一个类,如下:
package com.xxxx.batcom;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;

public class PackageBroadcast extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
String localPkgName = context.getPackageName();//取得MyReceiver所在的App的包名
Uri data = intent.getData();
String installedPkgName = data.getSchemeSpecificPart();//取得安装的Apk的包名,只在该app覆盖安装后自启动
if ((action.equals(Intent.ACTION_PACKAGE_ADDED)
|| action.equals(Intent.ACTION_PACKAGE_REPLACED)) && installedPkgName.equals(localPkgName)) {
Intent launchIntent = new Intent(context, MainActivity.class);
launchIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(launchIntent);
}
}
}
然后修改App的Manifest文件,增加下面的代码即可:
<receiver android:name=".PackageBroadcast">
<intent-filter>
<action android:name="android.intent.action.PACKAGE_ADDED" />
<action android:name="android.intent.action.PACKAGE_REPLACED" />
<action android:name="android.intent.action.PACKAGE_REMOVED" />
<data android:scheme="package" />
</intent-filter>
</receiver>