简体   繁体   English

Pending Intent > Flag_Immutable Android 12 仍然出现错误

[英]Pending Intent > Flag_Immutable Android 12 Still getting errors

Here is my Code For the Alarm to be set这是我设置警报的代码

public void setAlarm(int hr ,int min) {
    Calendar calendar = Calendar.getInstance();
    calendar.set(Calendar.HOUR_OF_DAY, hr);
    calendar.set(Calendar.MINUTE, min);
    calendar.set(Calendar.SECOND, 0);
    calendar.set(Calendar.MILLISECOND, 0);

    Calendar cur = Calendar.getInstance();

    if (cur.after(calendar)) {
        calendar.add(Calendar.DATE, 1);
    }
    Intent myIntent = new Intent(mCon, DailyReminder.class);
    int ALARM1_ID = 10000;


    PendingIntent pendingIntent;
    
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
       pendingIntent= PendingIntent.getBroadcast(mCon, ALARM1_ID, myIntent, PendingIntent.FLAG_IMMUTABLE | PendingIntent.FLAG_UPDATE_CURRENT);
    } else {
        pendingIntent = PendingIntent.getBroadcast(mCon, ALARM1_ID, myIntent, PendingIntent.FLAG_UPDATE_CURRENT);
    }

    AlarmManager alarmManager = (AlarmManager) mCon.getSystemService(Context.ALARM_SERVICE);
    alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pendingIntent);

}

I did the changes mentioned in this Stackoverfolw question to the above code.我对上面的代码做了这个Stackoverfolw 问题中提到的更改。

Here is the Broadcast Receiver I used in this这是我在此使用的广播接收器

public class DailyReminder extends BroadcastReceiver {


@Override
public void onReceive(Context mCon, Intent intent) {
   BirthdayNotification(mCon,intent);
}

private void BirthdayNotification(Context mCon, Intent intent) {私人无效生日通知(上下文mCon,意图){

Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);

    NotificationCompat.Builder mBuilder =new NotificationCompat.Builder(mCon.getApplicationContext(), "Birthday Notify");
    Intent i2 = new Intent(mCon.getApplicationContext(), MainActivity.class);
    PendingIntent pendingIntent2 = PendingIntent.getActivity(mCon, 2, i2, 0);

    DateData dateData=new DateData().getTimeNow();
    ArrayList<DoctorData> Birthday_List=new DataLoader(mCon).getFilteredDoctorData(dateData);

    if(Birthday_List.size()>0){
        //calls planned
        NotificationCompat.InboxStyle inboxStyle = new NotificationCompat.InboxStyle();
        inboxStyle.setBigContentTitle("Birthday's");
        for(DoctorData item :Birthday_List){
            inboxStyle.addLine(item.getName()+"-"+item.getLocation());
        }
        inboxStyle.setSummaryText("Total Birthday's :"+Birthday_List.size());
        mBuilder.setStyle(inboxStyle);

        mBuilder.setContentIntent(pendingIntent2);
        mBuilder.setSmallIcon(R.mipmap.ic_launcher_round);
        mBuilder.setContentTitle("Doctor Birthday's today");
        mBuilder.setSound(alarmSound);
        mBuilder.setContentText("See birthday's");
        mBuilder.setPriority(Notification.PRIORITY_MAX);


        NotificationManager mNotificationManager =
                (NotificationManager) mCon.getSystemService(Context.NOTIFICATION_SERVICE);

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
        {
            String channelId = "Birthday Channel";
            NotificationChannel channel = new NotificationChannel(
                    channelId,
                    "Birthday Notification",
                    NotificationManager.IMPORTANCE_HIGH);
            mNotificationManager.createNotificationChannel(channel);
            mBuilder.setChannelId(channelId);
        }

        mNotificationManager.notify(2, mBuilder.build());

    }

}

} }

When I run the above code, the following error shows up again.当我运行上面的代码时,再次出现以下错误。 can anyone help on this?有人可以帮忙吗?

在此处输入图像描述

Thank you谢谢

There is little bit mistake,有一点小错误,

Use both PendingIntent Flags like this example, PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_MUTABLE像这个例子一样使用两个PendingIntent FlagsPendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_MUTABLE PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_MUTABLE

example 例子

in your code change in PendingIntent.getBroadcast()PendingIntent.getBroadcast()的代码更改中

pendingIntent= PendingIntent.getBroadcast(mCon, ALARM1_ID, myIntent, PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_MUTABLE);

also change in PendingIntent.getActivity() PendingIntent.getActivity()也发生了变化

PendingIntent pendingIntent2 = PendingIntent.getActivity(mCon, 2, i2, PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_MUTABLE);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Android paho mqtt 崩溃 Android 12 - 定位 S+(版本 31 及更高版本)需要 FLAG_IMMUTABLE 或 FLAG_MUTABLE 之一 - Android paho mqtt crashes Android 12 - Targeting S+ (version 31 and above) requires that one of FLAG_IMMUTABLE or FLAG_MUTABLE 如何更正第三方库中 android 12 上缺少的待处理意图? - How to correct missing pending intent on android 12 in a third party library? 针对 31+ SDK 需要在创建 PendingIntent 时指定 FLAG_IMMUTABLE 或 FLAG_MUTABLE 之一吗? - Targeting 31+ SDK requires that one of FLAG_IMMUTABLE or FLAG_MUTABLE be specified when creating a PendingIntent? 针对 S+(版本 31 及以上)需要指定 FLAG_IMMUTABLE 或 FLAG_MUTABLE 之一 - Targeting S+ (version 31 and above) requires that one of FLAG_IMMUTABLE or FLAG_MUTABLE be specified 定位 S+(版本 33)需要指定 FLAG_IMMUTABLE 或 FLAG_MUTABLE 之一 - Targeting S+ (version 33 ) requires that one of FLAG_IMMUTABLE or FLAG_MUTABLE be specified 针对 S+(版本 31 及以上)需要 FLAG_IMMUTABLE 或 FLAG_MUTABLE 之一 - Targeting S+ (version 31 and above) requires that one of FLAG_IMMUTABLE or FLAG_MUTABLE MediaSessionCompat:Targeting S+(版本 31 及以上)要求在创建 PendingIntent 时指定 FLAG_IMMUTABLE 或 FLAG_MUTABLE 之一 - MediaSessionCompat:Targeting S+ (version 31 and above) requires that one of FLAG_IMMUTABLE or FLAG_MUTABLE be specified when creating a PendingIntent MediaSessionCompat:定位 S+(版本 31 及更高版本)需要音乐通知中的 FLAG_IMMUTABLE 或 FLAG_MUTABLE 之一 - MediaSessionCompat: Targeting S+ (version 31 and above) requires that one of FLAG_IMMUTABLE or FLAG_MUTABLE in music notification 通知中的待处理意图不适用于 Android - Pending Intent in Notification not working Android Android待处理的Intent行为和系统重启? - Behavior of Android pending Intent and system restart?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM