我有一个 BroadcastReceiver
应该启动 Activity :
@Override
public void onReceive(Context context, Intent intent)
{
if (wakeLock == null)
{
PowerManager pm = (PowerManager) ApplicationScreen.instance.getApplicationContext()
.getSystemService(Context.POWER_SERVICE);
wakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, TAG);
}
if (!wakeLock.isHeld())
{
wakeLock.acquire();
}
try
{
if (ApplicationScreen.instance != null) {
Intent dialogIntent = new Intent(context, MainScreen.class);
dialogIntent.addFlags(Intent.FLAG_FROM_BACKGROUND | Intent.FLAG_ACTIVITY_REORDER_TO_FRONT | Intent.FLAG_ACTIVITY_SINGLE_TOP);
ApplicationScreen.instance.startActivity(dialogIntent);
} else {
Intent dialogIntent = new Intent(context, MainScreen.class);
dialogIntent.addFlags(Intent.FLAG_FROM_BACKGROUND | Intent.FLAG_ACTIVITY_REORDER_TO_FRONT | Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP);
context.startActivity(dialogIntent);
}
} catch (NullPointerException e)
{
}
}
我的 MainScreen
是:
@Override
public void onResume()
{
Log.e("TAG", "onResume");
super.onResume();
}
@Override
protected void onNewIntent(Intent intent)
{
super.onNewIntent(intent);
// getIntent() should always return the most recent
setIntent(intent);
Log.e("TAG", "onNewIntent");
}
onResume
永远不会在 onNewIntent
之后调用。我希望,如果设备进入休眠状态,我的 BroadcastReceiver
应该唤醒它并启动我的 MainScreen
。
还应该告诉您,MainScreen
扩展了 ApplicationScreen
。并且 ApplicationScreen
扩展了 Activity
。
EDIT:
设备没有唤醒。屏幕保持关闭状态。
最佳答案
对我来说唯一可行的解决方案是在调用 onNewIntent()
时使用已过时的 PowerManager.SCREEN_BRIGHT_WAKE_LOCK
。
int SCREEN_BRIGHT_WAKE_LOCK = 10;
PowerManager lPm = (PowerManager) getSystemService(Context.POWER_SERVICE);
WakeLock lWl = lPm.newWakeLock(SCREEN_BRIGHT_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP);
lWl.acquire(1000);
如果 Activity 通过按下电源按钮停止,这将触发 onResume
。然后,您应该使用推荐的 WindowManager
标志 FLAG_TURN_SCREEN_ON
和 FLAG_KEEP_SCREEN_ON
。
关于Android onResume 在 onNewIntent 之后没有被调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30119557/