Android notification refresh activity

倖福魔咒の 提交于 2019-12-23 04:56:34

问题


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

It works with an AsyncTask that creates a BUNDLE.

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.

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.

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.

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);

and creates the notification:

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);

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>

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?

THANKS A LOT!


回答1:


Try this :

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);


来源:https://stackoverflow.com/questions/20149685/android-notification-refresh-activity

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!