Alarmmanager, Broadcastreceiver And Service Not Working
Solution 1:
Figured it out!
MyService should be extending IntentService instead of Service!
With this change, it also means that instead of overriding onStartCommand
should be overriding onHandleIntent
instead (see docs on IntentService)
So MyService now looks like this:
publicclassMyServiceextendsIntentService {
publicMyService() {
super("MyServiceName");
}
@OverrideprotectedvoidonHandleIntent(Intent intent) {
Log.d("MyService", "About to execute MyTask");
newMyTask().execute();
}
privateclassMyTaskextendsAsyncTask<String, Void, boolean> {
@OverrideprotectedbooleandoInBackground(String... strings) {
Log.d("MyService - MyTask", "Calling doInBackground within MyTask");
returnfalse;
}
}
}
Note: From the docs, the default implementation of onBind
returns null
so no need to override it.
More information about extending IntentService
: http://developer.android.com/guide/topics/fundamentals/services.html#ExtendingIntentService
Solution 2:
You could get AlarmManager to run the Service straight away rather than going through a BroadcastReceiver, if you change your intent like this:
//Change the intentIntentdownloader=newIntent(context, MyService.class);
downloader.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//Change to getService()PendingIntentpendingIntent= PendingIntent.getService(context, 0, downloader, PendingIntent.FLAG_CANCEL_CURRENT);
This may solve your problem!
Solution 3:
Instead of using
Intent dailyUpdater = new Intent(context, MyService.class);
use
Intent dailyUpdater = new Intent(this, MyService.class);
Other suggestion is to start service directly from alarm rather than sending a broadcast and starting the service in broadcast receiver.
Post a Comment for "Alarmmanager, Broadcastreceiver And Service Not Working"