我已经创建了一个自定义通知,但是当我使用主页按钮暂停活动并且通知到达时,我按下通知并创建一个新活动并且不会恢复预览活动,当我按下后退按钮时预览一个是同一个窗口。我尝试了singleTop, singleTask, singleIntent
,它可以正常工作,但是当消息进入时它不会更新活动,就像预览活动处于暂停状态一样。我该如何解决?
如果没有解决方案我恢复活动或破坏暂停活动或者重启活动,有没有办法做到这一点?
public void CustomNotification(String strtext) {
// Using RemoteViews to bind custom layouts into Notification
RemoteViews remoteViews = new RemoteViews(getPackageName(),
R.layout.customnotification);
// Set Notification Title
String strtitle = getString(R.string.customnotificationtitle);
// Open NotificationView Class on Notification Click
Intent intent = new Intent(this, NotificationView.class);
// Send data to NotificationView Class
intent.putExtra("title", strtitle);
intent.putExtra("text", strtext);
intent.putExtra("String T", "");
//intent.putExtra("Integer C", 0);
// Open NotificationView.java Activity
PendingIntent pIntent = PendingIntent.getActivity(this, 0, intent,
PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
// Set Icon
.setSmallIcon(R.drawable.ic_launcher)
// Set Ticker Message
.setTicker(getString(R.string.customnotificationticker))
// Dismiss Notification
.setAutoCancel(true)
// Set PendingIntent into Notification
.setContentIntent(pIntent)
// Set RemoteViews into Notification
.setContent(remoteViews);
// Locate and set the Image into customnotificationtext.xml ImageViews
remoteViews.setImageViewResource(R.id.imagenotileft,R.drawable.ic_launcher);
remoteViews.setImageViewResource(R.id.imagenotiright,R.mipmap.ic_action_chat);
// Locate and set the Text into customnotificationtext.xml TextViews
remoteViews.setTextViewText(R.id.title,getString(R.string.customnotificationtitle));
remoteViews.setTextViewText(R.id.text, strtext);
// Create Notification Manager
NotificationManager notificationmanager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
// Build Notification with Notification Manager
notificationmanager.notify(0, builder.build());
}
日志:
04-29 01:29:10.453 29001-29001/com.example.example E/STATE﹕NEW ACTIVITY
04-29 01:29:15.376 29501-29501/com.example.example D/Activity﹕ Activity.onPause(), editTextTapSensorList size: 0
04-29 01:30:06.981 29501-29501/com.example.example E/STATE﹕ RESUMING
当我按下通知时:
04-29 01:33:09.654 33449-33530/com.example.example E/STATE﹕NEW ACTIVITY
我将不胜感激,谢谢!
EDITED
解答:
答案是补充:
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
| Intent.FLAG_ACTIVITY_NEW_TASK);
因为当我在同一个窗口并且我按下主页按钮时,活动暂停,然后如果有一些通知到达它带我到那个窗口但没有更新消息所以我需要创建一个新意图并擦除暂停的前一个。所以代码完成了这项工作。
答案 0 :(得分:2)
在您的服务代码中尝试使用通知:
private static final int NOTIFY_ME_ID = 1222;
NotificationManager mNotificationManager = (NotificationManager) this
.getSystemService(Context.NOTIFICATION_SERVICE);
private void Notifi(String message){
int requestID = (int) System.currentTimeMillis();
Intent intent = new Intent(Intent.ACTION_MAIN);
//CLEAR PREVIOUS ACTIVITIES AND CREATE A NEW ONE
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
| Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setComponent(new ComponentName("packagenameofyourapplication",
"packagenameofyourapplication.NotificationView"));
PendingIntent pintent = PendingIntent.getActivity(this, 0, intent,
PendingIntent.FLAG_UPDATE_CURRENT);
Notification notif = new Notification.Builder(this)
.setContentTitle(getString(R.string.notification_titel)).setContentText(message)
.setSmallIcon(R.drawable.ic_launcher).setAutoCancel(true)
.setContentIntent(pintent).build();
notif.flags = Notification.FLAG_NO_CLEAR;
mNotificationManager.notify(NOTIFY_ME_ID, notif);
}
答案 1 :(得分:2)
要更新您的活动,您应该覆盖onNewIntent()
:
protected void onNewIntent (Intent intent) {
super.onNewIntent(intent);
//reload your data here
}
void onNewIntent (Intent intent)
这适用于将
launchMode
设置为“singleTop”的活动 他们的包,或者如果客户端使用FLAG_ACTIVITY_SINGLE_TOP
标志 致电startActivity(Intent)
时。在任何一种情况下,当活动 在活动堆栈的顶部而不是新的重新启动 正在启动的活动的实例,将调用onNewIntent()
在具有用于重新启动的Intent的现有实例上 它在收到新意图之前,活动将始终暂停,所以 你可以依靠这种方法调用
onResume()
。请注意
getIntent()
仍会返回原始意图。您可以使用setIntent(Intent)
将其更新为此新Intent。
注意:正如文档所说,您需要在Activity标记中设置android:launchMode="singleTop"
(在AndroidManifest中)。