I have a minor problem in one of my apps. It uses a BroadCastReceiver
to detect when a call finishes and then performs some minor housekeeping tasks. These have to be delayed for a few seconds, to allow the user to see some data and to ensure that the call log has been updated. I'm currently using handler.postDelayed()
for this purpose:
public class CallEndReceiver extends BroadcastReceiver {
@Override
public void onReceive(final Context context, final Intent intent) {
if (DebugFlags.LOG_OUTGOING)
Log.v("CallState changed "
+ intent.getStringExtra(TelephonyManager.EXTRA_STATE));
if (intent.getStringExtra(TelephonyManager.EXTRA_STATE)
.equalsIgnoreCase(TelephonyManager.EXTRA_STATE_IDLE)) {
SharedPreferences prefs = Utils.getPreferences(context);
if (prefs.getBoolean("auto_cancel_notification", true)) {
if (DebugFlags.LOG_OUTGOING)
Log.v("Posting Handler to remove Notification ");
final Handler mHandler = new Handler();
final Runnable mCancelNotification = new Runnable() {
public void run() {
NotificationManager notificationMgr = (NotificationManager) context
.getSystemService(Service.NOTIFICATION_SERVICE);
notificationMgr.cancel(12443);
if (DebugFlags.LOG_OUTGOING)
Log.v("Removing Notification ");
}
};
mHandler.postDelayed(mCancelNotification, 4000);
}
final Handler updateHandler = new Handler();
final Runnable mUpdate = new Runnable() {
public void run() {
if (DebugFlags.LOG_OUTGOING)
Log.v("Starting updateService");
Intent newBackgroundService = new Intent(context,
CallLogUpdateService.class);
context.startService(newBackgroundService);
}
};
updateHandler.postDelayed(mUpdate, 5000);
if (DebugFlags.TRACE_OUTGOING)
Debug.stopMethodTracing();
try
{
// Stopping old Service
Intent backgroundService = new Intent(context,
NetworkCheckService.class);
context.stopService(backgroundService);
context.unregisterReceiver(this);
}
catch(Exception e)
{
Log.e("Fehler beim Entfernen des Receivers", e);
}
}
}
}
Now I have the problem, that this setup works about 90% of the time. In about 10% of cases, the notification isn't removed. I suspect, that the thread dies before the message queue processes the message/runnable.
I'm now thinking about alternatives to postDelayed()
and one of my choices is obviously the AlarmManager. However, I'm not sure about the performance impact (or the resources it uses).
Maybe there is a better way to ensure that all messages have been processed before a thread dies or another way to delay the execution of those two bits of code.
Thank you
AlarmManager seems not to work very well for short periods of time like 10 seconds and according to user reports the behaviour heavily depends on the firmware.
At the end I decided to use
Handler
andRunnable
in my service.When creating the
Handler
, be sure to create it inside the Service class, not inside the BroadcastReceiver since in the last case you'll getCan't create Handler inside thread that has not called Looper.prepare()
In addition to the first answer, you might want to consider what the API documentation says for the
onReceive
method:So it looks like generally it is not a good idea to start something that waits a couple of time within
onReceive
(even though, in your case it's less than the 10s limit).I had a similar timinig problem with the BroadcastReceiver. I couldn't get my results processed even though I
onReceive
had been called with exactly what I was exepcting. It seemed that the thread theBroadastReceiver
was running in, got killed before my result processing could finish. My solutuion was to kick off a new thread to perform all processing.That's not a good idea, assuming the
BroadcastReceiver
is being triggered by a filter in the manifest.More accurately, the process is terminated, taking everything with it.
It's not that bad. Another possibility is to do your delayed work in an
IntentService
-- triggered via a call tostartService()
-- and have it sleep on its background thread for a couple of seconds.Let's try a new way of doing this. Using RxJava. It's much simpler to prototype and easier to manage lots of threads if you want to ever run hundreds of such delayed tasks concurrently, sequentially, coupled with async tasks, chained with synchronous chained async calls etc.
Firstly, set up the Subscriber. Remember
new
onSubscriber
should be done only once to avoid memory leaks.The snippet below will just emit the
1
in the onNext() of the subscriber. Note that this is done on the Computation Threadpool created and managed by the RxJava library.If you want to run a code after every one second, say, then you can do this: