才可以; intent.getData().getSchemeSpecificPart()得到的是包名。 public class RegisterReceiver extends BroadcastReceiver { p">
 | 注册
请输入搜索内容

热门搜索

Java Linux MySQL PHP JavaScript Hibernate jQuery Nginx
jopen
10年前发布

监听Android应用程序安装卸载的代码

  1. 需要通过receiver来监听;
  2. 在AndroidManifest.xml文件中注册的receiver中必须加上<data android:scheme="package"/>才可以;
  3. intent.getData().getSchemeSpecificPart()得到的是包名。

public class RegisterReceiver extends BroadcastReceiver {        private final static String TAG = "RegisterReceiver";        @Override      public void onReceive(Context context, Intent intent) {          String action = intent.getAction();            if (Intent.ACTION_PACKAGE_REMOVED.equals(action)) {              Log.d(TAG , "package has been removed");              String packageName = intent.getData().getSchemeSpecificPart();              Log.d(TAG, packageName);          } else if(Intent.ACTION_PACKAGE_ADDED.equals(action)) {              Log.d(TAG , "package has been added");          }       }  }

AndroidManifest.xml中增加的receiver

<receiver android:name=".RegisterReceiver">              <intent-filter>                  <action android:name="android.intent.action.PACKAGE_REMOVED" />                                 <action android:name="android.intent.action.PACKAGE_ADDED" />                                <data android:scheme="package"/>              </intent-filter>          </receiver>