How to catch or receive android os ' broadcasts' of installed applications?

后端 未结 1 1562
渐次进展
渐次进展 2020-12-12 07:21

I want to receive broadcasts of android installed applications. What would be the procedure ?

相关标签:
1条回答
  • 2020-12-12 08:03

    You can register Broadcast intent Intent.ACTION_PACKAGE_ADDED ( and/or Intent.ACTION_PACKAGE_REMOVED, Intent.ACTION_PACKAGE_CHANGED if needed).

    Code is something like following:

    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...
        ...
        }
    }
    
    0 讨论(0)
提交回复
热议问题