在OnNewIntent之后没有调用Android onResume

时间:2015-05-08 08:47:16

标签: android android-intent android-activity android-broadcast onresume

我有一个BroadcastReceiver应该启动活动:

@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延伸ApplicationScreenApplicationScreen扩展Activity

  

编辑:

设备无法唤醒。屏幕保持关闭状态。

2 个答案:

答案 0 :(得分:1)

对我来说唯一可行的解​​决方案是在调用PowerManager.SCREEN_BRIGHT_WAKE_LOCK时使用破旧的onNewIntent()

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);  

如果按下电源按钮停止活动,则会触发onResume。然后,您应该使用推荐的WindowManager标记FLAG_TURN_SCREEN_ONFLAG_KEEP_SCREEN_ON

答案 1 :(得分:0)

除了第一次创建活动时,

OnNewIntent()始终会调用singleTop/Task个活动。那时调用onCreate

您可以通过将onNewIntent方法添加到onCreate方法中来调用@Override public void onCreate(Bundle savedState) { super.onCreate(savedState); onNewIntent(getIntent()); } @Override protected void onNewIntent(Intent intent) { super.onNewIntent(intent); //code } ,如下所示:

onNewIntent()

而且,您的活动已经过了恢复生命周期。如果该行为恢复,他们可以简单地拨打i=0 for f in A/*/; do echo "$((++i)) $f" done 并保持恢复。