0

I have an alarm application. Flow looks like this :

WakefulBroadcastReceiver(Acquires wakelock) --->> Intent service -->> startActivity

public class AlarmService extends IntentService {
    protected void onHandleIntent(Intent intent) {
        Intent activityIntent = new Intent(this, TriggeredActivity.class);
        activityIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        startActivity(activityIntent);

Basically WakefulBroadcaseReceiver starts an intent service using startWakefulService(). Inside intent service's onHandleIntent(), only work I am doing is further starting a new activity using startActivity(). That new activity is where I am using mediaPlayer in a loop, which sounds the alarm. That activity has a dismiss button, which waits for user click to stop the media player & activity finishes. Now the problem I am facing is that after calling startactivity() inside intent service, I can not wait for TriggeredActivity to finish(no equivalent to startActivityForResult in Service) and then complete wakeful intent. Related link

startActivity(activityIntent); WakefulBCastReceiver.completeWakefulIntent(intent); /* can't do here */

So I am not explicitly releasing wakelock here.

My question is will the wakelock be released automatically(link-to-death), when the process that is holding it is killed. If yes, then in my particular scenario, I need not call WakefulBCastReceiver.completeWakefulIntent().

Community
  • 1
  • 1
gaurav.28ch
  • 39
  • 2
  • 6

2 Answers2

0

Yes, you need to use completeWakefulIntent.

You need to put your TriggeredActivity intent into EXTRAs.

@Override
public void onReceive(Context context, Intent intent) {

    Intent intentService = new Intent(context, NotificationsIntentService.class);
    // Inserting data inside the Intent
    intentService.putExtra(NotificationsIntentService.EXTRA_NOTIF, new Intent(context, TriggeredActivity.class));
    startWakefulService(context, intentService);
}

NotificationsIntentService.class

public class NotificationsIntentService extends IntentService {

     private static final String TAG = "DebugNotifIntent";

     public static final String EXTRA_NOTIF = "extra_notif";

     public NotificationsIntentService(){
         super(NotificationsIntentService.class.getSimpleName());
     }

     @Override
     protected void onHandleIntent(Intent intent) {
          Log.d(TAG, "onHandleIntent: ");
          Intent extraIntent = intent.getParcelableExtra(EXTRA_NOTIF);
          extraIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
          startActivity(extraIntent);

          NotificationWakefulBroadcastReceiver.completeWakefulIntent(intent);
     }

     @Override
     public void onDestroy() {
         super.onDestroy();
         Log.d(TAG, "onDestroy: ");
     }
}
aleksandrbel
  • 1,422
  • 3
  • 20
  • 38
0

I have managed to find a solution for my problem. I am now using a Messenger for message based cross process communication between intent service & triggered activity. I am passing a handler - alarmServiceHandler, from intent service to activity through a messenger.

Handler alarmServiceHandler = new Handler(){
    @Override
    public void handleMessage(Message msg) {
        if(msg.arg1 == 1) {
            completedTriggeredActivity = true;
        }
    }
};

Inside onHandleIntent(), I am passing handler through Messenger object in intent's extra data.

    Messenger alarmServiceMessenger = new Messenger(alarmServiceHandler);
    Intent activityIntent = new Intent(this, TriggeredActivity.class);
    activityIntent.putExtra("AlarmServiceMessenger", alarmServiceMessenger);
    activityIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    startActivity(activityIntent);


    while(!completedTriggeredActivity){
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
    WakefulBCastReceiver.completeWakefulIntent(intent);

In TriggeredActivity, I am retrieving messenger in Dismiss button's OnClickListener, just before calling finish() on the activity. And sending back a message to AlarmService with arg = 1, implying end of processing in triggered activity.

buttonDismiss.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                Messenger alarmServiceMessenger = getIntent().getParcelableExtra("AlarmServiceMessenger");
                Message alarmServiceMessage = Message.obtain();
                alarmServiceMessage.arg1 = 1;
                try {
                    alarmServiceMessenger.send(alarmServiceMessage);
                } catch (RemoteException e) {
                    e.printStackTrace();
                }

                finish();

            }

After starting triggered activity, I am putting AlarmService in sleep mode till boolean variable completedTriggeredActivity has not been set to true in handleMessage(). Once true, it means triggered activity has finished & now I can proceed with releasing wake lock.

I would be glad to receive comments about my approach & any suggestions towards a better solution to my problem, than the one I have deviced.

gaurav.28ch
  • 39
  • 2
  • 6