简体   繁体   English

处理FLAG_ACTIVITY_NEW_TASK的NewIntent

[英]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 : 根据您的配置,这应该可以工作:

  • build an application class as a singleton, later you can access it easily 以单例形式构建应用程序类,以后您可以轻松访问它
  • give it a data member that is a data model, it will be accessible both from the C2DM notification receiver and all acitivies 给它一个作为数据模型的数据成员,就可以从C2DM通知接收器和所有活动中访问它
  • build your data model according to observable-Observer design pattern 根据可观察的观察者设计模式构建数据模型
  • the C2DM notification will change the model C2DM通知将更改模型
  • activities , in onCreate will plug an event listener, unplug during onDestroy 活动,在onCreate中将插入事件监听器,在onDestroy期间拔出
  • when the model changes it fires an event to all listeners, and activities, when they receive the event, update their UI 当模型更改时,它将向所有侦听器触发一个事件,而活动在他们接收到该事件时会更新其UI

This is a good opportunity to get a better design in your app too. 这也是在您的应用程序中获得更好设计的好机会。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 错误FLAG_ACTIVITY_NEW_TASK标志 - error FLAG_ACTIVITY_NEW_TASK flag Android FLAG_ACTIVITY_NEW_TASK无效 - Android FLAG_ACTIVITY_NEW_TASK not working 关于上下文FLAG_ACTIVITY_NEW_TASK的错误 - error about context FLAG_ACTIVITY_NEW_TASK 使用FLAG_ACTIVITY_NEW_TASK恢复播放后停止音乐 - Stop music after resume using FLAG_ACTIVITY_NEW_TASK 解析活动上下文需要FLAG_ACTIVITY_NEW_TASK标志。 这真的是您想要的吗? - Resolve Activity context requires the FLAG_ACTIVITY_NEW_TASK flag. Is this really what you want? 上下文需要 FLAG_ACTIVITY_NEW_TASK 但我已经设置了那个标志 - Context wants FLAG_ACTIVITY_NEW_TASK but I've already set that flag 虽然已经设置了标志 - 从Activity上下文外部调用startActivity()需要FLAG_ACTIVITY_NEW_TASK标志 - Although flag is already set - Calling startActivity() from outside of an Activity context requires the FLAG_ACTIVITY_NEW_TASK flag Android Studio - 如何修复使用 flag_activity_clear_task 后后退按钮仍然有效 | flag_activity_new_task? - Android Studio - How to fix back button still working after using flag_activity_clear_task | flag_activity_new_task? FLAG_ACTIVITY_NEW_TASK不打开以前的活动,而仅在新安装apk时打开 - FLAG_ACTIVITY_NEW_TASK doesn't open previous activity but only on fresh apk install 奇怪的行为:首次使用FLAG_ACTIVITY_NEW_TASK启动时,活动无法重新开始 - Weird behaviour : Activity not getting started again when first started with FLAG_ACTIVITY_NEW_TASK
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM