Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Handling onNewIntent for FLAG_ACTIVITY_NEW_TASK

I have the Activity running in singleTop mode and C2DM receiver. On some notification I need to run that activity and I doing it in that way:

Intent activity = new Intent(context, klass);
activity.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
context.startActivity(activity);

When activity if background (e.g. Home button pressed before), everything works fine. But when I just pressed Power button to switch off screen, running activity cannot be notified about some changes (onNewIntent never called).

How can I notify running activity about notification?

like image 572
skayred Avatar asked Mar 22 '12 04:03

skayred


1 Answers

Skayred, I believe I had the exact same situation. However I noticed when the phone was asleep and a new intent was sent to the activity it would not start the activity until the screen was on(For my purposes I wanted the screen on).

My solution was to acquire a wakelock in my C2DM receiver.

PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
mWakeLock = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK|PowerManager.ACQUIRE_CAUSES_WAKEUP,     TAG);
mWakeLock.acquire();

Of course, be sure to release the lock with mWakeLock.release() in your Activity.

This is strange behavior and it does not seem is consistent with other android activity behavior. In my case I am using a singleTask activity(I'm not sure what type of activity your using, you did not state). If there is not an instance of the activity at the top of the stack, and the phone is asleep my activity will start and I can use the following in the onCreate():

getActivity().getWindow().addFlags(WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON); 
getActivity().getWindow().addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED);

However if the activity is on the top of the stack and the phone is asleep the activity is not started until I turn the screen on. Hopefully this works for you. If you don't need the screen to come on I would try playing with the other flags for the wakelock.

like image 174
Patrick Avatar answered Nov 08 '22 19:11

Patrick