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的APP自动更新程序,检测版本,然后下载安装,但app安装后不提示“完成,打开”?
- android 自动更新APP版本,并使用ProgressDialog显示进度
- Android App实现应用内部自动更新的最基本方法示例
- android应用版本自动更新
- Android应用更新-自动检测版本及自动升级
- Android实现App版本自动更新
- android应用版本自动更新
- Android实现APP版本自动更新功能
- Android实现App版本自动更新
- Android(3)—Mono For Android App版本自动更新(2)
- Android应用APP自动更新功能的代码实现
- Android实现App版本自动更新
- [置顶] Android实现App版本自动更新
- 用友盟的自动更新组件实现Android应用版本升级
- 自动更新Android应用后, app 进入后台重进会重启
- 实现android应用版本自动更新
- Android应用更新之自动检测版本及自动升级
- Android实现App版本自动更新
- android应用版本自动更新
- Android App 自动更新版本