一、项目目录结构
二、activity_main.xml和MainActivity.Java没有增加任何代码
三、AppStateReceiver.Java代码
package com.zgs.appListener;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
public class AppStateReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
//获取到当前广播的事件类型
String action = intent.getAction();
if ("android.intent.action.PACKAGE_INSTALL".equals(action)) {
System.out.println("应用被安装了");
}else if ("android.intent.action.PACKAGE_ADDED".equals(action)) {
System.out.println("~~~~~~应用被安装了");
}else if ("android.intent.action.PACKAGE_REMOVED".equals(action)) {
System.out.println("应用被卸载了"+intent.getData());
}
}
}
四、AndroidManifest.xml代码
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.zgs.appListener"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="16"
android:targetSdkVersion="22" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name="com.itheima.appstate.AppStateReceiver">
<intent-filter >
<action android:name="android.intent.action.PACKAGE_INSTALL"/>
<action android:name="android.intent.action.PACKAGE_ADDED"/>
<action android:name="android.intent.action.PACKAGE_REMOVED"/>
<data android:scheme="package"/>
</intent-filter>
</receiver>
</application>
</manifest>