Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/209.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 安卓:屏幕锁定时播放_Android_Android Broadcast - Fatal编程技术网

Android 安卓:屏幕锁定时播放

Android 安卓:屏幕锁定时播放,android,android-broadcast,Android,Android Broadcast,我想在android手机屏幕锁定出现时运行一个方法 我在上测试了作为广播的ACTION\u SCREEN\u,但它只在活动现场时起作用 我还测试了ACTION\u USER\u PRESENT,它在手机解锁时工作,但我想在解锁前运行该方法(就在屏幕锁定出现时) 我还测试了AlarmManager,每1分钟重复一次报警,但此解决方案有两个缺陷: 电池很快就空了 这是一种不推荐的方法,我不需要每1分钟做一次 我该怎么办?您可以创建一个服务,在广播中收听动作屏幕 以下是供您参考的示例代码: publi

我想在android手机屏幕锁定出现时运行一个方法

我在上测试了作为广播的
ACTION\u SCREEN\u,但它只在活动现场时起作用

我还测试了
ACTION\u USER\u PRESENT
,它在手机解锁时工作,但我想在解锁前运行该方法(就在屏幕锁定出现时)

我还测试了
AlarmManager
,每1分钟重复一次报警,但此解决方案有两个缺陷:

  • 电池很快就空了
  • 这是一种不推荐的方法,我不需要每1分钟做一次

  • 我该怎么办?

    您可以创建一个服务,在广播中收听动作屏幕

    以下是供您参考的示例代码:

    public class LockScreenService extends Service {
    
        private BroadcastReceiver mScreenStateReceiver = new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                switch (intent.getAction()) {
                    case Intent.ACTION_SCREEN_ON:
                        // do your stuff
                        break;
                }
            }
        };
    
        public LockScreenService () {}
    
        @Override
        public void onCreate() {
            super.onCreate();
    
            // register ACTION_SCREEN_ON receiver
            IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_ON);
            registerReceiver(mScreenStateReceiver, filter);
        }
    
        @Override
        public int onStartCommand(Intent intent, int flags, int startId) {
            // do some extra things
            return START_STICKY;
        }
    
        @Override
        public IBinder onBind(Intent intent) {
            // do not bind service to activity because service will end if activity is destroyed
            return null;
        }
    
        @Override
        public void onDestroy() {
            // release receiver
            unregisterReceiver(mScreenStateReceiver);
        }
    }
    
    完整答案:

    使用
    service
    并将其设置为
    START\u STICKY
    。 这会导致在终止服务后,服务将再次重新启动。 这是我的代码:

    android manifest : 
    <application 
    ....
        <service android:name=".UpdateService" />
    
     </application>
    
    接收器类别:

    import android.content.BroadcastReceiver;
    import android.content.Context;
    import android.content.Intent;
    import android.util.Log;
    
    
    public class MyReceiver extends BroadcastReceiver {
    
        private boolean screenOff;
    
    
        @Override
        public void onReceive(Context context, Intent intent) {
    
    
            if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {
                screenOff = true;
                Log.i("screenLog", "screen off");
    
            } else if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) {
                screenOff = false;
                Log.i("screenLog", "screen on");
            }
    
    
        }
    }
    
    在StartupActivity中:

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    
    
            Context context = getApplicationContext();
            Intent service = new Intent(context, UpdateService.class);
            context.startService(service);
    }
    

    你是否勾选了“动作屏幕关闭”和“动作屏幕打开”,我认为“动作屏幕关闭”是你正在寻找的是的,我勾选了它们,它们都只有在活动处于活动状态时才起作用。即使活动已死亡,它们也起作用吗?我在屏幕上测试了广播动作。但是当活动终止时,它就不起作用了。是的,如果您正确地实现了服务,即使您的活动终止,您也会继续收到操作屏幕。请参考Android开发者文档,了解更多关于这项服务的信息。非常感谢。它成功了。重要的一点是“return START_STICKY;”即杀死后重新启动服务。@daashet如果您觉得这有帮助,请投票表决我的答案。
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    
    
            Context context = getApplicationContext();
            Intent service = new Intent(context, UpdateService.class);
            context.startService(service);
    }