Issue
I have an object that schedules a service through the AlarmManager like this:
public void schedule(MyEnum enum) {
Intent intent = new Intent(context, MyService.class);
intent.setAction(enum.name());
Bundle args = new Bundle();
args.putSerializable("MyEnum", content);
intent.putExtra("Bundle", args);
PendingIntent pendingIntent = PendingIntent.getService(context, enum.ordinal(),
intent, 0);
alarmManager.set(AlarmManager.RTC_WAKEUP, 0, pendingIntent);
}
The service:
public class MyService extends IntentService {
@Override
public void onHandleIntent(Intent intent) {
Log.d("this: " + this);
}
}
The way I start the scheduler:
scheduler.schedule(MyEnum.ONE);
scheduler.schedule(MyEnum.TWO);
And What I see in the logs:
MyService@41ea7858
MyService@41ea7858
I tried using a BroadcastReceiver, I also played with the flags, but same result. Then I tried something like that:
button1.setOnClickListener() { scheduler.scheduler(MyEnum.ONE); }
button2.setOnClickListener() { scheduler.scheduler(MyEnum.TWO); }
And if I click on button1 then button2 right away, I see the same Id in the logs. But if I leave a bit more time between the clicks, I get two different Ids...
Any idea what's going on and how to avoid that? Thanks!
Solution
You can not.
Services and BroadcastReceivers are different from Activities in such a way, that there can only be one instance at the same time.
Only when the first instance is destroyed, a new is created. That's what you experience with your second experiment.
Answered By - flx
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.