面向 S+(版本 31 及更高版本)要求在创建 PendingIntent 时指定 FLAG_IMMUTABLE 或 FLAG_MUTABLE 之一。我在将目标 SDK 更新到 31 后得到它。错误总是出现在 AlarmPingSender 之后。但我不知道任何使用 AlarmPingSender 的类。
2021-10-31 10:43:04.990 17031-17341/com.app.mobile D/AlarmPingSender: Register alarmreceiver to MqttServiceMqttService.pingSender.com.app.mobile-2e24ccbde048f2e91635651784
2021-10-31 10:43:04.993 17031-17341/com.app.mobile E/AndroidRuntime: FATAL EXCEPTION: MQTT Rec: com.app.mobile-2e24ccbde048f2e91635651784
Process: com.app.mobile, PID: 17031
java.lang.IllegalArgumentException: com.app.mobile: Targeting S+ (version 31 and above) requires that one of FLAG_IMMUTABLE or FLAG_MUTABLE be specified when creating a PendingIntent.
Strongly consider using FLAG_IMMUTABLE, only use FLAG_MUTABLE if some functionality depends on the PendingIntent being mutable, e.g. if it …Run Code Online (Sandbox Code Playgroud) android kotlin android-pendingintent android-workmanager android-12
我试图在我的意图中添加一条额外的消息,以便传递给稍后触发的AlarmManager.我的onReceive正确触发,但extras.getString()返回null
建立:
public PendingIntent getPendingIntent(int uniqueRequestCode, String extra) {
Intent intent = new Intent(this, ActionReceiver.class);
intent.putExtra("EXTRA", extra);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, uniqueRequestCode,
intent, 0);
return pendingIntent;
}
public void setSilentLater(TimeRule timeRule) {
boolean[] weekdays = timeRule.getReoccurringWeekdays();
int dayOfWeek = 0;
for (boolean day : weekdays) {
dayOfWeek++;
if (day == true) {
Calendar cal = Calendar.getInstance();
AlarmManager alarmManager = (AlarmManager) this
.getSystemService(Context.ALARM_SERVICE);
cal.set(Calendar.DAY_OF_WEEK, dayOfWeek);
cal.set(Calendar.HOUR_OF_DAY,
timeRule.getStartTime().get(Calendar.HOUR_OF_DAY));
cal.set(Calendar.MINUTE,
timeRule.getStartTime().get(Calendar.MINUTE));
cal.set(Calendar.SECOND, 0);
cal.set(Calendar.MILLISECOND, 0);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,
cal.getTimeInMillis(), 3600000 * 7, getPendingIntent(0, "RINGER_OFF"));
} …Run Code Online (Sandbox Code Playgroud) android broadcastreceiver alarmmanager android-pendingintent android-bundle
所以我让我的AccessibilityService使用以下代码:
@Override
public void onAccessibilityEvent(AccessibilityEvent event) {
if (event.getEventType() == AccessibilityEvent.TYPE_NOTIFICATION_STATE_CHANGED) {
List<CharSequence> notificationList = event.getText();
for (int i = 0; i < notificationList.size(); i++) {
Toast.makeText(this.getApplicationContext(), notificationList.get(i), 1).show();
}
}
}
Run Code Online (Sandbox Code Playgroud)
它可以很好地读出创建通知时显示的文本(1).

唯一的问题是,我还需要在用户打开通知栏时显示的值(3).(2)对我来说并不重要,但知道如何阅读它会很好.您可能知道,所有值都可能不同.

那么,我怎么读出来(3)?我怀疑这是不可能的,但我notificationList似乎只有一个条目(至少只显示一个吐司).
非常感谢!
/ edit:我可以使用提取通知包裹
if (!(parcel instanceof Notification)) {
return;
}
final Notification notification = (Notification) parcel;
Run Code Online (Sandbox Code Playgroud)
但是,我不知道如何从/ notification或中提取通知的消息.notification.contentViewnotification.contentIntent
有任何想法吗?
/编辑:澄清这里的问题:我如何读出(3)?
我有一个从通知栏打开的活动,但是当我这样做时NotificationManager.notify(...),我将意图赋予不同的捆绑,以便每个通知打开相同的活动,但从DB获取彼此的其他信息.
但是当我尝试输入任何通知时(例如有3个通知),它们都会将我发送到与最后一个相同的捆绑的活动.尝试使用一些Flags之后,我真的不知道问题出在哪里(一些标志使通知进入第一个包的活动).
我按照他们在教程中使用它的方式.
android android-intent notificationmanager android-pendingintent
我正在尝试使用两种不同的方法创建和删除警报,这两种方法都在应用程序逻辑的不同时刻调用.
但是,当我调用AlarmManager的cancel()方法时,不会删除警报.
这是我的addAlarm()方法:
AlarmManager alarmManager = (AlarmManager) mContext.getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(PROX_ALERT_INTENT);
intent.putExtra("ALERT_TIME", alert.date);
intent.putExtra("ID_ALERT", alert.idAlert);
intent.putExtra("TITLE", alert.title);
intent.putExtra("GEO_LOC", alert.isGeoLoc);
PendingIntent pendingIntent = PendingIntent.getBroadcast(mContext,
alert.idAlert, intent, PendingIntent.FLAG_CANCEL_CURRENT);
Calendar calendar = Calendar.getInstance();
calendar.setTime(alert.date);
alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);
Log.e("ADD ALERT - WithoutGeoLoc - ",alert.toString());
Run Code Online (Sandbox Code Playgroud)
这是我的deleteAlarm()方法:
AlarmManager alarmManager = (AlarmManager) mContext.getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(PROX_ALERT_INTENT);
intent.putExtra("ALERT_TIME", alert.date);
intent.putExtra("ID_ALERT", alert.idAlert);
intent.putExtra("TITLE", alert.title);
intent.putExtra("GEO_LOC", alert.isGeoLoc);
PendingIntent pendingIntent = PendingIntent.getBroadcast(mContext,
alert.idAlert, intent, PendingIntent.FLAG_CANCEL_CURRENT);
alarmManager.cancel(pendingIntent);
Log.e(TAG,"REMOVE ALERT - without GeoLoc"+alert.toString());
Run Code Online (Sandbox Code Playgroud)
这是我的Logcat:
01-23 …Run Code Online (Sandbox Code Playgroud) 我试图从通知发送信息到被调用的活动,而从我的活动我得到null.
通知代码是:
private void showNotification() {
Intent resultIntent = new Intent(this, MainActivity.class);
if (D)
Log.d(TAG, "Id: " + Id);
resultIntent.putExtra("ineedid", deviceId);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
stackBuilder.addParentStack(MeterActivity.class);
stackBuilder.addNextIntent(resultIntent);
PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0,
PendingIntent.FLAG_UPDATE_CURRENT);
// Bundle tmp = resultIntent.getExtras();
// if (tmp == null) {
// Log.d(TAG, "tmp bundle is null");
// } else {
// long id = tmp.getLong("ineedid", -1);
// Log.d(TAG, "tmp id : " + id);
// }
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
BLEMessengerService.this)
.setSmallIcon(R.drawable.ic_action_search)
.setContentTitle("Event tracker")
.setContentText("Events received").setOngoing(true) …Run Code Online (Sandbox Code Playgroud) android bundle android-intent android-notifications android-pendingintent
下面是我的代码块,它应该NotificationActivity在点击通知时打开.但它不起作用.
private void setNotification(String notificationMessage) {
Uri alarmSound = getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
mNotificationManager = getApplication().getSystemService(Context.NOTIFICATION_SERVICE);
Intent notificationIntent = new Intent(getApplicationContext(), NotificationActivity2.class);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0,notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(getApplicationContext())
.setSmallIcon(R.drawable.logo)
.setContentTitle("My Notification")
.setStyle(new NotificationCompat.BigTextStyle()
.bigText(notificationMessage))
.setContentText(notificationMessage).setAutoCancel(true);
mBuilder.setSound(alarmSound);
mBuilder.setContentIntent(contentIntent);
mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
}
Run Code Online (Sandbox Code Playgroud) 我在通知栏中按通知时试图打开一个片段.我的app结构是:
从菜单打开的一些片段
b.setOnClickListener(new OnClickListener() {
@SuppressWarnings({ "deprecation", "static-access" })
public void onClick(View v) {
w_nm=(NotificationManager) getActivity().getSystemService(getActivity().NOTIFICATION_SERVICE);
Notification notify=new Notification(R.drawable.notnificationlogo,waternoti,System.currentTimeMillis());
Intent notificationIntent = new Intent(getActivity(), Abc.class);
PendingIntent pending=PendingIntent.getActivity(getActivity(), 0,notificationIntent, 0);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
| Intent.FLAG_ACTIVITY_SINGLE_TOP );
notify.flags = Notification.DEFAULT_LIGHTS | Notification.FLAG_AUTO_CANCEL;
notify.setLatestEventInfo(getActivity(),waternoti,waternoti1, pending);
w_nm.notify(0, notify);
Run Code Online (Sandbox Code Playgroud)谁能告诉我如何链接下一个片段页面(当前代码在扩展片段的类中)
android android-notifications android-fragments android-pendingintent
所以我正在研究一个简单的音乐播放器.音乐播放器命名状态,可以播放歌曲,暂停播放,转发到下一首歌曲,返回上一首歌曲,并完全停止播放.播放歌曲时,会显示艺术家姓名和歌曲名称的通知; 此通知还有三个按钮(操作):停止,暂停下一个.
我遇到的问题是确保在单击任一操作时,触发与Action相关的播放控件,而且我完全不知道如何执行此操作.我搜索了Android通知:http://developer.android.com/guide/topics/ui/notifiers/notifications.html但它没有澄清,也没有提供太多关于通知操作的信息.
这是一个简单的操作(例如,应该与"下一步"按钮相关联点击通知:注意:下面描述的所有代码都是在以下包中编写的:com.deadpixels.light.player并且调用了Activity :PlayerActivity
public void nextSong() {
if (position == songs.size() -1) {
Toast.makeText(this, "No more songs on queue", Toast.LENGTH_SHORT).show();
if(mPlayer.isPlaying()) {
return;
}
else {
buttonPlay.setImageResource(R.drawable.button_play);
}
return;
}
else {
position++;
playSong(songs.get(position));
}
}
Run Code Online (Sandbox Code Playgroud)
这是我试图做的:
Intent nextIntent = new Intent(KEY_NEXT);
PendingIntent nextPendingIntent = createPendingResult(0, nextIntent, 0);
Run Code Online (Sandbox Code Playgroud)
NextIntent的操作是:
public static final String KEY_NEXT = "com.deadpixels.light.player.PlayerActivity.nextSong";
Run Code Online (Sandbox Code Playgroud)
然后我通过addAction()将其添加到通知中:
mBuilder.addAction(R.drawable.not_action_next, "Next", nextPendingIntent);
Run Code Online (Sandbox Code Playgroud)
你们知道我还能尝试什么吗?使用我上面解释的内容什么都没做,通知显示,并有三个操作按钮,但点击它们对我没有任何作用.
有一次,我想可能是我添加了带有动作名称的intent过滤器,但后来我想,好吧,它们都在同一名称空间中,我为什么要这样做?
android mediaplayback android-notifications android-pendingintent
我向用户发送推送通知,点击它时会打开应用程序.
我的问题是,当应用程序已经打开时,单击通知会再次启动应用程序.
我只希望它启动应用程序,如果它还没有运行.
我在通知中使用Pending Intent:
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, new Intent(this, Splash.class), 0);
Run Code Online (Sandbox Code Playgroud)
我看到帖子说使用:
<activity
android:name=".Splash"
android:launchMode="singleTask"
Run Code Online (Sandbox Code Playgroud)
但问题是我正在运行的应用程序正在运行其他活动,然后启动应用程序启动后7秒内完成,因此当应用程序运行时,Splash不是当前活动
notifications android push-notification android-pendingintent