Android onResume 在 onNewIntent 之后没有被调用

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

我有一个 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_ONFLAG_KEEP_SCREEN_ON

关于Android onResume 在 onNewIntent 之后没有被调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30119557/

相关文章:

android - 为什么在 Android 上,OpenCV 摄像头在捕获视频时比 Android 摄像头更快

android - 设置和显示动态列表

java - 如何通过Intent发送Object的ArrayList?

android - 如何防止我的媒体播放器在我切换 Activity 时停止但在我点击主页时不停止?

android - 同一个包中的两个 Activity 是否属于同一个线程?

android - 如何从 android cordova 插件调用另一个 cordova 插件?

android - Roboguice 辅助标注

Android 白色预览屏幕根据应用程序的启动位置花费不同的时间

android - 使用 fragment 时出现空指针错误

android - Android Activity 的多个实例是否应该显示在内存分析器中?