標籤:context extend cas nbsp end filter 檔案中 androi 方法
一、建立一個類繼承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 = 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系統中應用的安裝和卸載的監聽