cannot start activity background in android 10 [ android Q ]

前端 未结 2 581
没有蜡笔的小新
没有蜡笔的小新 2020-12-06 02:36

I use android 10 [android Q, galaxy 10],

I use android studio 3.3,

using AVD, and made a api 29 [android 10] virtual phone.

at the virtual machine, I

2条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-06 03:13

    I'm open activity using the below logic. as google, blog says if you want to open activity in background service for use notification on android 10 or higher.

    In Manifest:

    
    

    Example:

    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(openScreen(Constants.NOTIFICATION_ID), true)
                        .setAutoCancel(true)
                        .setOngoing(true);
    
                Notification notification = builder.build();
                notificationManager.notify(Constants.NOTIFICATION_ID, notification);
            } else {
                startActivity(new Intent(BackgroundService.this, LoginActivity.class)
                        .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK));
            }
    
        }
    
        private PendingIntent openScreen(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);
        }
    

提交回复
热议问题