标签:
手机中的应用在安装、更新和卸载时都会发送广播
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="xidian.dy.com.chujia"> <uses-permission android:name="android.permission.RECEIVE_SMS"/> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:supportsRtl="true" android:theme="@style/AppTheme"> <activity android:name=".MainActivity" android:label="主界面"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <receiver android:name=".SDstatusReceiver"> <intent-filter> <action android:name="android.intent.action.PACKAGE_ADDED"/> <action android:name="android.intent.action.PACKAGE_CHANGED"/> <action android:name="android.intent.action.PACKAGE_REMOVED"/> <data android:scheme="package"/> </intent-filter> </receiver> </application> </manifest>
在清单文件中设置要接收的广播
package xidian.dy.com.chujia; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.net.Uri; import android.widget.Toast; /** * Created by dy on 2016/7/12. */ public class APPstatusReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { //判断收到的广播类型 String action = intent.getAction(); Uri uri = intent.getData(); if("android.intent.action.PACKAGE_ADDED".equals(action)){ Toast.makeText(context,uri.toString() + "被安装", Toast.LENGTH_LONG).show(); } else if("android.intent.action.PACKAGE_CHANGED".equals(action)){ Toast.makeText(context, uri.toString()+ "被更新了", Toast.LENGTH_SHORT).show(); } else if("android.intent.action.PACKAGE_REMOVED".equals(action)){ Toast.makeText(context, uri.toString() + "被卸载", Toast.LENGTH_SHORT).show(); } } }
标签:
原文地址:http://www.cnblogs.com/xidongyu/p/5662452.html