Android实践 -- 监听应用程序的安装、卸载
16lz
2021-01-25
监听应用程序的安装、卸载
在AndroidManifest.xml
中注册一个静态广播,监听安装的广播
android.intent.action.PACKAGE_ADDED
监听程序卸载的广播
android.intent.action.PACKAGE_REMOVED
,在广播中一定要加上
不然就监听不到
<receiver android:name=".AppInstallReceiver" android:enabled="true"> <intent-filter> <action android:name="android.intent.action.PACKAGE_ADDED" /> <action android:name="android.intent.action.PACKAGE_REMOVED" /> <data android:scheme="package" /> intent-filter> receiver>
在java代码中,需要写一个类继承 BroadcastReceiver
public class AppInstallReceiver extends BroadcastReceiver{ @Override public void onReceive(Context context, Intent intent) { String action = intent.getAction(); if(action.equals(Intent.ACTION_PACKAGE_ADDED)){ Log.d("tag","app installed "); }else if(action.equals(Intent.ACTION_PACKAGE_REMOVED)){ Log.d("tag","app uninstalled"); } } }
可以通过intent
获取应用的报名
String pkgName = intent.getDataString().substring(8)
更多相关文章
- 没有一行代码,「2020 新冠肺炎记忆」这个项目却登上了 GitHub 中
- Nginx系列教程(四)| 一文带你读懂Nginx的动静分离
- Android处理图像数据转换的各种方法
- Android(安卓)源代码下载
- Android(安卓)App启动图启动界面(Splash)的简单实现
- Android(安卓)Annotations(1)
- 【Android(安卓)开发教程】TimePicker
- Android(安卓)在子线程中更新UI
- 在 Ubuntu 下配置 Android(安卓)开发环境