有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

无法在安卓 10上从BroadcastReceiver启动活动

昨晚我把我的操作系统版本升级到了安卓10,从那以后,广播接收器内的startActivity功能就什么都没做了。以下是我如何根据Commonware的答案开始活动:

Intent i = new Intent(context, AlarmNotificationActivity.class);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) { // This is at least 安卓 10...

                Log.d("Debug", "This is 安卓 10");
                // Start the alert via full-screen intent.
                PendingIntent startAlarmPendingIntent = PendingIntent.getBroadcast(context, 0, i, PendingIntent.FLAG_UPDATE_CURRENT);
                String CHANNEL_ID = "my_channel_02";
                NotificationChannel channel = new NotificationChannel(CHANNEL_ID,
                        context.getString(R.string.notification_channel_name_second),
                        NotificationManager.IMPORTANCE_HIGH);
                NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
                notificationManager.createNotificationChannel(channel);
                NotificationCompat.Builder builder = new NotificationCompat.Builder(context, CHANNEL_ID)
                        .setContentTitle("Um, hi!")
                        .setAutoCancel(true)
                        .setPriority(NotificationCompat.PRIORITY_HIGH)
                        .setFullScreenIntent(startAlarmPendingIntent, true);
                Log.d("Debug", "Try to load screen");
                notificationManager.notify(0, builder.build());

            }

日志显示我正在使用notify命令,但什么也没发生。我正在申请清单上的使用全屏意图许可,这样我就可以使用全屏意图了。 因为这个问题,我的应用程序现在没用了。有人知道怎么解决吗


共 (2) 个答案

  1. # 1 楼答案

    安卓10对后台活动启动的限制大约在六个月前宣布。你可以在the documentation中阅读更多关于它的信息

    使用高优先级通知,并使用相关的全屏Intent。见the documentationThis sample app通过使用WorkManager触发需要提醒用户的后台事件来演示这一点。在那里,我使用高优先级通知,而不是直接启动活动:

    val pi = PendingIntent.getActivity(
      appContext,
      0,
      Intent(appContext, MainActivity::class.java),
      PendingIntent.FLAG_UPDATE_CURRENT
    )
    
    val builder = NotificationCompat.Builder(appContext, CHANNEL_WHATEVER)
      .setSmallIcon(R.drawable.ic_notification)
      .setContentTitle("Um, hi!")
      .setAutoCancel(true)
      .setPriority(NotificationCompat.PRIORITY_HIGH)
      .setFullScreenIntent(pi, true)
    
    val mgr = appContext.getSystemService(NotificationManager::class.java)
    
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O
      && mgr.getNotificationChannel(CHANNEL_WHATEVER) == null
    ) {
      mgr.createNotificationChannel(
        NotificationChannel(
          CHANNEL_WHATEVER,
          "Whatever",
          NotificationManager.IMPORTANCE_HIGH
        )
      )
    }
    
    mgr.notify(NOTIF_ID, builder.build())
    
  2. # 2 楼答案

    安卓10对后台活动启动的限制大约在六个月前宣布。你可以在documentation中阅读更多关于它的信息

    因此,您需要有一个高级通知,当用户单击该通知时,您的活动将被打开

    public class UIExampleReceiver extends BroadcastReceiver {
    
    public static final String TAG_NOTIFICATION = "NOTIFICATION_MESSAGE";
    public static final String CHANNEL_ID = "channel_1111";
    public static final int NOTIFICATION_ID = 111111;
    private static final String TAG = "Receiver";
    
    @Override
    public void onReceive(Context context, Intent intent) {
    
        try {
    
                // If android 10 or higher
                if (Build.VERSION.SDK_INT > Build.VERSION_CODES.P)
                {
    
                     startActivityNotification(context,NOTIFICATION_ID,context.getResources().getString(R.string.open_app), context.getResources().getString(R.string.click_app));
    
                }
                else
                {
                    // If lower than Android 10, we use the normal method ever.
                    Intent activity = new Intent(context, ExampleActivity.class);
                    activity.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    context.startActivity(activity);
                }
    
        } catch (Exception e)
        {
            Log.d(TAG,e.getMessage()+"");
        }
    }
    
    
     // notification method to support opening activities on Android 10
    public static void startActivityNotification(Context context, int notificationID, 
    String title, String message) {
    
        NotificationManager mNotificationManager =
                (NotificationManager) 
       context.getSystemService(Context.NOTIFICATION_SERVICE);
        //Create GPSNotification builder
        NotificationCompat.Builder mBuilder;
    
        //Initialise ContentIntent
        Intent ContentIntent = new Intent(context, ExampleActivity.class);
        ContentIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | 
        Intent.FLAG_ACTIVITY_CLEAR_TASK);
        PendingIntent ContentPendingIntent = PendingIntent.getActivity(context,
                0,
                ContentIntent,
                PendingIntent.FLAG_UPDATE_CURRENT);
    
        mBuilder = new NotificationCompat.Builder(context)
                .setSmallIcon(R.drawable.ic_launcher)
                .setContentTitle(title)
                .setContentText(message)
                .setColor(context.getResources().getColor(R.color.colorPrimaryDark))
                .setAutoCancel(true)
                .setContentIntent(ContentPendingIntent)
                .setDefaults(Notification.DEFAULT_LIGHTS | Notification.DEFAULT_VIBRATE)
                .setCategory(NotificationCompat.CATEGORY_MESSAGE)
                .setPriority(NotificationCompat.PRIORITY_DEFAULT);
    
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
            NotificationChannel mChannel = new NotificationChannel(CHANNEL_ID,
                    "Activity Opening Notification",
                    NotificationManager.IMPORTANCE_HIGH);
            mChannel.enableLights(true);
            mChannel.enableVibration(true);
            mChannel.setDescription("Activity opening notification");
    
            mBuilder.setChannelId(CHANNEL_ID);
    
       Objects.requireNonNull(mNotificationManager).createNotificationChannel(mChannel);
        }
    
     Objects.requireNonNull(mNotificationManager).notify(TAG_NOTIFICATION,notificationID, 
     mBuilder.build());
        }
    
    }