onNewIntent()将不会触发直到手机是醒着 [英] onNewIntent() will not fire until phone is awake

查看:115
本文介绍了onNewIntent()将不会触发直到手机是醒着的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经发现了一些有趣的现象,不知道是什么原因或如何解决它。 该活动是一个s​​ingletask活动,这意味着onNewIntent()是为活动而活动是在堆叠的顶部。这工作得很好很好,的而手机的屏幕上的。一旦屏幕关闭然而,新的意图没有接收直到电话是醒,并在该点的onNewIntent()被调用。如果该活动是不位于堆栈顶部和电话是睡着那么活动已经启动,而的onCreate()方法被调用。

该活动声明:

 <活动机器人:名称=。MyActivity
          机器人:launchMode =singleTask
          机器人:alwaysRetainTaskState =真正的>
 

该活动还推出了与FLAG_ACTIVITY_NEW_TASK标志。意图从广播接收器发射。如果手机是睡着了,其目的是广播活动唤醒手机与这些标志:

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

然而活性不创建直到手机醒时,它是在堆叠的顶部。现在我感到困惑,不知道从哪里开始。还有我的活动使用的onSaveInstanceState()。该活动是一个FragmentActivity并包含一个片段。

任何帮助将是很大的AP preciated!

解决方案
  

然而活动不创建直到电话是清醒的,当它   是在堆叠的顶部。

据我所知,这是由设计。唤醒了电话,并保持清醒的唯一方法就是通过抓住唤醒锁。

http://developer.android.com/reference/android/ OS / PowerManager.WakeLock.html

典型的模式是,你得到一些意图唤醒你的应用程序,如网络状态已经改变,或什么的。在接收器,在 onHandleIntent()你抢唤醒锁。你启动一个服务做一些工作,服务释放它完成之后唤醒锁。

在你的情况,发送的意图,你的活动能抢到局部唤醒锁接收器,那么你的活动可以抓取屏幕唤醒锁。请记住,只要你持有唤醒锁,手机无法入睡......导致它用得多的电池。唤醒锁是非常危险的事情,会严重损害可用性。

请注意,你必须做锁的手了。如果你不持有,当 onHandleIntent()返回时,设备可以直接回到睡眠状态唤醒锁。所以它会是这样,

 接收器:获得部分锁定
活动:购买锁屏
活动:释放部分锁定
 

您也可能只是抓取屏幕锁临时,然后松开......屏幕会来的,然后熄灭正常。

I have found some interesting behavior and do not know why or how to get around it. The activity is a singletask activity, which means onNewIntent() is for the activity while the activity is on the top of the stack. This works fine and well, while the phone's screen is on. Once the screen is off however, the new intent is not received until the phone is "awake" and at that point the onNewIntent() is called. If the activity is not at the top of the stack and the phone is asleep then the activity is started and the onCreate() method is called.

The Activity declared as:

<activity android:name=".MyActivity" 
          android:launchMode="singleTask" 
          android:alwaysRetainTaskState="true" >

The Activity is also launched with the FLAG_ACTIVITY_NEW_TASK flag. The intent is launched from a broadcast receiver. If the phone is asleep and the intent is broadcast the activity wakes the phone up with these flags:

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

However the activity is not created until the phone is awake when it is on the top of the stack. Right now I am perplexed and not sure where to start. Also my activity uses onSaveInstanceState(). The activity is a FragmentActivity and contains one fragment.

Any help would be greatly appreciated!

解决方案

However the activity is not created until the phone is awake when it is on the top of the stack.

as far as i know, that's by design. the only way to wake up the phone and keep it awake is by grabbing a wake lock.

http://developer.android.com/reference/android/os/PowerManager.WakeLock.html

the typical pattern is that you receive some intent to wake up your app, like the network state has changed, or whatever. in your receiver, in onHandleIntent() you grab a wake lock. you start a service to do some work, and the service releases the wake lock after it is done.

in your case, the receiver that sends the intent to your activity can grab a partial wake lock, then your activity can grab a screen wake lock. keep in mind that as long as you hold the wake lock, the phone cannot sleep ... causing it to use much more battery. wake locks are very dangerous things that can severely harm usability.

note that you must do the lock hand off. if you don't hold a wake lock when onHandleIntent() returns, the device can go right back to sleep. so it'd be something like,

receiver: acquire partial lock
activity: acquire screen lock
activity: release partial lock

you could also probably just grab the screen lock temporarily, then release it ... the screen would come on, then go off normal.

这篇关于onNewIntent()将不会触发直到手机是醒着的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆