Android的通知刷新活动 [英] Android notification refresh activity

查看:221
本文介绍了Android的通知刷新活动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个IntentService开始于9:00和重复每隔一小时。

I have an IntentService starts at 9:00 and repeats every hour.

它的工作原理与创建捆绑的AsyncTask的。

It works with an AsyncTask that creates a BUNDLE.

该IntentService必须显示在对话框(如Viber的消息),显示在此包DATAS的一部分的Actvity,它必须创建一个通知,显示DATAS的相同部分。如果用户点击了通知,它会开始显示从捆绑中的所有DATAS第二次活动。

The IntentService have to show an Actvity in a Dialog (like Viber for messages), that shows a portion of datas in this BUNDLE and it have to create a notification that shows the same portion of datas. If the user clicks on the notification, it will start a second activity that shows all datas from BUNDLE.

问题是:IntentService做他的工作,显示了活动,并创建了通知。但问题是,如果用户没有使用智能手机。一个小时后,重新启动IntentService并做他的工作创造了新的软件包。

The problem is: the IntentService do his job, shows the activity and creates the notification. But the problem is, if the user didn't use the smartphone. After an hour, the IntentService restarts and it does his job creating a NEW BUNDLE.

以code节,通知被刷新,而不是活动。如果用户,现在使用智能手机时,他会看到老包的DATAS活动中,没有新的软件包。
我要刷新这个活动,很明显,如果用户点击启动必须刷新通知,甚至该活动。

WITH THE CODE BELOW, THE NOTIFICATION IS REFRESHED, BUT NOT THE ACTIVITY. If the user, now, uses the smartphone, he will see the datas of old BUNDLE in the activity, not the new BUNDLE. I HAVE TO REFRESH THIS ACTIVITY, obviously if the user click on the notification, even the activity that starts have to be refreshed.

在'IntentService,onHandleIntent(意向意图)启动对话框活动:

In the 'IntentService', "onHandleIntent(Intent intent)" starts the dialog activity:

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:

In the Android Manifest, 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>

和SECOND_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>

想法?

非常感谢!

推荐答案

试试这个:

myList.setAdapter(adapter);

NotificationManager nm = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);

Notification notification = new Notification(android.R.drawable.stat_sys_warning, 
                        "This is an important msg", 

System.currentTimeMillis());

ntent intent = new Intent(MainActivity.this, MainActivity.class);

PendingIntent pendingNotify = PendingIntent.getActivity(MainActivity.this,
                        0, intent, 0);

notification.setLatestEventInfo(MainActivity.this, "This is a demo", "Continue with yourwork.",pendingNotify);


nm.notify(0, notification);

这篇关于Android的通知刷新活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆