您的位置:首页 > 移动开发

Android版本更新完毕自动开启APP应用

2016-11-29 14:13 549 查看
1.Manifest文件权限配置:
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<!--开机自启权限:开机时接收系统广播并开启APP-->
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
2.新增Download APK & 安装:
<script src="https://code.csdn.net/snippets/2015269.js"></script>
3. Android 监听apk安装替换卸载广播,Manifest.xml注册广播接收器
首先是要获取应用的安装状态,通过广播的形式
<!--以下是和应用程序相关的Broadcast Action-->
ACTION_PACKAGE_ADDED 一个新应用包已经安装在设备上,数据包括包名(最新安装的包程序不能接收到这个广播)
ACTION_PACKAGE_REPLACED     一个新版本的应用安装到设备,替换之前已经存在的版本
ACTION_PACKAGE_CHANGED  一个已存在的应用程序包已经改变,包括包名
ACTION_PACKAGE_REMOVED  一个已存在的应用程序包已经从设备上移除,包括包名(正在被安装的包程序不能接收到这个广播)
ACTION_PACKAGE_RESTARTED   用户重新开始一个包,包的所有进程将被杀死,所有与其联系的运行时间状态应该被移除,包括包名(重新开始包程序不能接收到这个广播)
ACTION_PACKAGE_DATA_CLEARED    用户已经清楚一个包的数据,包括包名(清除包程序不能接收到这个广播)
<receiver
android:name=".PkInstallReceiver"
android:label=" @string/app_name">
<intent-filter>
<action android:name="android.intent.action.PACKAGE_ADDED" />
<action android:name="android.intent.action.PACKAGE_REMOVED" />
<action android:name="android.intent.action.PACKAGE_REPLACED"/>
<data android:scheme="package" />
</intent-filter>
</receiver>
4.增加监听替换完成后开启制定的APP
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;

public class PkInstallReceiver extends BroadcastReceiver {
private String TAG = PkInstallReceiver.class.getSimpleName();

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

if (intent.getAction().equals("android.intent.action.PACKAGE_ADDED")) {
String packageName = intent.getDataString().substring(8);
System.out.println("安装:" + packageName + "包名的程序");
Log.d(TAG,"安装:" + packageName + "包名的程序");
}

//接收卸载广播
if (intent.getAction().equals("android.intent.action.PACKAGE_REPLACED")) {
String packageName = intent.getDataString().substring(8);
System.out.println("卸载:" + packageName + "包名的程序");
Log.d(TAG,"卸载:" + packageName + "包名的程序");
Intent newIntent = new Intent();
//MainActivity 为APP入口程式(替换为对应的Java文件名即可)
newIntent.setClassName(packageName, packageName + ".MainActivity");
newIntent.setAction("android.intent.action.MAIN");
newIntent.addCategory("android.intent.category.LAUNCHER");
newIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Log.e(TAG,"Start ACTIVITY");
context.startActivity(newIntent);
}
}

}
Appendix:开机自动启动APP:
import android.content.BroadcastReceiver;import android.content.Context;import android.content.Intent;import android.util.Log;public class BootUpReceiver extends BroadcastReceiver {private String TAG = BootUpReceiver.class.getSimpleName();@Overridepublic void onReceive(Context context, Intent intent) {Log.d(TAG, "-BootUpReceiver-onReceive--");Intent i = new Intent(context, MainActivity.class);i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);context.startActivity(i);}}

                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  android