0
我有一个IntentService从9:00开始,并且每隔一小时重复一次。Android通知刷新活动
它与一个创建BUNDLE的AsyncTask一起工作。
IntentService必须在对话框中显示Actvity(如Viber的消息),它显示该BUNDLE中的部分数据,并且必须创建显示相同数据部分的通知。如果用户点击通知,它将开始第二个活动,显示来自BUNDLE的所有数据。
问题是:IntentService完成他的工作,显示活动并创建通知。但问题是,如果用户没有使用智能手机。一小时后,IntentService重新启动,并创建一个新的BUNDLE。
随着下面的代码,通知被刷新,但不是活动。如果用户现在使用智能手机,他会在活动中看到旧BUNDLE的数据,而不是新的BUNDLE。 我必须刷新此活动,显然如果用户点击通知,即使是开始的活动也必须刷新。
在 'IntentService', “onHandleIntent(意向意图)” 启动对话框活动:
Intent myIntent = new Intent(this.myContext, *DIALOG_ACTIVITY*);
myIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
myIntent.putExtras(**BUNDLE**);
this.myContext.startActivity(myIntent);
,并创建了通知:
Intent notifyIntent = new Intent(context, *SECOND_ACTIVITY*);
notifyIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
notifyIntent.putExtras(**BUNDLE**);
PendingIntent pIntent = PendingIntent.getActivity(context, 0, notifyIntent, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder builder = new NotificationCompat.Builder(context);
builder.setSmallIcon(icon);
builder.setContentTitle(title);
builder.setContentText(longText);
builder.setWhen(System.currentTimeMillis());
builder.setContentIntent(pIntent);
Notification n = builder.build();
n.flags |= Notification.FLAG_AUTO_CANCEL;
NotificationManager nm = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
nm.notify(idNotification, n);
在Android清单,DIALOG_ACTIVITY:
<activity
android:name="com.example.sample.DIALOG_ACTIVITY"
android:launchMode="singleTask"
android:taskAffinity=""
android:excludeFromRecents="true"
android:theme="@android:style/Theme.Dialog">
</activity>
and SECOND_ACTIVITY:
<activity
android:name="com.example.sample.SECOND_ACTIVITY"
android:theme="@style/Theme.AppCompat"
android:parentActivityName="com.example.sample.MainActivity" >
<!-- Parent activity meta-data to support API level 7+ -->
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.example.sample.MainActivity" />
</activity>
IDEAS?
非常感谢!
你能解释一下吗?有什么区别? “notification.setLatestEventInfo”?从文档“此方法已在API级别11中弃用。请改用Notification.Builder” –