I want to display a notification and repeat it every period. I read how to make that and i maked that code in the class that extends Broadcast Receiver:
public class OnAlarmReceiver extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
String ns = Context.NOTIFICATION_SERVICE;
NotificationManager mNotificationManager = (NotificationManager) getSystemService(ns);
int icon = R.drawable.icon;
CharSequence tickerText = "Votre vidange approche";
long when = System.currentTimeMillis();
Notification notification = new Notification(icon, tickerText, when);
CharSequence contentTitle = "Notification";
CharSequence contentText = "Vérifier votre kilométrage";
Intent notificationIntent = new Intent(this, Acceuil.class);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);
final int HELLO_ID = 1;
mNotificationManager.notify(HELLO_ID, notification);
}
However, i don’t know why i have this errors:
The method getSystemService(String) is undefined for the type OnAlarmReceiverThe constructor Intent(OnAlarmReceiver, Class<Acceuil>) is undefined
The method getActivity(Context, int, Intent, int) in the type PendingIntent is not applicable for the arguments (OnAlarmReceiver, int, Intent, int)
In the other activity i guess that i have to do this:
public void notifs() {
AlarmManager mgr=(AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
Intent i=new Intent(context, OnAlarmReceiver.class);
PendingIntent pi=PendingIntent.getBroadcast(context, 0, i, 0);
mgr.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime(), 1800000, pi);
}
What’s the problem here ?
Thank you very much.
The difference is :
and
The second one works because you call getSystemService on an Application Context, where as in the first you are trying to call it on the this object which at the point is an instance of subclass of BroadcastReceiver which has no method called getSystemService.
To fix this do :
This line gives you problems because you are trying to pass an instance of your custom class as the this object instead of a Context object like it expects.
To fix this do :