How to Get a Reliable Broadcastreceiver

I want Broadcastreceiver to be called every time Intent.ACTION_BATTERY_CHANGED gets broacasted, so I can track some data.

What I have now:

public class ReceiverApplication extends Application { @Override public void onCreate() { // Register Receiver } public static BroadcastReceiver myReceiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { // Start Service to progress data } }; } 

This works fine until I use the "Task Manager" of the latest applications to remove one of my preference screens or my main action. Then BroadcastReceiver is no longer called. If I start one of my actions, it will call onCreate () ReceiverApplication and work again.

I could get around this by having an empty service that runs all the time. The service does not seem to be killed by scrolling activity, and onCreate () is called after some time and restarts BroadcastReceiver, doing nothing. The problem here is that I do not want the service to work all the time.

So, how can I get a Broadcastreceiver outside of any Activity or Service that can receive broadcasts as long as the user wants?

+4
source share
3 answers

Instead of inserting a broadcast receiver and registering it dynamically, declare it in your Android manifest . According to the documentation, BATTERY_CHANGED cannot be declared in the manifest:

You cannot get this through components declared in the manifest, only by explicitly registering for it using Context.registerReceiver (). See ACTION_BATTERY_LOW, ACTION_BATTERY_OKAY, ACTION_POWER_CONNECTED and ACTION_POWER_DISCONNECTED for various battery-related transmissions that are sent and received via manifest receivers.

I would register a broadcast receiver to get all of these events:

  <receiver android:name="MyBroadcastReceiver"> <intent-filter> <action android:name="android.intent.action.BATTERY_LOW" /> <category android:name="android.intent.category.DEFAULT" /> </intent-filter> <intent-filter> <action android:name="android.intent.action.BATTERY_OKAY" /> <category android:name="android.intent.category.DEFAULT" /> </intent-filter> <intent-filter> <action android:name="android.intent.action.ACTION_POWER_CONNECTED" /> <category android:name="android.intent.category.DEFAULT" /> </intent-filter> <intent-filter> <action android:name="android.intent.action.ACTION_POWER_DISCONNECTED" /> <category android:name="android.intent.category.DEFAULT" /> </intent-filter> </receiver> 

The broadcast receiver will be notified whether your application is running or not.

0
source

I want Broadcastreceiver to be called every time Intent.ACTION_BATTERY_CHANGED gets broacasted, so I can track some data.

This broadcast can be sent a lot. Consequently, Android only allows you to run applications to receive this broadcast, so you do not need to fork a bunch of processes, just to say a bunch of applications "hey, the battery level has changed."

This works fine until I use the "Task Manager" of the latest applications to remove one of my preference screens or my main action. Then BroadcastReceiver will no longer be called.

This act completes your process.

So, how can I get a Broadcastreceiver outside of any Activity or Service that can receive broadcasts as long as the user wants?

Usually you register to broadcast in the manifest. For ACTION_BATTERY_CHANGED this is not possible.

Remember that you can poll ACTION_BATTERY_CHANGED - instead of calling registerReceiver() with the actual BroadcastReceiver , go to null . The return value of registerReceiver() will be the last ACTION_BATTERY_CHANGED Intent . So, if you can walk away with a battery level detection every few minutes, use the AlarmManager to wake up so often and check the current battery level.

+2
source

In addition to the answer already made, itโ€™s normal that your application stops working correctly when you โ€œdelete itโ€ using the task manager. Taskmanager will stop it because the user will request it.

If you do not want to stop the life cycle of your activity, just do not do this ... Your application will call the onPause () method and others if necessary, and your brochure will still be available even if the user does not actually use your application. Task Manager will kill the application and all threads associated with it.

If you want to get detailed information about the life cycle of an activity, check out the official documentation: http://developer.android.com/reference/android/app/Activity.html

0
source

Source: https://habr.com/ru/post/1434695/


All Articles