当屏幕锁定在Android Q中时,如何从广播接收器开始活动 [英] How to start a activity from a broadcast receiver when the screen is locked in Android Q

查看:93
本文介绍了当屏幕锁定在Android Q中时,如何从广播接收器开始活动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用广播接收器在 Android Q 中实现基于警报的应用程序.我正在运行使用通知来触发警报广播接收器的前台服务.该服务运行良好,并且正在触发广播接收器.如果我们在设置警报后关闭应用程序或锁定屏幕,则该服务将在前台运行并显示通知.

I am trying to implement an alarm based application in Android Q using a broadcast receiver. I am running a foreground service using notification for triggering the alarm broadcast receiver. The service is working fine and it is also triggering the broadcast receiver. If we close the application or lock the screen after setting an alarm, the service will be running in the foreground with a notification.

当警报广播被调用时,我试图在锁定屏幕时打开新的活动,以提供停止警报和服务的功能.我尝试禁用键盘锁,打开屏幕,然后从广播接收器中打开活动,但无法成功.

When the alarm broadcast is called I am trying to open a new activity when the screen is locked to provide the functionality to stop the alarm and service. I tried disabling the keyguard, turn on the screen and then opening the activity from the broadcast receiver, but I couldn't succeed.

我尝试使用 WindowManager标志,但已弃用它们,并且在代码中没有任何区别.

I tried using WindowManager flags but they are deprecated and do not make any difference in the code.

WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED
| WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD
| WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON
| WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON

由于我尝试从BroadcastReceiver启动活动,因此我将没有任何活动可以使用 KeyguardManager.requestDismissKeyguard(活动活动,KeyguardDismissCallback回调)

Since I am trying to start an activity from a BroadcastReceiver I won't be having any Activity to use KeyguardManager.requestDismissKeyguard(Activity activity, KeyguardDismissCallback callback)

锁定屏幕以关闭警报时,是否有任何方法可以启动活动.我的实现如下所示,

我还在清单文件中添加了权限.

I also added permissions in the manifest file.

AndroidManifest.xml

<uses-permission android:name="com.android.alarm.permission.SET_ALARM" />
    <uses-permission android:name="android.permission.WAKE_LOCK" />
    <uses-permission android:name="android.permission.DISABLE_KEYGUARD" />

AlarmBroadcastReceiver.class

public class AlarmBroadcastReceiver extends BroadcastReceiver {
    public static MediaPlayer mp;
    public static Vibrator vibrator;

    private boolean isVibrationEnabled = false;

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

        long[] mVibratePattern = new long[]{0, 400, 400, 400, 400, 400, 400, 400};
        final int[] mAmplitudes = new int[]{0, 128, 0, 128, 0, 128, 0, 128};

        isVibrationEnabled = intent.getExtras().getBoolean(LocationAlertService.IS_VIBRATE);

        mp=MediaPlayer.create(context, R.raw.ring1);
        mp.setLooping(true);
        mp.start();

        if(isVibrationEnabled) {
            vibrator = (Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE);

            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                vibrator.vibrate(VibrationEffect.createWaveform(mVibratePattern, mAmplitudes, 0));
            } else {
                //deprecated in API 26
                vibrator.vibrate(mVibratePattern, 3);
            }
        }

        Intent wakeIntent = new Intent(context, WakeUpActivity.class);
        wakeIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(wakeIntent);
    }
}

我知道我想念一些东西.如果有解决我所面临问题的任何建议,我将感到高兴.预先感谢您对我的帮助.

I know that I am missing something. I would be happy if there are any suggestions to overcome the issue which I am facing. Thanks in advance for helping me out.

推荐答案

对于Android Q,如果您的应用未包含以下链接中列出的那些例外情况,则无法从后台自动启动活动.您可以选择仅显示服务通知,然后单击以启动待定意向.

With Android Q, it is impossible to start an activity from background automatically if your app is not includes those exceptions listed in the link below. You can choose just show a service notification, and start pending intent with click.

https://developer.android.com/guide/components/activities/background-starts

使系统正常工作.我认为,最可能的解决方案是在清单文件中添加"SYSTEM_ALERT_WINDOW".并在应用程序首次打开时请求一次用户权限.(用户可以手动授予此权限-(设置-应用程序-您的应用程序-高级-绘制其他应用程序))示例代码以请求权限:

To make the system work. The most possible solution in my view is adding "SYSTEM_ALERT_WINDOW" to manifest file. And ask for user permission once when the app opened first time.(The user can give this permission manually - (Settings-Apps-Your App-Advanced- Draw over other apps)) Example code to request permission :

在清单中

    <uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>

应用中的某处:

 private void RequestPermission() {
            // Check if Android M or higher
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                // Show alert dialog to the user saying a separate permission is needed
                // Launch the settings activity if the user prefers
                Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION,
                        Uri.parse("package:" + getActivity().getPackageName()));
                startActivityForResult(intent, ACTION_MANAGE_OVERLAY_PERMISSION_REQUEST_CODE);
            }
        }

    @Override
    public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (requestCode == ACTION_MANAGE_OVERLAY_PERMISSION_REQUEST_CODE) {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                if (!Settings.canDrawOverlays(getContext())) {
                    PermissionDenied();
                }
                else
                {
                 //Permission Granted-System will work
            }

        }
    }

这篇关于当屏幕锁定在Android Q中时,如何从广播接收器开始活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆