简体   繁体   English

Android-广播接收器屏幕关闭,无法显示意图吗?

[英]Android - Broadcast Receiver screen off to Intent, not working…?

I have code below that I have inserted into my onCreate method, but it dosent seem to work. 我下面有插入我的onCreate方法中的代码,但是它似乎起作用。 I want it to launch other activity when screen goes off/battery button press. 我希望它在屏幕关闭/按下电池按钮时启动其他活动。

CODE: 码:

protected void onStartCommand() {
    // TODO Auto-generated method stub
    super.onStart();
    ScreenReceiver();
}

 public class ScreenReceiver extends BroadcastReceiver {

      @Override
        public void onReceive(Context context, Intent intent) {

            if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {

                lock.reenableKeyguard();        
                Intent s = new Intent(Activity1.this , Activity2.class); 
                startActivity(s);


            } 
            else if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) {
                lock.disableKeyguard(); 



            }
        }
    }

Any help would be VERY appreciated? 任何帮助将不胜感激?

You need to acquire a WakeLock before you try starting that Activity. 在尝试启动该活动之前,您需要获取WakeLock

Refer to CommonsWare answer here android AlarmManager not waking phone up 请参阅此处的CommonsWare答案android AlarmManager不唤醒电话

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM