我是Android的新手.我阅读了Android文档,但我仍需要进一步澄清.谁能告诉我究竟PendingIntent是什么?
我正在使用一个AlarmManager触发广播信号的意图.以下是我的代码:
AlarmManager mgr = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
Intent i = new Intent(this, Wakeup.class);
try
{
PendingIntent pi = PendingIntent.getBroadcast(this, 0, i, 0);
Long elapsed += // sleep time;
mgr.set(AlarmManager.RTC_WAKEUP, elapsed, pi);
}
catch(Exception r)
{
Log.v(TAG, "RunTimeException: " + r);
}
Run Code Online (Sandbox Code Playgroud)
我从一个调用这个代码Activity,所以我不知道如何得到以下错误...
ERROR/AndroidRuntime(7557): java.lang.RuntimeException: Unable to start receiver com.wcc.Wakeup: android.util.AndroidRuntimeException: Calling startActivity() from outside of an Activity context requires the FLAG_ACTIVITY_NEW_TASK flag. Is this really what you want?
Run Code Online (Sandbox Code Playgroud) 我通过以下代码在BroadcastReceiver中创建通知:
String ns = Context.NOTIFICATION_SERVICE;
NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(ns);
int icon = R.drawable.ic_stat_notification;
CharSequence tickerText = "New Notification";
long when = System.currentTimeMillis();
Notification notification = new Notification(icon, tickerText, when);
notification.defaults |= Notification.DEFAULT_VIBRATE;
long[] vibrate = {0,100,200,200,200,200};
notification.vibrate = vibrate;
notification.flags |= Notification.FLAG_AUTO_CANCEL;
CharSequence contentTitle = "Title";
CharSequence contentText = "Text";
Intent notificationIntent = new Intent(context, NotificationActivity.class);
notificationIntent.putExtra(Global.INTENT_EXTRA_FOO_ID, foo_id);
PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0);
notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);
int mynotification_id = 1;
mNotificationManager.notify(mynotification_id, notification);
Run Code Online (Sandbox Code Playgroud)
当我点击通知时,它会打开NotificationActivity,在Activity中我可以从Intent-Bundle中检索foo_id(例如1)
但是,如果触发另一个通知并再次单击它,则活动仍会从Intent-Bundle接收"旧"值(1).我试图用clear()清除包,但收到的效果相同.我觉得我的代码错了......
android android-notifications android-notification-bar android-pendingintent
我的应用程序有通知,这是 - 很明显 - 没有任何标志,每次启动一个新的活动,所以我得到对方的顶部,这是错误的运行多个同样的活动.
我想要它做的是将通知挂起意图中指定的活动带到前面(如果它已在运行),否则启动它.
到目前为止,我所拥有的通知的意图/待定意图是
private static PendingIntent prepareIntent(Context context) {
Intent intent = new Intent(context, MainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
return PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
}
Run Code Online (Sandbox Code Playgroud)
奇怪的是,它有时会起作用,有时它不会......我觉得我已经尝试了每一个标志组合.
android android-intent android-activity android-pendingintent
我MainActicity 开始RefreshService用Intent具有boolean所谓的多余isNextWeek.
我RefreshService做了一个在用户点击它时Notification启动我MainActivity的.
这看起来像这样:
Log.d("Refresh", "RefreshService got: isNextWeek: " + String.valueOf(isNextWeek));
Intent notificationIntent = new Intent(this, MainActivity.class);
notificationIntent.putExtra(MainActivity.IS_NEXT_WEEK, isNextWeek);
Log.d("Refresh", "RefreshService put in Intent: isNextWeek: " + String.valueOf(notificationIntent.getBooleanExtra(MainActivity.IS_NEXT_WEEK,false)));
pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
builder = new NotificationCompat.Builder(this).setContentTitle("Title").setContentText("ContentText").setSmallIcon(R.drawable.ic_notification).setContentIntent(pendingIntent);
notification = builder.build();
// Hide the notification after its selected
notification.flags |= Notification.FLAG_AUTO_CANCEL;
notificationManager.notify(NOTIFICATION_REFRESH, notification);
Run Code Online (Sandbox Code Playgroud)
你可以看到notificationIntent应该有boolean额外IS_NEXT_WEEK的值,isNextWeek其中放入了PendingIntent.
当我现在点击这个时, …
android android-intent android-service android-pendingintent
背景:
我通过AlarmManager使用PendingIntent进行报警.
问题:
起初我认为为了取消之前的那些,我必须提供我之前用来启动警报的确切requestCode.
但后来我发现我错了,因为取消API说:
删除具有匹配Intent的所有警报.任何类型的警报,其Intent与此匹配的警报(由filterEquals(Intent)定义)将被取消.
看一下" filterEquals ",文档说:
为了意图解析(过滤)的目的,确定两个意图是否相同.也就是说,如果他们的行为,数据,类型,类别和类别是相同的.这不会比较意图中包含的任何额外数据.
所以我没有得到"requestCode"的用途......
问题:
什么是"requestCode"用于?
如果我使用相同的"requestCode"创建多个警报怎么办?他们互相覆盖了吗?
protected void displayNotification(String response) {
Intent intent = new Intent(context, testActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, Intent.FLAG_ACTIVITY_NEW_TASK);
Notification notification = new Notification(R.drawable.icon, "Upload Started", System.currentTimeMillis());
notification.setLatestEventInfo(context, "Upload", response, pendingIntent);
nManager.notify((int)System.currentTimeMillis(), notification);
}
Run Code Online (Sandbox Code Playgroud)
该函数将被多次调用.我希望每个人notification在点击时启动testActivity.不幸的是,只有第一个通知启动testActivity.单击其余部分会导致通知窗口最小化.
额外信息:函数displayNotification()在一个叫做的类中UploadManager. 从实例化Context传递进来.函数在函数中多次调用,也在UploadManager中运行.UploadManageractivitydisplayNotification()AsyncTask
编辑1:我忘了提到我正在将String响应传递Intent intent给extra.
protected void displayNotification(String response) {
Intent intent = new Intent(context, testActivity.class);
intent.putExtra("response", response);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
Run Code Online (Sandbox Code Playgroud)
这有很大的不同,因为我需要额外的"响应"来反映创建通知时的字符串响应.相反,使用PendingIntent.FLAG_UPDATE_CURRENT,额外的"响应"反映了最后一次调用时String响应displayNotification().
我知道为什么这是通过阅读文档 …
应用程序在运行时崩溃并出现以下错误:
java.lang.IllegalArgumentException:maa.abc:针对 S+(版本 31 及更高版本)要求在创建 PendingIntent 时指定 FLAG_IMMUTABLE 或 FLAG_MUTABLE 之一。强烈考虑使用 FLAG_IMMUTABLE,仅当某些功能依赖于可变的 PendingIntent 时才使用 FLAG_MUTABLE,例如,如果需要与内联回复或气泡一起使用。在 android.app.PendingIntent.checkFlags(PendingIntent.java:375) 在 android.app.PendingIntent.getBroadcastAsUser(PendingIntent.java:645) 在 android.app.PendingIntent.getBroadcast(PendingIntent.java:632) 在 com.google。 android.exoplayer2.ui.PlayerNotificationManager.createBroadcastIntent(PlayerNotificationManager.java:1373) 在 com.google.android.exoplayer2.ui.PlayerNotificationManager.createPlaybackActions(PlayerNotificationManager.java:1329) 在 com.google.android.exoplayer2.ui.PlayerNotificationManager。 (PlayerNotificationManager.java:643) 在 com.google.android.exoplayer2.ui.PlayerNotificationManager.(PlayerNotificationManager.java:529) 在 com.google.android.exoplayer2.ui.PlayerNotificationManager.createWithNotificationChannel(PlayerNotificationManager.java:456) 在 com .google.android.exoplayer2.ui.PlayerNotificationManager.createWithNotificationChannel(PlayerNotificationManager.java:417)
我尝试了所有可用的解决方案,但应用程序在 Android 12 上仍然崩溃。
@Nullable
@Override
public PendingIntent createCurrentContentIntent(@NonNull Player player) {
Intent intent = new Intent(service, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP |
Intent.FLAG_ACTIVITY_SINGLE_TOP |
Intent.FLAG_ACTIVITY_NEW_TASK);
return PendingIntent.getActivity(service, 0, intent,PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_IMMUTABLE);
}
Run Code Online (Sandbox Code Playgroud) 我有办法在调试时查看PendingIntent设备中活动s 的列表吗?
我开始使用AlarmManager,我想看看我的PendingIntents是否正确创建和删除.
看到其他PendingIntent的东西也很好,就像一个好奇心看一些应用程序是否正在做一些"额外的工作".
任何人都知道我们如何以编程方式从应用程序中删除使用Pending intent调用的通知.
我曾经使用以下方法取消通知.
AlarmManager am=(AlarmManager)getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(Display.this, TwoAlarmService.class);
PendingIntent pi = PendingIntent.getBroadcast(Display.this, AlarmNumber, intent, PendingIntent.FLAG_CANCEL_CURRENT);
am.cancel(pi);
Run Code Online (Sandbox Code Playgroud)
但问题是已经解雇的通知没有从通知栏中删除.
提前致谢...

notifications android android-notifications android-pendingintent