kob*_*der 2 android android-notifications
我创建了一个自定义通知,但是当我使用主页按钮暂停活动并且收到通知时,我按下通知,它会创建一个新活动,并且不会恢复预览活动,当我按下后退按钮时,它会转到预览一个是同一个窗口。我已经尝试过singleTop, singleTask, singleIntent,它可以工作,但当消息输入时它不会更新活动,就像预览活动处于暂停状态一样。我该如何修复它?
如果我没有解决方案来恢复活动或销毁暂停的活动或重新启动活动,有没有办法做到这一点?
\n\npublic void CustomNotification(String strtext) {\n // Using RemoteViews to bind custom layouts into Notification\n RemoteViews remoteViews = new RemoteViews(getPackageName(),\n R.layout.customnotification);\n\n // Set Notification Title\n String strtitle = getString(R.string.customnotificationtitle);\n // Open NotificationView Class on Notification Click\n Intent intent = new Intent(this, NotificationView.class);\n // Send data to NotificationView Class\n intent.putExtra("title", strtitle);\n intent.putExtra("text", strtext);\n intent.putExtra("String T", "");\n //intent.putExtra("Integer C", 0);\n\n // Open NotificationView.java Activity\n PendingIntent pIntent = PendingIntent.getActivity(this, 0, intent,\n PendingIntent.FLAG_UPDATE_CURRENT);\n\n NotificationCompat.Builder builder = new NotificationCompat.Builder(this)\n // Set Icon\n .setSmallIcon(R.drawable.ic_launcher)\n // Set Ticker Message\n .setTicker(getString(R.string.customnotificationticker))\n // Dismiss Notification\n .setAutoCancel(true)\n // Set PendingIntent into Notification\n .setContentIntent(pIntent)\n // Set RemoteViews into Notification\n .setContent(remoteViews);\n\n // Locate and set the Image into customnotificationtext.xml ImageViews\n remoteViews.setImageViewResource(R.id.imagenotileft,R.drawable.ic_launcher);\n remoteViews.setImageViewResource(R.id.imagenotiright,R.mipmap.ic_action_chat);\n\n // Locate and set the Text into customnotificationtext.xml TextViews\n remoteViews.setTextViewText(R.id.title,getString(R.string.customnotificationtitle));\n remoteViews.setTextViewText(R.id.text, strtext);\n\n // Create Notification Manager\n NotificationManager notificationmanager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);\n // Build Notification with Notification Manager\n notificationmanager.notify(0, builder.build());\n\n }\nRun Code Online (Sandbox Code Playgroud)\n\n日志:
\n\n04-29 01:29:10.453 29001-29001/com.example.example E/STATE\xef\xb9\x95NEW ACTIVITY\n04-29 01:29:15.376 29501-29501/com.example.example D/Activity\xef\xb9\x95 Activity.onPause(), editTextTapSensorList size: 0\n04-29 01:30:06.981 29501-29501/com.example.example E/STATE\xef\xb9\x95 RESUMING\nRun Code Online (Sandbox Code Playgroud)\n\n当我按下通知时:
\n\n04-29 01:33:09.654 33449-33530/com.example.example E/STATE\xef\xb9\x95NEW ACTIVITY\nRun Code Online (Sandbox Code Playgroud)\n\n我将不胜感激任何答案,谢谢!
\n\n编辑
\n\n回答:
\n\n答案是添加:
\n\nintent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP\n | Intent.FLAG_ACTIVITY_NEW_TASK);\nRun Code Online (Sandbox Code Playgroud)\n\n因为当我在同一个窗口中按下主页按钮时,活动处于暂停状态,然后如果有通知到达,它会将我带到该窗口,但没有更新消息,所以我需要创建一个新意图并删除前一个意图正在暂停。这样代码就完成了工作。
\n要更新您的活动,您应该覆盖onNewIntent():
protected void onNewIntent (Intent intent) {
super.onNewIntent(intent);
//reload your data here
}
Run Code Online (Sandbox Code Playgroud)
void onNewIntent (Intent intent)这是针对
launchMode在其包中设置为“singleTop”的活动调用的,或者如果客户端FLAG_ACTIVITY_SINGLE_TOP在调用时使用了该标志startActivity(Intent)。在任何一种情况下,当活动在活动堆栈顶部重新启动时,onNewIntent()将在现有实例上调用用于重新启动它的 Intent,而不是启动正在启动的活动的新实例。在接收新意图之前,活动将始终暂停,因此您可以指望
onResume()在此方法之后被调用。请注意,
getIntent()仍然返回原始 Intent。您可以使用setIntent(Intent)它来将其更新为这个新的 Intent。
注意:正如文档所说,您需要android:launchMode="singleTop"在 Activity 标签中进行设置(在 AndroidManifest 中)。
| 归档时间: |
|
| 查看次数: |
2284 次 |
| 最近记录: |