- Forum-Beiträge: 33
11.03.2015, 16:16:38 via Website
11.03.2015 16:16:38 via Website
Hallo zusammen,
ich versuche jetzt schon länger herauszufinden, wie ich eine Notification zu einer bestimmten Zeit & Datum ausführen kann.
Verschiedene Codes hatte ich gefunden, die einen deprecated, die anderen gingen nicht :/..
Ich wäre echt mega froh, wenn mir jemand n funktionierendes & aktuelles Beispiel posten könnte.
Bisher habe ich folgenden Code und noch ein Problem:
Die Funktion zur Erstellung einer Notification:
private void createScheduledNotification(int days) {
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
//calendar.add(Calendar.MINUTE, 1);
// Retrieve alarm manager from the system
AlarmManager alarmManager = (AlarmManager) getApplicationContext()
.getSystemService(getBaseContext().ALARM_SERVICE);
// Every scheduled intent needs a different ID, else it is just executed
// once
int id = (int) System.currentTimeMillis();
// Prepare the intent which should be launched at the date
Intent intent = new Intent(this, TimeAlarm.class);
intent.putExtra("test", "sjdksjdksdjsakjd");
// Prepare the pending intent
PendingIntent pendingIntent = PendingIntent.getBroadcast(
getApplicationContext(), id, intent,
PendingIntent.FLAG_UPDATE_CURRENT);
// Register the alert in the system. You have the option to define if
// the device has to wake up on the alert or not
alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
pendingIntent);
}
Die TimeAlarm-Klasse:
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
// The class has to extend the BroadcastReceiver to get the notification from the system
public class TimeAlarm extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent paramIntent) {
// Request the notification manager
NotificationManager notificationManager = (NotificationManager) context
.getSystemService(Context.NOTIFICATION_SERVICE);
/*
// Create a new intent which will be fired if you click on the
// notification
Intent intent = new Intent(context, NoteDetailView.class);
intent.putExtra("test2", "test22");
// Attach the intent to a pending intent
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0,
intent, PendingIntent.FLAG_UPDATE_CURRENT);*/
// Create the notification
Notification notification = new Notification(R.drawable.ic_launcher,
"Visit our homepage", System.currentTimeMillis());
notification.setLatestEventInfo(context, "Visit our homepage",
"http://www.papers.ch", pendingIntent);
notificationManager.notify(1, notification);
}
}
Mit der obigen Funktion erstelle ich die Notification und übergebe der TimeAlarm Class bereits ein Extra namens "test".
Jetzt möchte ich dies in der dritten Klasse (die hier nicht ist, weil nur ein onCreate mit getExtras drin ist..) auslesen.
Allerdings kann ich nur das Extra test2 empfangen/auslesen. Kann ich nicht direkt aus der 2 Klasse Infos mitgeben?
BZw. wie bekomme ich das erste "test" zur letzten Klasse?
PS: Man ignoriere die "unused" Sachen, die sind beabsichtig so.