Q
qwert_zuiopue
Fortgeschrittenes Mitglied
- 6
Hallo ,
ich habe jetzt schon einiges ausprobiert und auch aus diversen Foren und Anleitungen im Internet Kopiert, aber egal wie ich es auch mache, es wird einfach keine Notification angezeigt.
Folgender Code (zusammenkopiert ):
wird aufgerufen. Die Ausgabe "Service meldet: X" erscheint auch, aber es wird eben keine Notification angezeigt.
Was mache ich hier falsch?
Vielen Dank !
ich habe jetzt schon einiges ausprobiert und auch aus diversen Foren und Anleitungen im Internet Kopiert, aber egal wie ich es auch mache, es wird einfach keine Notification angezeigt.
Folgender Code (zusammenkopiert ):
Code:
import android.app.AlarmManager;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.IBinder;
import android.support.v4.app.NotificationCompat;
public class NotificationService extends Service {
private static int num_msg = 0;
public static void incNumMsg() {
num_msg++;
}
@Override
public IBinder onBind(Intent arg0) {
return null;
}
@Override
public void onCreate() {
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
// Query the database and show alarm if it applies
Datenbank.getInstance().update();
int tmp = Datenbank.getInstance().getNumOfMessagesForUser(Datenbank.getInstance().getMyID());
System.out.println("Service meldet: "+tmp);
NotificationCompat.Builder builder =
new NotificationCompat.Builder(this)
.setContentTitle("My Notification Title")
.setContentText("Something interesting happened");
int NOTIFICATION_ID = 12345;
Intent targetIntent = new Intent(this, TargetActivity.class);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, targetIntent, PendingIntent.FLAG_UPDATE_CURRENT);
builder.setContentIntent(contentIntent);
NotificationManager nManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
nManager.notify(NOTIFICATION_ID, builder.build());
// I don't want this service to stay in memory, so I stop it
// immediately after doing what I wanted it to do.
stopSelf();
return Service.START_STICKY;
}
@Override
public void onDestroy() {
// I want to restart this service again in one hour
AlarmManager alarm = (AlarmManager)getSystemService(ALARM_SERVICE);
alarm.set(
AlarmManager.RTC,
System.currentTimeMillis() + (1000 * 5),
PendingIntent.getService(this, 0, new Intent(this, NotificationService.class), 0)
);
}
}
wird aufgerufen. Die Ausgabe "Service meldet: X" erscheint auch, aber es wird eben keine Notification angezeigt.
Was mache ich hier falsch?
Vielen Dank !