0

我正在尝试创建一个自动运行服务:以便应用程序在每次解锁屏幕后、输入图形密钥或密码(如果存在)后启动(在 Android 7、8、9、10 上)。我通过 borocast 接收器 (ACTION_SCREEN_OFF) 动态编写了代码,但它在应用程序在堆栈上(运行)时工作,我希望它始终启动。通过在 android 9 中的清单中注册的方法已经不适用于侦听器。如何实施?

public class WordsBase extends AppCompatActivity {
    ScreenReceiver resiverStart;

 @Override
 protected void onPause() {
        super.onPause();

        resiverStart= new ScreenReceiver();
        IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_OFF);
        registerReceiver(resiverStart,filter);
    }
}
public class ScreenReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {
            Intent intent1 = new Intent(context, WordsBase.class);
            intent1.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            context.startActivity(intent1);
        }
        throw new UnsupportedOperationException("Not yet implemented");
    }
}
4

1 回答 1

0

我了解您想要执行以下操作:如果用户解锁设备,您想要启动您的应用程序。

为什么不执行以下操作:

  1. 使用USER_PRESENT接收器 ( android.intent.action.USER_PRESENT)。请注意,您必须明确注册到此接收器,仅在清单中注册它是不够的
  2. 如果触发了相应的广播,请启动您的应用程序并确保您仍然注册到广播(以便在用户下次解锁设备时再次启动您的应用程序)。
于 2019-11-14T10:56:51.683 回答