【问题标题】:Calling startActivity from service doesn't allow to switch the screen off从服务调用 startActivity 不允许关闭屏幕
【发布时间】:2014-12-30 20:34:11
【问题描述】:

您好 Stackoverflow 用户,

我正在尝试使用以下代码为这种情况打开新活动if(intent.getAction().equals(Intent.ACTION_SCREEN_ON))(该服务具有广播接收器)

    Intent intent = new Intent(context, OverlayActivity.class);
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

问题是当新活动打开时,屏幕超时设置无法像以前那样工作并且屏幕始终亮着。活动中没有标志 FLAG_KEEP_SCREEN_ON。活动只有空的 onCreate() 方法。我不知道是什么问题。为什么屏幕超时(15 秒)后屏幕不关闭?当我在没有此活动的情况下运行服务时,它会像往常一样禁用屏幕。

【问题讨论】:

  • 您正在检查哪个 android 版本?
  • Android 4.3 版(Galaxy S3 和 Nexus 4)
  • 除非您正在执行其他操作,例如持有电源锁或在其中一个视图上的活动布局中使用 android:keepScreenOn="true",否则不应这样做
  • 没有类似 android:keepScreenOn="true" 的任何属性。关于 WakeLock,我什至已经评论了所有这部分。但是,一旦它在 KeyGuard(KeyGuard 通过触摸屏禁用用户)后打开活动,超时就消失了。不评论释放唤醒锁也无济于事。

标签: android android-activity android-service


【解决方案1】:

对于 Intent.ACTION_SCREEN_OFF 和 Intent.ACTION_SCREEN_ON,您不能在 Android 清单中声明它们!

可能是你犯了这个错误。

检查此代码 -

public class ScreenReceiver extends BroadcastReceiver {

    public static boolean wasScreenOn = true;

    @Override
    public void onReceive(final Context context, final Intent intent) {
        if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {
            // do whatever you need to do here
            wasScreenOn = false;
        } else if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) {
            // and do whatever you need to do here
            wasScreenOn = true;
        }
    }

}

在活动中 -

public class ExampleActivity extends Activity {

    private BroadcastReceiver mReceiver = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // initialize receiver
        final IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_ON);
        filter.addAction(Intent.ACTION_SCREEN_OFF);
        mReceiver = new ScreenReceiver();
        registerReceiver(mReceiver, filter);
        // your code
    }

    @Override
    protected void onPause() {
        // when the screen is about to turn off
        if (ScreenReceiver.wasScreenOn) {
            // this is the case when onPause() is called by the system due to a screen state change
            Log.e("MYAPP", "SCREEN TURNED OFF");
        } else {
            // this is when onPause() is called when the screen state has not changed
        }
        super.onPause();
    }

    @Override
    protected void onResume() {
        super.onResume();
        // only when screen turns on
        if (!ScreenReceiver.wasScreenOn) {
            // this is when onResume() is called due to a screen state change
            Log.e("MYAPP", "SCREEN TURNED ON");
        } else {
            // this is when onResume() is called when the screen state has not changed
        }
    }

    @Override
    protected void onDestroy() {
        if (mReceiver != null) {
            unregisterReceiver(mReceiver);
            mReceiver = null;
        }
        super.onDestroy();
    }

}

这或许可以解决您的问题。

干杯:)

【讨论】:

  • 抱歉,我没有得到我不能申报的东西?
  • 这些是状态,只允许在您的活动运行时捕获它们。尝试从 Manifest 中删除它们,看看这是否适合您。
  • 对不起,我可能写了一些不清楚的地方。事件的屏幕和事件被广播接收器捕获
  • 您的回答告诉我们如何为该事件注册服务。就我而言,它有效。当服务运行并且之后我不打开新活动时,屏幕超时工作正常。但是一旦我从服务中启动Activity,它就不再起作用了。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-05-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多