如何捕获或接收安装的应用程序的android os'广播'?

时间:2011-07-16 11:36:14

标签: android

我想接收Android安装的应用程序的广播。会是什么程序?

1 个答案:

答案 0 :(得分:8)

如果需要,您可以注册广播意图Intent.ACTION_PACKAGE_ADDED(和/或Intent.ACTION_PACKAGE_REMOVEDIntent.ACTION_PACKAGE_CHANGED

代码如下:

void registerReceiver() {
    IntentFilter filter = new IntentFilter(Intent.ACTION_PACKAGE_ADDED);
    filter.addAction(Intent.ACTION_PACKAGE_REMOVED);
    filter.addAction(Intent.ACTION_PACKAGE_CHANGED);
    filter.addDataScheme("package");
    ...
}

public void onReceive(Context context, Intent intent) {
    String actionStr = intent.getAction();
    if (Intent.ACTION_PACKAGE_ADDED.equals(actionStr)) {
        Uri data = intent.getData();
        String pkgName = data.getEncodedSchemeSpecificPart();
        //handle package adding...
    ...
    }
}