广播监听手机中应用程序是否安装并卸载
2018-07-20 来源:open-open
public class PackageReceiver extends BroadcastReceiver { private static final int PACKAGE_NAME_INDEX = 8; @Override public void onReceive(Context context, Intent intent) { if (intent != null) { /**安装程序的ACTION为Intent.ACTION_PACKAGE_ADDED*/ if (intent.getAction().equals(Intent.ACTION_PACKAGE_ADDED)) { String data = intent.getDataString(); if (data == null || data.length() <= PACKAGE_NAME_INDEX) { return; } String packageName = data.substring(PACKAGE_NAME_INDEX); if (isAvilible(context, packageName)) { /**说明安装了应用包*/ Uri uri = Uri.fromParts("package", packageName, null); /**监听应用卸载的ACTION*/ intent = new Intent(Intent.ACTION_DELETE, uri); intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); context.startActivity(intent); } else { Toast.makeText(context, "手机并没有安装该应用!", Toast.LENGTH_SHORT).show(); } } } } /**检测是否安装了微信的客户端*/ public static boolean isAvilible(Context context, String packageName) { /**包管理器PackageManager*/ final PackageManager packageManager = context.getPackageManager(); /**所有已安装程序的包信息*/ List<PackageInfo> pinfo = packageManager.getInstalledPackages(0); /**存储所有已安装程序的包名*/ List<String> pName = new ArrayList<String>(); if (pinfo != null) { for (int i = 0; i < pinfo.size(); i++) { String pn = pinfo.get(i).packageName; pName.add(pn); } } return pName.contains(packageName); } } -----------AndroidManifest.xml中注册广播--------------------------- <receiver android:name=".PackageReceiver"> <intent-filter> <action android:name="android.intent.action.PACKAGE_ADDED" /> <data android:scheme="package" /> </intent-filter> </receiver>
标签:
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点!
本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。
最新资讯
热门推荐