Android 12挂起意图

10

在创建PendingIntent时,要求指定FLAG_IMMUTABLE或FLAG_MUTABLE之一,以针对S+(版本31及以上)进行目标定位。强烈建议使用FLAG_IMMUTABLE,仅在某些功能依赖于PendingIntent可变性时使用FLAG_MUTABLE。

我无法在Android Studio项目代码中更新pending intent标志

这是AlarmPingSender.java中出现错误的位置

  public void start()        
   {       
   String action = MqttServiceConstants.PING_SENDER
            + comms.getClient().getClientId();
    Log.d(TAG, "Register alarmreceiver to MqttService"+ action);
    service.registerReceiver(alarmReceiver, new IntentFilter(action));

    pendingIntent = PendingIntent.getBroadcast(service, 0, new Intent(
            action), PendingIntent.FLAG_UPDATE_CURRENT);

    schedule(comms.getKeepAlive());
    hasStarted = true;
}

帮我解决问题 在Android Studio中出现错误的图片


请有人帮忙。 - Dark_Clouds_369
错误信息告诉你需要做什么。此外,在Stackoverflow上有许多关于这个问题的提问。请参见https://developer.android.com/guide/components/intents-filters#DeclareMutabilityPendingIntent。 - David Wasser
我不确定在代码中应该在哪里更改待处理意图,我们尝试了很多方法和不同的代码位置,但都没有起作用。 - Dark_Clouds_369
4个回答

8

在项目中创建任何PendingIntent时,请使用以下两个公共方法。

创建活动(Activity)的pendingIntent。

   public static PendingIntent createPendingIntentGetActivity(Context context, int id, Intent intent, int flag) {
         if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
             return PendingIntent.getActivity(context, id, intent, PendingIntent.FLAG_IMMUTABLE | flag);
         } else {
             return PendingIntent.getActivity(context, id, intent, flag);
         }
     }

创建广播的待处理意图。
 public static PendingIntent createPendingIntentGetBroadCast(Context context, int id, Intent intent, int flag) {
     if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
         return PendingIntent.getBroadcast(context, id, intent, PendingIntent.FLAG_IMMUTABLE | flag);
     } else {
         return PendingIntent.getBroadcast(context, id, intent, flag);
     }
 }

Kotlin的答案:

fun getActivity(context: Context?, id: Int, intent: Intent?, flag: Int): PendingIntent {
    return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
        PendingIntent.getActivity(context, id, intent, PendingIntent.FLAG_MUTABLE or flag)
    } else {
        PendingIntent.getActivity(context, id, intent, flag)
    }
}

fun getBroadcast(context: Context?, id: Int, intent: Intent?, flag: Int): PendingIntent {
    return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
        PendingIntent.getBroadcast(context, id, intent!!, PendingIntent.FLAG_MUTABLE or flag)
    } else {
        PendingIntent.getBroadcast(context, id, intent!!, flag)
    }
}

1
为什么是版本M?M代表Api 23吗? - Emil
1
我编辑了答案,将skd更改为S+。 - Shahab Saalami
1
@ShahabSaalami 我认为应该只针对FLAG_IMMUTABLE(在api 23中引入)使用M。 - Shweta

6

你需要这样做:

pendingIntent = PendingIntent.getBroadcast(service, 0, new Intent(
        action), PendingIntent.FLAG_UPDATE_CURRENT |
                 PendingIntent.FLAG_IMMUTABLE);

由于您正在使用 AlarmManager,因此您应该能够使用 IMMUTABLE 标志。


先生,我可以知道我们需要在哪里使用这段代码吗?因为我们已经尝试过了,但它对我们没有用。我们在AlarmPingIntent中进行了更改。 - Dark_Clouds_369
1
你看过这个吗?你正在使用相同的MQTT库吗?https://dev59.com/88Pra4cB1Zd3GeqPl7HH - David Wasser
1
那个工单里有一个链接指向另一个版本的库,也许可以解决你的问题。 - David Wasser
2
你能用提供的解决方案解决问题了吗? - David Wasser
3
一般来说,如果您将PendingIntent提供给另一个应用程序,并且该应用程序需要在最终触发PendingIntent时添加/修改Intent,则需要使用FLAG_MUTABLE - David Wasser
显示剩余9条评论

3

我遇到了同样的问题,仅仅为我的PendingIntents添加IMMUTABLE标志位并不起作用。

请确保在app/build.gradle中,您至少有2.7.1版本的WorkManager(如果您也在应用内使用WorkManager):

implementation 'androidx.work:work-runtime:2.7.1'

我之前使用的是1.0.3版本。升级到这个版本(或者最新版本),问题就解决了。


3

如果你的代码中没有使用PendingIntent,但仍然出现了这个错误,那么很可能是你的某个库依赖项在使用它,请将依赖项更新到最新版本(这对我起作用了)。


谢谢,你的回答帮助我解决了问题。 - Yassine BENNKHAY

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接