Dieses Codebeispiel habe ich von NewsRob erhalten, scheint also nicht so schwer zu sein
1package com.newsrob;
2
3import java.util.Date;
4
5import android.app.Notification;
6import android.app.NotificationManager;
7import android.app.PendingIntent;
8import android.content.Context;
9import android.content.Intent;
10
11class NewsRobNotificationManager implements IEntryModelUpdateListener {
12
13 static final int NOTIFICATION_SYNCHRONIZATION_RUNNING = 0;
14 static final int NOTIFICATION_SYNCHRONIZATION_STOPPED_WITH_ERROR = 1;
15
16 private NotificationManager nm;
17 private Context context;
18
19 private boolean displaysNotification;
20
21 NewsRobNotificationManager(Context context) {
22 this.nm = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
23 this.context = context.getApplicationContext();
24 }
25
26 private void cancelAllNotifications() {
27 cancelSyncInProgressNotification();
28 cancelSyncProblemNotification();
29 }
30
31 private void cancelSyncProblemNotification() {
32 nm.cancel(NOTIFICATION_SYNCHRONIZATION_STOPPED_WITH_ERROR);
33 }
34
35 private void cancelSyncInProgressNotification() {
36 nm.cancel(NOTIFICATION_SYNCHRONIZATION_RUNNING);
37 displaysNotification = false;
38 PL.log("NOTIFICATION: Running: unset", context);
39 }
40
41 private Notification createSynchronizationProblemNotification() {
42 PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, new Intent().setClass(context,
43 LoginActivity.class), 0);
44 Notification n = new Notification(R.drawable.sync_problem, U.t(context,
45 R.string.login_to_google_needed), new Date().getTime());
46 n.setLatestEventInfo(context, U.t(context, R.string.app_name),
47
48 U.t(context, R.string.login_to_google_needed), pendingIntent); // TODO
49 // i18n
50
51 return n;
52 }
53
54 private Notification createSynchronizationRunningNotification() {
55 Notification n = new Notification(R.drawable.sync_running, context.getResources().getString(
56 R.string.synchronization_running_notification_title), new Date().getTime());
57 Intent intent = new Intent(context, DashboardListActivity.class);
58 n.setLatestEventInfo(context, U.t(context, R.string.synchronization_running_notification_title), U.t(
59 context, R.string.synchronization_running_notification_summary), PendingIntent.getActivity(
60 context, 0, intent, 0));
61 n.flags = Notification.FLAG_ONGOING_EVENT;
62 return n;
63 }
64
65 private void sendSynchronizationRunningNotification() {
66 nm.notify(NOTIFICATION_SYNCHRONIZATION_RUNNING, createSynchronizationRunningNotification());
67 displaysNotification = true;
68 PL.log("NOTIFICATION: Running: set", context);
69 }
70
71 void sendSynchronizationProblemNotification() {
72 nm
73 .notify(NOTIFICATION_SYNCHRONIZATION_STOPPED_WITH_ERROR,
74 createSynchronizationProblemNotification());
75 }
76
77 public void modelUpdateFinished(ModelUpdateResult result) {
78 cancelAllNotifications();
79 }
80
81 public void modelUpdateStarted() {
82 // clear old and error notifications
83 cancelAllNotifications();
84
85 // set during notification
86 if (EntryManager.DEBUG)
87 sendSynchronizationRunningNotification();
88 }
89
90 public void modelUpdated() {
91 }
92
93 public void statusUpdated() {
94
95 }
96
97 public void finalize() {
98 if (displaysNotification)
99 PL.log("WTF? Notification wasn't cleared.", context);
100 }
101}
Empfohlener redaktioneller Inhalt
Mit Deiner Zustimmung wird hier ein externer Inhalt geladen.
Mit Klick auf den oben stehenden Button erklärst Du Dich damit einverstanden, dass Dir externe Inhalte angezeigt werden dürfen. Dabei können personenbezogene Daten an Drittanbieter übermittelt werden. Mehr Infos dazu findest Du in unserer Datenschutzerklärung.