Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

BroadcastReceiver: set android:process programmatically

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?

like image 245
Anton Cherkashyn Avatar asked Nov 13 '22 06:11

Anton Cherkashyn


1 Answers

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.

like image 107
CommonsWare Avatar answered Nov 15 '22 00:11

CommonsWare