Statusbar Notification

  • Antworten:5
Alexander R.
  • Forum-Beiträge: 1.148

24.11.2010, 20:21:16 via Website

Hallo,
ich arbeite zur Zeit an einer Statusbar Notification.
Momentan lasse ich sie durch die MainActivity starten, da in dieser bisher noch nicht viel drin ist.
Gibt es eine Möglichkeit diese in eine "Notification.java"-Datei zu packen und dort beim Start ausführen zu lassen?

Danke schonmal

Gruß Alexander

Antworten
Alexander R.
  • Forum-Beiträge: 1.148

26.11.2010, 14:09:09 via Website

Bisher habe ich es folgendermassen versucht:
Die App an sich startet, jedoch wird keine Notification gestartet/gezeigt.

Status.java
1package de.Rocky.AppWunsch;
2
3import java.util.Timer;
4
5import android.app.Notification;
6import android.app.NotificationManager;
7import android.app.PendingIntent;
8import android.app.Service;
9import android.content.Context;
10import android.content.Intent;
11import android.os.IBinder;
12
13public class Status extends Service {
14
15 private NotificationManager nManager;
16 private static final int APP_ID = 0;
17 private Timer timer = new Timer();
18
19 public IBinder onBind(Intent intent) {
20 return null;
21 }
22
23 @Override
24 public void onCreate() {
25 super.onCreate();
26 // init the service here
27 _startService();
28
29 }
30
31 @Override
32 public void onStart(Intent intent, int startId) {
33
34 }
35
36
37 @Override
38 public void onDestroy() {
39 super.onDestroy();
40 _shutdownService();
41
42 }
43
44 private void _shutdownService() {
45 if (timer != null) timer.cancel();
46 }
47
48
49 private void _startService() {
50
51 Thread t = new Thread() {
52 public void run() {
53 _Notification();
54 }
55 };
56 t.start();
57
58 }
59
60 private void _Notification() {
61
62 nManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
63
64 Notification notification = new Notification(R.drawable.icon,
65 "Notify", System.currentTimeMillis());
66 notification.setLatestEventInfo(this.getApplication(),
67 "AppName","Description of the notification",
68 PendingIntent.getActivity(this.getBaseContext(), 0, null,
69 PendingIntent.FLAG_CANCEL_CURRENT));
70 notification.flags = Notification.FLAG_NO_CLEAR;
71 nManager.notify(APP_ID, notification);
72
73 }
74}

AndroidManifest.xml
1<?xml version="1.0" encoding="utf-8"?>
2<manifest xmlns:android="http://schemas.android.com/apk/res/android"
3 package="de.Rocky.AppWunsch"
4 android:versionCode="1"
5 android:versionName="1.0"
6 android:installLocation="auto">
7 <application android:icon="@drawable/icon" android:label="@string/app_name">
8 <activity android:name=".AppWunsch"
9 android:label="@string/app_name"
10 android:configChanges="keyboardHidden|orientation">
11 <intent-filter>
12 <action android:name="android.intent.action.MAIN" />
13 <category android:name="android.intent.category.LAUNCHER" />
14 </intent-filter>
15 </activity>
16
17 <activity android:name="Entry"
18 android:configChanges="keyboardHidden|orientation" />
19
20<service android:name="Status" />
21</application>
22 <uses-sdk android:minSdkVersion="3"
23 android:targetSdkVersion="8" />
24
25 <supports-screens android:smallScreens="true"
26 android:normalScreens="true"
27 android:largeScreens="true"
28 android:anyDensity="true" />
29
30 <uses-permission android:name="android.permission.INTERNET" />
31
32</manifest>

— geändert am 26.11.2010, 17:36:00

Gruß Alexander

Antworten
Alexander R.
  • Forum-Beiträge: 1.148

27.11.2010, 12:28:35 via App

Habe es mit verschiedenen aufrufen versucht aber es kommt nicht.
Habe gerade keine LogCat zur Verfügung

— geändert am 27.11.2010, 12:34:46

Gruß Alexander

Antworten
Alexander R.
  • Forum-Beiträge: 1.148

29.11.2010, 09:23:55 via App

Hallo,
muss ich es eventuell noch in eer MainActivity irgendwie aufrufen?

Gruß Alexander

Antworten
Hendrik
  • Forum-Beiträge: 13

29.11.2010, 12:21:36 via Website

na du solltest den Service den du angelegt hast schon zu irgendeinem Zeitpunkt auch starten. Ob du das nun in deiner "MainActivity" tust ist dem Design deiner Anwendung überlassen.

http://developer.android.com/reference/android/app/Service.html

Antworten
Alexander R.
  • Forum-Beiträge: 1.148

03.12.2010, 23:20:56 via Website

Hallo,
ich habe mal wieder etwas dran rum gebastelt...

Es wird nun geöffnet und angezeigt und beim beenden auch beendet.

Nur wird es auch beendet, wenn ich über die "Zurück"-Taste die App verlass beendet er die Notification...
Wie kann ich das verhindern?

— geändert am 04.12.2010, 00:59:47

Gruß Alexander

Antworten