[英]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 Flags
, PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_MUTABLE
PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_MUTABLE
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.