【问题标题】:Android make the application autorun after unlocking the screen even if the application is not running即使应用程序没有运行,Android也会在解锁屏幕后自动运行应用程序
【发布时间】:2019-11-14 08:12:42
【问题描述】:

我正在尝试创建一个自动运行服务:以便应用程序在每次解锁屏幕后、输入图形密钥或密码(如果存在)后启动(在 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");
    }
}

【问题讨论】:

    标签: android service broadcastreceiver screen screen-off


    【解决方案1】:

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

    你为什么不做以下事情:

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

    【讨论】:

    • ACTION_SCREEN_OFF 和 USER_PRESENT 但它在应用程序在堆栈上(运行)时工作,我希望它始终启动。永远开始! - 对我来说是个大问题。
    • 您是否尝试按照我上面的建议来实现它?如果这不起作用,请检查广播接收器是否每次都正确注册。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-23
    • 1970-01-01
    相关资源
    最近更新 更多