简体   繁体   中英

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.

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) {

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.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_MUTABLE

example

in your code change in PendingIntent.getBroadcast()

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

also change in PendingIntent.getActivity()

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM