首页 > 代码库 > Android系统中应用的安装和卸载的监听

Android系统中应用的安装和卸载的监听

一、创建一个类继承BroadcastReceiver并且复写onReceive的方法

public class AppStateReceiver extends BroadcastReceiver
{
    @Override
    public void onReceive(Context context, Intent intent)
    {
        String action = intent.getAction();
        if ("android.intent.action.PACKAGE_ADDED".equals(action))
        {
            Toast.makeText(context, "应用被安装了", Toast.LENGTH_SHORT).show();
        } else if ("android.intent.action.PACKAGE_REMOVED".equals(action))
        {
            //获取被卸载的包名
            Uri data =http://www.mamicode.com/ intent.getData();
            String pac = data.toString();
            Toast.makeText(context, pac, Toast.LENGTH_SHORT).show();
            Toast.makeText(context, "应用被卸载了", Toast.LENGTH_SHORT).show();
        }
    }
}

二、在AndroidManifest.xml文件中配置如下:

   <receiver android:name=".AppStateReceiver">
            <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>

 

Android系统中应用的安装和卸载的监听