Android onResume 未在 onNewIntent 之后调用
Android onResume not called after onNewIntent
我有一个 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()
总是会调用 singleTop/Task
活动,但第一次创建 activity 时除外。那个时候叫onCreate
.
您可以通过将 onNewIntent
放入 onCreate
方法来调用它,例如:
@Override
public void onCreate(Bundle savedState)
{
super.onCreate(savedState);
onNewIntent(getIntent());
}
@Override
protected void onNewIntent(Intent intent)
{
super.onNewIntent(intent);
//code
}
而且,您的 activity 已经经历了它的生命周期才能恢复。如果该行为恢复,他们可以简单地调用 onNewIntent()
并留在恢复中。
对我来说唯一可行的解决方案是在调用 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
.
我有一个 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()
总是会调用 singleTop/Task
活动,但第一次创建 activity 时除外。那个时候叫onCreate
.
您可以通过将 onNewIntent
放入 onCreate
方法来调用它,例如:
@Override
public void onCreate(Bundle savedState)
{
super.onCreate(savedState);
onNewIntent(getIntent());
}
@Override
protected void onNewIntent(Intent intent)
{
super.onNewIntent(intent);
//code
}
而且,您的 activity 已经经历了它的生命周期才能恢复。如果该行为恢复,他们可以简单地调用 onNewIntent()
并留在恢复中。
对我来说唯一可行的解决方案是在调用 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
.