BroadcastReceiver: set android:process programmatically

不羁的心 提交于 2019-12-10 13:48:16

问题


I want my app to detect if when the state of external storage changes. At first defined a BroadcastReceiver in my AndroidManifest. Here I can set android:process and android:exported attributes like this:

    <receiver android:name=".StorageStateReceiver" android:process=":storage_state" android:exported="false">
        <intent-filter>
            <action android:name="android.intent.action.MEDIA_UNMOUNTED" />
            <action android:name="android.intent.action.MEDIA_MOUNTED" />
            <action android:name="android.intent.action.MEDIA_EJECT" />
            <action android:name="android.intent.action.MEDIA_BAD_REMOVAL" />
            <data android:scheme="file" />
        </intent-filter>
    </receiver>

Then I realized that I only use this receiver in one single Activity, so there's no need to have it instantiated when the app starts, instead I can define it programmatically in code. This is what I came up with:

BroadcastReceiver StorageStateReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            // Do what needs to be done
        }
    };
IntentFilter filter = new IntentFilter();
filter.addAction(Intent.ACTION_MEDIA_UNMOUNTED);
filter.addAction(Intent.ACTION_MEDIA_MOUNTED);
filter.addAction(Intent.ACTION_MEDIA_EJECT);
filter.addAction(Intent.ACTION_MEDIA_BAD_REMOVAL);
filter.addDataScheme("file");
getApplicationContext().registerReceiver(StorageStateReceiver, filter);

I put this code in onCreate() method of my activity.

But I can't find a way to set process from code. I've read documentation on BroadcastReceiver and Context classes. BroadcastReceiver doesn't seem to host any methods that let you define process name. registerReceiver() on the other hand can acceps two extra arguments: String broadcastPermission, Handler scheduler. Handler sounds promising, but I couldn't find a Handler constructor that would accept process name in a form of a string. I feel that I ran out of ideas. Is there a way to set process name programmatically?


回答1:


Then I realized that I only use this receiver in one single Activity, so there's no need to have it instantiated when the app starts, instead I can define it programmatically in code.

A manifest-registered BroadcastReceiver is not "instantiated when the app starts". It is instantiated only when a matching broadcast is sent.

But I can't find a way to set process from code.

That is because it is impossible. Besides, you do not need it, and it harms the user by wasting RAM, CPU, and battery. You should not have had the android:process attribute in the manifest entry, either, unless you know completely and precisely why you need another process. The vast majority of Android apps do not.



来源:https://stackoverflow.com/questions/16126030/broadcastreceiver-set-androidprocess-programmatically

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!