BroadcastReceiver: set android: software process

I want my application to detect if the state of the external storage has changed. First BroadcastReceiver was defined in my AndroidManifest. Here I can set the android:process and android:exported attributes as follows:

  <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 use this receiver in only one action, so there is no need to create it when the application starts, instead I can define it programmatically in the code. Here 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 the onCreate () method of my activity.

But I can not find a way to install process from the code. I read the documentation for the BroadcastReceiver and Context classes. BroadcastReceiver does not seem to contain methods that allow you to determine the process name. registerReceiver (), on the other hand, can take two additional arguments: String broadcastPermission , Handler scheduler . The handler sounds promising, but I could not find a Handler constructor that would accept the process name as a string. I feel like I'm running out of ideas. Is there a way to set the process name programmatically?

+4
source share
1 answer

Then I realized that I use this receiver in only one action, so there is no need to create it when the application starts, instead I can define it programmatically in the code.

A registered BroadcastReceiver manifest is not "created when the application starts." It is created only when the corresponding broadcast is sent.

But I can not find a way to install the process from the code.

This is because it is not possible. In addition, you do not need it, and it harms the user, losing memory, processor and battery. You should not have the android:process attribute in the manifest entry, unless you know completely and exactly why another process is needed. The vast majority of Android apps do not.

+1
source

All Articles