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?