How to give notice by AlarmManager?
-
There are two classes.
First...
AlarmManager
- causing an event at a certain interval,Second -
NotificationManager
- removes direct notices.Both classes work correctly, but I can't combine them at a certain interval in the class.
AlarmManager
a class methodNotificationManager
First Class
package com.example.reminder;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.PowerManager;
import android.widget.Toast;import java.text.Format;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;public class ReminderManager extends BroadcastReceiver{
ReminderNotifications rn = new ReminderNotifications();
final public static String ONE_TIME = "onetime";
Random random = new Random();
int time;@Override public void onReceive(Context context, Intent intent) { PowerManager pm = (PowerManager)context.getSystemService(Context.POWER_SERVICE); PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "YOUR_TAG"); wl.acquire(); Bundle extras = intent.getExtras(); StringBuilder msgStr = new StringBuilder(); Format formatter = new SimpleDateFormat("hh:mm:ss a"); msgStr.append(formatter.format(new Date())); //rn.MyNotification(); //метод из второго класса, при вызове приложение прерывается Toast.makeText(context, msgStr, Toast.LENGTH_SHORT).show(); wl.release(); } public void setAlarm(Context context){ AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE); Intent intent = new Intent(context, ReminderManager.class); intent.putExtra(ONE_TIME, Boolean.FALSE); PendingIntent pi = PendingIntent.getBroadcast(context, 0, intent, 0); time = random.nextInt(1000 * 10) + 1000; am.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), time, pi); } public void cancelAlarm(Context context){ Intent intent = new Intent(context, ReminderManager.class); PendingIntent sender = PendingIntent.getBroadcast(context, 0, intent, 0); AlarmManager alarmManager = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE); alarmManager.cancel(sender); }
}
Second Class
package com.example.reminder;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.content.res.Resources;
import android.graphics.BitmapFactory;
import android.support.v7.app.AppCompatActivity;public class ReminderNotifications extends AppCompatActivity{
private final static int NOTIFY_ID = 101;public void MyNotification(){ Context context = getApplicationContext(); Intent notificationIntent = new Intent(context, ReminderNotifications.class); PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, PendingIntent.FLAG_CANCEL_CURRENT); Resources res = context.getResources(); Notification.Builder builder = new Notification.Builder(context); builder.setContentIntent(contentIntent) .setSmallIcon(R.mipmap.ic_launcher) .setLargeIcon(BitmapFactory.decodeResource(res, R.mipmap.ic_launcher)) .setTicker("Attention") .setWhen(System.currentTimeMillis()) .setAutoCancel(true) .setContentTitle("Напоминание") .setContentText("Пора уходить домой"); Notification notification = builder.getNotification(); //до 16 API //Notification notification = builder.build(); NotificationManager notificationManager = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE); notificationManager.notify(NOTIFY_ID, notification); }
}
I'm sorry if the problem is too trivial, but it hasn't figured out yet.
-
BroadcastReceiver
He doesn't know anything about your activism, he's being created by a system in his own context, which is transmitted to the method.onReceive()
♪ That context should be used to create notifications.That's why your notification code isn't so easy to use.
BroadcastReceiver
♪Just take it apart from activating, the context can be presented as a parameter, then it'll work.