Aktivity nur ein Mal erzeugen

  • Antworten:3
Jürgen
  • Forum-Beiträge: 50

01.09.2012, 10:49:52 via Website

Hi,

als blutiger Android-Neuling bin ich auf dieses Forum gestossen und hab auch schon einiges hier gelesen.
Nun hab ich aber doch eine Frage, auf die ich keine Antwort finden konnte.

Ich bin dabei eine kleine app zu schreiben (meine erste :) )

Ein Service soll immer im Hintergrund auf ein Ereignis triggern und im bei Bedarf eine
Notification anzeigen, die wiederum eine Aktivity anzeigt.
Die Activity ist die Einzige und kann also auch über das Icon bei den Programmen gestartet werden.

Soweit funktioniert das auch schon mal alles, nur das es mir die Aktivity mehrfach erzeugt wird und wenn ich dann auf "zurück" gehe ich die auch immer mehrmals angezeigt bekomme.
Dieses möchte ich aber so nicht, sondern ich möchte nur eine Aktivity erzeugen wenn es noch keine gibt (sonst die alte anzeigen
oder die alte killen und eine neue erzeugen).

Hier der Aufruf im Service (quasi nach Lehrbuch)
1Intent notificationIntent = new Intent(context, Main.class);
2 notificationIntent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP/*FLAG_ACTIVITY_NEW_TASK*/);

THX und Gruss
J.

Antworten
Gelöschter Account
  • Forum-Beiträge: 694

01.09.2012, 11:40:18 via Website

Ich gebe der Notfication im Service gleich einen PendingIntent mit - damit bin ich von eigener Arbeit befreit.

"this" ist der Service:

1private void processStartNotification() {
2 Intent intent = new Intent(this, DeineActivity.class);
3 intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
4
5 PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);
6
7 Notification notification = new Notification(R.drawable.xxx, appName, System.currentTimeMillis());
8 notification.setLatestEventInfo(this, appName, getResources().getString(R.string.txt_xxx), pendingIntent);
9
10 notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
11 notificationManager.notify(NOTIFICATION_TAG, notification);
12 }

— geändert am 01.09.2012, 11:41:21

Antworten
Jürgen
  • Forum-Beiträge: 50

01.09.2012, 12:18:34 via Website

Danke für die schnelle Antwort.
Ich werde es mal ausprobieren ob das besser ist.

Interessant ist, dass es sich im Emulator offensichtlich anders verhält als auf der echten HW.

Gruß
J.

Antworten
Jürgen
  • Forum-Beiträge: 50

02.09.2012, 12:08:19 via Website

Thx das hat geholfen und funktioniert soweit erst mal.
Allerdings eine Frage hab ich noch dazu, ich mach mal deshalb keinen neuen Beitrag auf.

Der Service holt zyklisch einen String ab auf den er auch triggert und die Notivication bringt.
Dieser String steht dann auch als Text in der Notivication.

Wie bekomme ich jetzt diesen String in ein TextView der Activity???

1//dynamischer BroadcastReciver für Notivication
2 private class bprReciver extends BroadcastReceiver
3 {
4 @Override
5 public void onReceive(Context context, Intent intent) {
6 String str = intent.getStringExtra("textStrg"); // <-- Dieser Sting soll auch in TV
7 if(str != null) {
8 processStartNotification(str);
9 }
10 }
11 }
12
13 /*
14 * Start Notivication
15 */
16 private void processStartNotification(String str) {
17 int icon = R.drawable.icon;
18 String title = getString(R.string.title);
19 String text = str;
20 long when = System.currentTimeMillis();
21
22 Intent intent = new Intent(this, Main.class);
23 intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
24
25 PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);
26
27 Notification notification = new Notification(icon, title, when);
28 notification.setLatestEventInfo(this, title, text, pendingIntent);
29 notification.defaults |= Notification.DEFAULT_SOUND;
30 notification.flags |= Notification.FLAG_NO_CLEAR;
31
32 NotificationManager mgr = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
33 mgr.notify(ID, notification);
34 }

Gruß
J.

Antworten