L
lewa19
Neues Mitglied
- 0
Hallo. Ich habe eine App, in der ich zu einer bestimmten Zeit eine automatische Benachrichtigung (Notification) in der Android Benachrichtigungsleiste anzeigen will, so, wie wenn man etwa bei Whatsapp eine Nachricht bekommt. Das ganze soll natürlich vor allem dann funktionieren, wenn die App gerade nicht verwendet wird. Aber es funktioniert gar nicht, auch nicht, wenn ich die App geöffnet habe, sondern nur beim Öffnen und Schließen der App. Hier ist mein Code der Activity:
Und hier der Code von meinem Service:
Was muss ich ändern? Vielen Dank!
Der ursprüngliche Beitrag von 14:19 Uhr wurde um 15:00 Uhr ergänzt:
Ich habed das Problem wie folgt gelöst: In meinem Service habe ich ein Runnable geschrieben, dass sich alle 5 Sekunde wiederholt (die Zeit kann man ja ändern, alle 5 Sekunden eine Benachrichtigung wäre ja voll nervig ). Wird der Service beendet, startet er sich automatisch neu, er läuft also immer. Code von meinem Service:
In der MainActivity wird der Service beim ersten Aufrufen der App gestartet:
Allerdings ist diese Lösung wahrscheinlich nicht optimal. Wenn irgendjemand noch etwas besseres weiß, würde ich mich über Hilfe freuen
Code:
Intent myIntent = new Intent(MainActivity.this , NotificationSetter.class);
AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
PendingIntent pending;
pending = PendingIntent.getService(MainActivity.this, 0, myIntent, 0);
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, 12);
calendar.set(Calendar.MINUTE, 00);
calendar.set(Calendar.SECOND, 00);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 24*60*60*1000 , pending);
Code:
public class NotificationSetter extends Service
{
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
@Override
public void onCreate()
{
// TODO: Implement this method
super.onCreate();
Intent intent = new Intent(this, MainActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);
Notification n = new Notification.Builder(this)
.setContentTitle("Titel")
.setContentText("Info")
.setSmallIcon(R.drawable.ic_launcher)
.setContentIntent(pendingIntent)
.setAutoCancel(true)
.addAction(R.drawable.ic_launcher, "Öffnen", pendingIntent).build();
NotificationManager notificationManager =
(NotificationManager) getSystemService(NOTIFICATION_SERVICE);
notificationManager.notify(0, n);
}
}
Der ursprüngliche Beitrag von 14:19 Uhr wurde um 15:00 Uhr ergänzt:
Ich habed das Problem wie folgt gelöst: In meinem Service habe ich ein Runnable geschrieben, dass sich alle 5 Sekunde wiederholt (die Zeit kann man ja ändern, alle 5 Sekunden eine Benachrichtigung wäre ja voll nervig ). Wird der Service beendet, startet er sich automatisch neu, er läuft also immer. Code von meinem Service:
Code:
public class NotificationSetter extends Service
{
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
@Override
public void onCreate()
{
// TODO: Implement this method
super.onCreate();
handler.postDelayed(updateNotification, 5000);
}
private Handler handler=new Handler();
private Runnable updateNotification=new Runnable(){
@Override
public void run()
{
// TODO: Implement this method
Intent intent = new Intent(NotificationSetter.this, Erinnerungen.class);
PendingIntent pendingIntent = PendingIntent.getActivity(NotificationSetter.this, 0, intent, 0);
Notification n = new Notification.Builder(NotificationSetter.this)
.setContentTitle("Erinnerung")
.setContentText("Nächste Erinnerung")
.setSmallIcon(R.drawable.logo_cnow)
.setContentIntent(pendingIntent)
.setAutoCancel(true)
.addAction(R.drawable.ic_launcher, "Ansehen", pendingIntent).build();
NotificationManager notificationManager =
(NotificationManager) getSystemService(NOTIFICATION_SERVICE);
notificationManager.notify(0, n);
handler.removeCallbacks(updateNotification);
handler.postDelayed(updateNotification, 5000);
}
};
@Override
public void onDestroy()
{
// TODO: Implement this method
Intent in=new Intent(NotificationSetter.this, NotificationSetter.class);
startService(in);
}
}
Code:
Intent myIntent = new Intent(Erinnerungen.this , NotificationSetter.class);
startService(myIntent);
Zuletzt bearbeitet: