Alexander R.
- Forum-Beiträge: 1.148
21.01.2011, 22:53:34 via Website
21.01.2011 22:53:34 via Website
Hallo,
ich erstelle über einen Service eine Notification beim AppStart, wenn ich nun auf diese Notification klicke, soll die Activity allerdings nicht neu geöffnet werden sondern nur aufgerufen werden.
Im dickgedruckten Teil sage ich der Notification dass er die activity nur "updaten" soll jedoch ruft er eine neue auf...
Hoffe ihr könnt mir helfen
Der code sieht momentan folgendermassen aus:
ich erstelle über einen Service eine Notification beim AppStart, wenn ich nun auf diese Notification klicke, soll die Activity allerdings nicht neu geöffnet werden sondern nur aufgerufen werden.
Im dickgedruckten Teil sage ich der Notification dass er die activity nur "updaten" soll jedoch ruft er eine neue auf...
Hoffe ihr könnt mir helfen
Der code sieht momentan folgendermassen aus:
1public class StatusbarNotificator extends Service {
2 private NotificationManager mNM;
3 private Notification notification;
4
5 public class LocalBinder extends Binder {
6 StatusbarNotificator getService() {
7 return StatusbarNotificator.this;
8 }
9 }
10
11 @Override
12 public void onCreate() {
13 mNM = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
14 // Display a notification about us starting. We put an icon in the
15 // status bar.
16 showNotification();
17 }
18
19 @Override
20 public int onStartCommand(Intent intent, int flags, int startId) {
21 Log.i("LocalService", "Received start id " + startId + ": " + intent);
22 // We want this service to continue running until it is explicitly
23 // stopped, so return sticky.
24 return START_STICKY;
25 }
26
27 @Override
28 public void onDestroy() {
29 // Cancel the persistent notification.
30 mNM.cancel(R.string.local_service_started);
31 mNM.cancelAll();
32 }
33
34 @Override
35 public IBinder onBind(Intent intent) {
36 return mBinder;
37 }
38
39 // This is the object that receives interactions from clients. See
40 // RemoteService for a more complete example.
41 private final IBinder mBinder = new LocalBinder();
42
43 /**
44 * Show a notification while this service is running.
45 */
46 private void showNotification() {
47 // In this sample, we'll use the same text for the ticker and the
48 // expanded notification
49 CharSequence tickertext = getText(R.string.local_service_started_ticker);
50 CharSequence actiontext = getText(R.string.local_service_started_action);
51
52 // Set the icon, scrolling text and timestamp
53 notification = new Notification(R.drawable.icon, tickertext, System
54 .currentTimeMillis());
55
56 // The PendingIntent to launch our activity if the user selects this
57 // notification
58 PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
59 new Intent(this, MainActivity.class),
60 PendingIntent.FLAG_UPDATE_CURRENT);
61
62 // Set the info for the views that show in the notification panel.
63 notification.setLatestEventInfo(this,
64 getText(R.string.local_service_label), actiontext,
65 contentIntent);
66
67 notification.flags = Notification.FLAG_NO_CLEAR;
68
69 // Send the notification.
70 // We use a layout id because it is a unique number. We use it later to
71 // cancel.
72 mNM.notify(R.string.local_service_started, notification);
73 }
74}
2 private NotificationManager mNM;
3 private Notification notification;
4
5 public class LocalBinder extends Binder {
6 StatusbarNotificator getService() {
7 return StatusbarNotificator.this;
8 }
9 }
10
11 @Override
12 public void onCreate() {
13 mNM = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
14 // Display a notification about us starting. We put an icon in the
15 // status bar.
16 showNotification();
17 }
18
19 @Override
20 public int onStartCommand(Intent intent, int flags, int startId) {
21 Log.i("LocalService", "Received start id " + startId + ": " + intent);
22 // We want this service to continue running until it is explicitly
23 // stopped, so return sticky.
24 return START_STICKY;
25 }
26
27 @Override
28 public void onDestroy() {
29 // Cancel the persistent notification.
30 mNM.cancel(R.string.local_service_started);
31 mNM.cancelAll();
32 }
33
34 @Override
35 public IBinder onBind(Intent intent) {
36 return mBinder;
37 }
38
39 // This is the object that receives interactions from clients. See
40 // RemoteService for a more complete example.
41 private final IBinder mBinder = new LocalBinder();
42
43 /**
44 * Show a notification while this service is running.
45 */
46 private void showNotification() {
47 // In this sample, we'll use the same text for the ticker and the
48 // expanded notification
49 CharSequence tickertext = getText(R.string.local_service_started_ticker);
50 CharSequence actiontext = getText(R.string.local_service_started_action);
51
52 // Set the icon, scrolling text and timestamp
53 notification = new Notification(R.drawable.icon, tickertext, System
54 .currentTimeMillis());
55
56 // The PendingIntent to launch our activity if the user selects this
57 // notification
58 PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
59 new Intent(this, MainActivity.class),
60 PendingIntent.FLAG_UPDATE_CURRENT);
61
62 // Set the info for the views that show in the notification panel.
63 notification.setLatestEventInfo(this,
64 getText(R.string.local_service_label), actiontext,
65 contentIntent);
66
67 notification.flags = Notification.FLAG_NO_CLEAR;
68
69 // Send the notification.
70 // We use a layout id because it is a unique number. We use it later to
71 // cancel.
72 mNM.notify(R.string.local_service_started, notification);
73 }
74}
Gruß Alexander
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.