Как настроить режим повтора диспетчера будильников в режиме ожидания? - PullRequest
0 голосов
/ 12 января 2020

Я хочу установить диспетчер тревог, установить будильник по времени ежедневно и каждый час. все работает, но когда экран выключен. диспетчер аварийных сигналов не работает, и когда экран включается, все уведомления получены.

как мне справиться с повторением набора в режиме ожидания?

это мой приемник:

public class MyReceiver extends WakefulBroadcastReceiver {
private SqliteDatabase mDatabase;
int id;
String title, time, date, repeat, report;

@Override
public void onReceive(Context context, Intent intent) {
    //and doing something

        if (intent.getAction().equals("com.nooshindroid.yastashir")) {

            WakeLocker.acquire(context);
            mDatabase = new SqliteDatabase(context);
            id = intent.getExtras().getInt(EXTRA_ALARM_ID);
            title = intent.getExtras().getString(EXTRA_TITLE);
            time = intent.getExtras().getString(EXTRA_TIME);
            date = intent.getExtras().getString(EXTRA_ALARM_DATE);
            report = intent.getExtras().getString(EXTRA_REPORT);
            repeat = intent.getExtras().getString(EXTRA_REPEAT);
            Toast.makeText(context, "Its time!!!", Toast.LENGTH_SHORT).show();
            Log.i("khhhhhhhhhhhhhhh", "Started >>>>>>>" + title);
            if ("هشدار".equals(report)) {
                Intent intent1 = new Intent(context, AlarmDisplay.class);
                intent1.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                Bundle bundle = new Bundle();
                bundle.putInt(EXTRA_ALARM_ID, id);
                bundle.putString(EXTRA_TITLE, title);
                bundle.putString(EXTRA_ALARM_DATE, date);
                bundle.putString(EXTRA_TIME, time);
                bundle.putString(EXTRA_REPEAT, repeat);
                bundle.putString(EXTRA_REPORT, report);
                intent1.putExtras(bundle);
                context.startActivity(intent1);
            } else {
                createNotificationChannel(context, title);
            }
            WakeLocker.release();
    }
}

private void createNotificationChannel(Context context, String message) {

    if ("بدون تکرار".equals(repeat)) {
        Alarm alarm = new Alarm(id, title, date, time, repeat, report, 0);
        mDatabase.updateAlarm(alarm);
        Intent intent1 = new Intent("noti");
        intent1.putExtra("Message", "This is my message!");
        LocalBroadcastManager.getInstance(context).sendBroadcast(intent1);
    }
    Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);

    Intent intent = new Intent(context, AlarmActivity.class);
    intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);


    NotificationCompat.Builder builder = new NotificationCompat.Builder(context, "1234")
            .setSmallIcon(R.drawable.ic_launcher)
            .setContentTitle(context.getResources().getString(R.string.app_name))
            .setSound(alarmSound)
            .setContentText(message)
            .setContentIntent(PendingIntent.getActivity(context, 0,
                    intent, PendingIntent.FLAG_UPDATE_CURRENT))
            .setPriority(NotificationCompat.PRIORITY_DEFAULT);
    // Create the NotificationChannel, but only on API 26+ because
    // the NotificationChannel class is new and not in the support library
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        CharSequence name = "myNotification";
        String description = "test";
        int importance = NotificationManager.IMPORTANCE_DEFAULT;
        NotificationChannel channel = new NotificationChannel("1234", name, importance);
        channel.setDescription(description);
        // Register the channel with the system; you can't change the importance
        // or other notification behaviors after this
        NotificationManager notificationManager = context.getSystemService(NotificationManager.class);
        notificationManager.createNotificationChannel(channel);

        notificationManager.notify(1234, builder.build());


    } else {
        NotificationManagerCompat notificationManager1 = NotificationManagerCompat.from(context);
        // notificationId is a unique int for each notification that you must define
        notificationManager1.notify(1234, builder.build());

    }


}

}

и это мой установленный метод тревоги:

   public void StartAlarm(int id, Alarm alarm, Long time) {
    Intent intent = new Intent(context, MyReceiver.class);
    intent.setAction("com.nooshindroid.yastashir");
    intent.addFlags(Intent.FLAG_RECEIVER_FOREGROUND);
    Bundle bundle = new Bundle();
    bundle.putInt(EXTRA_ALARM_ID, alarm.getId());
    bundle.putString(EXTRA_TITLE, alarm.getTitle());
    bundle.putString(EXTRA_ALARM_DATE, alarm.getDate());
    bundle.putString(EXTRA_TIME, alarm.getTime());
    bundle.putString(EXTRA_REPEAT, alarm.getRepeat());
    bundle.putString(EXTRA_REPORT, alarm.getReport());
    intent.putExtras(bundle);
    pendingIntent = PendingIntent.getBroadcast(context, id, intent, PendingIntent.FLAG_CANCEL_CURRENT);
    AlarmManager alarmManager = (AlarmManager) context.getSystemService(ALARM_SERVICE);
    if ("بدون تکرار".equals(alarm.getRepeat())) {
        Log.i("ShowMeTime", "Once");


        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            // Wakes up the device in Doze Mode
            alarmManager.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, time, pendingIntent);
        } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            // Wakes up the device in Idle Mode
            alarmManager.setExact(AlarmManager.RTC_WAKEUP, time, pendingIntent);
        } else {
            // Old APIs
            alarmManager.set(AlarmManager.RTC, time, pendingIntent);
        }


    } else {
        Log.i("ShowMeTime", "Repeatably");
        switch (alarm.getRepeat()) {
            case "هر ساعت":
                Log.i("ShowMeTimeeee", "Every Hours");
             //   alarmManager.setAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, time, pendingIntent);
                alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, time, AlarmManager.INTERVAL_HOUR, pendingIntent);
                break;
            case "هر روز":
                alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, time, AlarmManager.INTERVAL_DAY, pendingIntent);
                break;
            case "هر هفته":
                alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, time, AlarmManager.INTERVAL_DAY * 7, pendingIntent);
                break;
            case "هر ماه":
                alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, time, AlarmManager.INTERVAL_DAY * 30, pendingIntent);
                break;
        }
    }

}
...