无法在android 10 [android Q]中启动活动背景

问题描述 投票:0回答:2

我使用android 10 [android Q,银河10],

我使用android studio 3.3,

使用AVD,并制作了api 29 [android 10]虚拟电话。

在虚拟机上,我执行我的应用程序,然后启动其他应用程序,如日历,计算器。因此我的应用活动进入了后台模式。

当我在BroadcastReceiver收到消息时。我叫startActivity。

这里是代码->公共类myReceiver扩展了BroadcastReceiver {}

public void onReceive(Context context, Intent intent)
{
        Intent intentRun = new Intent(context, LoginSuccess.class);
        intentRun.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_CLEAR_TASK);
        context.startActivity(intentRun);
}

但是未显示LoginSuccess活动。[当我的应用程序处于后台模式时]

使用相同的代码,LoginSuccess活动显示得很好当我的应用程序处于前台模式时。

call stack capture image

上图显示了调用堆栈在我在广播接收器中调用startActivity之前。

我已阅读android 10后台活动问题的指南。[developer.android.com/~~一些位置]

在指导线,

我知道如果活动存在于调用堆栈中,则可以将其启动即使在后台模式下。

以上代码,它尝试启动最近调用堆栈中存在的活动。

为什么startActivity调用在后台模式下失败?[可能不会失败,但无论如何都不会激活到前台]

android avd
2个回答
0
投票

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

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

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

在清单中:

    <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
            }

        }
    }

0
投票

我正在使用以下逻辑进行公开活动。与Google一样,博客表示您是否要在后台服务中打开活动以在Android 10或更高版本上使用通知。

在清单中:

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

示例:

private void startActivity() {

        Uri sound = Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.siren);

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
            NotificationManager notificationManager =
                    (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

            AudioAttributes attributes = new AudioAttributes.Builder()
                    .setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
                    .setUsage(AudioAttributes.USAGE_ALARM)
                    .build();

            String CHANNEL_ID = BuildConfig.APPLICATION_ID.concat("_notification_id");
            String CHANNEL_NAME = BuildConfig.APPLICATION_ID.concat("_notification_name");
            assert notificationManager != null;

            NotificationChannel mChannel = notificationManager.getNotificationChannel(CHANNEL_ID);
            if (mChannel == null) {
                mChannel = new NotificationChannel(CHANNEL_ID, CHANNEL_NAME, NotificationManager.IMPORTANCE_HIGH);
                mChannel.setSound(sound, attributes);
                notificationManager.createNotificationChannel(mChannel);
            }

            NotificationCompat.Builder builder = new NotificationCompat.Builder(this, CHANNEL_ID);

            builder.setSmallIcon(R.drawable.logo)
                    .setContentTitle(getString(R.string.app_name))
                    .setContentText(getString(R.string.login))
                    .setPriority(NotificationCompat.PRIORITY_HIGH)
                    .setCategory(NotificationCompat.CATEGORY_CALL)
                    .setFullScreenIntent(openParkingViolationScreen(Constants.NOTIFICATION_ID), true)
                    .setAutoCancel(true)
                    .setOngoing(true);

            Notification notification = builder.build();
            notificationManager.notify(Constants.NOTIFICATION_ID, notification);
        } else {
            startActivity(new Intent(ParkingService.this, LoginActivity.class)
                    .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK));
        }

    }

    private PendingIntent openParkingViolationScreen(int notificationId) {
        Intent fullScreenIntent = new Intent(this, LoginActivity.class);
        fullScreenIntent.putExtra(Constants.NOTIFICATION_IDS, notificationId);
        return PendingIntent.getActivity(this, 0, fullScreenIntent, PendingIntent.FLAG_UPDATE_CURRENT);
    }
© www.soinside.com 2019 - 2024. All rights reserved.