[英]Handling onNewIntent for FLAG_ACTIVITY_NEW_TASK
I have the Activity running in singleTop mode and C2DM receiver. 我的Activity在singleTop模式和C2DM接收器中运行。 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 (eg Home button pressed before), everything works fine. 当活动为背景时(例如,按下Home按钮),一切正常。 But when I just pressed Power button to switch off screen, running activity cannot be notified about some changes ( onNewIntent
never called). 但是,当我只是按下电源按钮以关闭屏幕时,无法通知运行活动有关某些更改的信息(从未调用过onNewIntent
)。
How can I notify running activity about notification? 如何通知正在运行的活动有关通知?
Skayred, I believe I had the exact same situation. Skayred,我相信我的情况完全一样。 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. 我的解决方案是在C2DM接收器中获取唤醒锁。
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. 当然,请确保在Activity中使用mWakeLock.release()释放锁定。
This is strange behavior and it does not seem is consistent with other android activity behavior. 这是奇怪的行为,似乎与其他android活动行为不一致。 In my case I am using a singleTask activity(I'm not sure what type of activity your using, you did not state). 就我而言,我使用的是singleTask活动(我不确定您使用的是哪种类型的活动,您没有说明)。 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(): 如果堆栈顶部没有该活动的实例,并且电话处于睡眠状态,则我的活动将开始,我可以在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. 如果不需要打开屏幕,我会尝试使用其他标志来唤醒。
According to you configuration, this should work : 根据您的配置,这应该可以工作:
This is a good opportunity to get a better design in your app too. 这也是在您的应用程序中获得更好设计的好机会。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.