阿拉姆没有按时间表工作

时间:2018-08-14 17:25:28

标签: android broadcastreceiver android-service alarmmanager android-alarms

我正在创建一个Android应用,用户可以在其中添加多个婴儿,每个婴儿都有多个日期被调用。 我从 SQLite数据库获取日期。获取日期后,警报将设置为这些日期。我已正确设置所有内容,但警报无法正常工作。

这是代码。

List  babyDataList = new ArrayList<>();
List  listItems = new ArrayList<>();

Database db = new Database();

babyDataList = db.getAllBabies();
calendar = Calendar.getInstance();
calenderThird = Calendar.getInstance();

for(BabyModal b : babyDataList ){
 String name = b.getBabyName();
 String first = b.getFirstVac(); // FirstDate
 String sec = b.getSecVac(); //SecondDate

 String[] val1 = first.split("-");
  int dateSec = Integer.parseInt(val1[0]);
  int monthSec = Integer.parseInt(val1[1]);
  int yearSec = Integer.parseInt(val1[2]);
   calendar.set(
       yearSec, monthSec-1, dateSec, 8, 30, 30
   );
 setAlarm(calendar);

String[] val2 = sec.split("-");
   int dateThird = Integer.parseInt(val2[0]);
   int monthThird = Integer.parseInt(val2[1]);
   int yearThird = Integer.parseInt(val2[2]);
  calenderThird.set(
       yearThird, monthThird-1, dateThird, 8, 30, 30
  );
     setAlarm(calendarThird);

   listItems.add(b);
}

setAlarm 的方法是

private void setAlarm(Calendar cal){
   Intent intent = new Intent(this, AlarmReceiver.class);
    PendingIntent pendingIntent = PendingIntent.getActivity(this,
            (int) cal.getTimeInMillis(), intent, PendingIntent.FLAG_CANCEL_CURRENT);
    AlarmManager am =
            (AlarmManager)getSystemService(Activity.ALARM_SERVICE);
    am.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(),
            pendingIntent);
}

AlarmReceiver 类:

public class AlarmReceiver extends BroadcastReceiver{
  @Override
  public void onReceive(Context context, Intent intent) {
    Intent intent1 = new Intent(context, MyNewIntentService.class);
    context.startService(intent1);
  }
}

MyNewIntentService 类:

    public class MyNewIntentService extends IntentService {
    private static final int NOTIFICATION_ID = 3;

    public MyNewIntentService(String name) {
        super(name);
    }
      @Override
    protected void onHandleIntent(@Nullable Intent intent) {
        Notification.Builder builder = new Notification.Builder(this);
        builder.setContentTitle("Vaccination Time");
        builder.setContentText(" ");
        builder.setSmallIcon(R.drawable.health);
            builder.setVibrate(new long[] { 1000, 1000, 1000, 1000, 1000 });

        builder.setLights(Color.RED, 3000, 3000);

        Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);

        builder.setSound(alarmSound);

        PowerManager pm = (PowerManager)this.getSystemService(Context.POWER_SERVICE);
        boolean isScreenOn = pm.isScreenOn();
        if(isScreenOn==false){
            PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK |PowerManager.ACQUIRE_CAUSES_WAKEUP |PowerManager.ON_AFTER_RELEASE,"MyLock");
            wl.acquire(10000);
            PowerManager.WakeLock wl_cpu = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,"MyCpuLock");

            wl_cpu.acquire(10000);
        }
       Intent notifyIntent = new Intent(this, BabiesList.class);
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 2, notifyIntent, PendingIntent.FLAG_UPDATE_CURRENT);
        //to be able to launch your activity from the notification
        builder.setContentIntent(pendingIntent);
        Notification notificationCompat = builder.build();
        NotificationManagerCompat managerCompat = NotificationManagerCompat.from(this);
        managerCompat.notify(NOTIFICATION_ID, notificationCompat);
    }
}

谢谢。

1 个答案:

答案 0 :(得分:0)

  

无论您发布了什么并想解决的,我都不会解决您的问题;而是我会尝试清除基础知识...

什么是alarm manager

not the one是系统使用traditional alarms唤醒人们的睡眠,相反,它是相同的概念……只是developers所用;谁想wake up their piece of code at the specified time ...

然后如何wake up my code ...

  

请注意,以下代码在4.0 to the latest android P中有效; tested全部

Calendar CalendarEvent = Calendar.getInstance(TimeZone.getDefault(), Locale.getDefault());
CalendarEvent.set(Calendar.HOUR_OF_DAY, 14);
CalendarEvent.set(Calendar.MINUTE, 00);
CalendarEvent.set(Calendar.SECOND, 00);

Log.d("SCHEDULER : ", "CALENDAR SET TIME :"+CalendarEvent.getTime()+"\n");

Intent myintent = new Intent(this, MyService.class);
PendingIntent pi = PendingIntent.getService(this, 0, myintent, 0);
AlarmManager alarm_manager = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
alarm_manager.set(AlarmManager.RTC, CalendarEvent.getTimeInMillis(), pi);

What is this ... ??

此处HOUR_OF_DAY设置为14,表示根据您的默认时区,2PM中的afternoon。在下午2点,它将开始MyService并注意not necessary,您的应用程序必须保留在foregroundbackgroundrecents中!! IT WILL GET CALLED ...

需要什么permissions

仅在需要时<uses-permission android:name="android.permission.WAKE_LOCK" /> ...请注意,<uses-permission android:name="com.android.alarm.permission.SET_ALARM" />not necessary,因为它打算用于traditional alarms

什么是deadlines ...?

Until next-boot这些警报是由Android系统remembered .. !!下次启动时,所有警报都是由Android系统deleted发出的。。。这意味着您是在2018年1月20日设置警报,还是在前一天甚至在timeinmillis之前重启设备。 。android will forget it

如何记住它across every boot ...?

BOOT_COMPLETED的{​​{1}} intent用于具有receiver权限的<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />并重新设置所有警报manually ... again。 。借助数据库table

It is not the answer of my question which i asked ....

Yes, it is。首先在新的concept sample中实现此project。在您想要的所有情况下,都使用可在android平台上运行的代码清除概念,并在主项目中使用then implement it in the way you wants...。

Huuushhhh ...