Настраиваемое уведомление на дату и время пользователя - PullRequest
0 голосов
/ 05 октября 2018

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

public static void setAlarm(Context context, long id, int rYear, int rMonth, int rDay, int rHour, int rMinute) {
    if (Share.wantReminder) {
        Share.wantReminder = false;

        Calendar calendar = new GregorianCalendar(rYear, rMonth, rDay, rHour, rMinute);


        Calendar calendar1 = new GregorianCalendar();
        calendar1.setTimeInMillis(System.currentTimeMillis());
        calendar1.set(Calendar.YEAR, calendar.get(Calendar.YEAR));
        calendar1.set(Calendar.MONTH, calendar.get(Calendar.MONTH));
        calendar1.set(Calendar.DAY_OF_MONTH, calendar.get(Calendar.DAY_OF_MONTH));
        calendar1.set(Calendar.HOUR_OF_DAY, calendar.get(Calendar.HOUR_OF_DAY));
        calendar1.set(Calendar.MINUTE, calendar.get(Calendar.MINUTE));

        Intent intent = new Intent(context, ReminderService.class);
        intent.setAction(String.valueOf(id));
        PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_ONE_SHOT);

        AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
        alarmManager.set(AlarmManager.RTC_WAKEUP, calendar1.getTimeInMillis(), pendingIntent);

        Toast.makeText(context, "Alarm set", Toast.LENGTH_SHORT).show();
    }
}

Мой Manifest.xml

    <receiver android:name=".service.ReminderService">
        <intent-filter>
            <action android:name="android.intent.action.BOOT_COMPLETED" />
        </intent-filter>
    </receiver>

На панель действий не приходят уведомления
Что мне делать?

Ответы [ 2 ]

0 голосов
/ 05 октября 2018

Внесите следующие изменения в свой приемник в Manifest.xml

<receiver
            android:name=".service.ReminderService"
            android:process=":remote"
            android:excludeFromRecents="true" />

Создание и отображение уведомлений внутри onReceive () метод ReminderService класс, как показано ниже.

public class ReminderService extends BroadcastReceiver {

    @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, "");
        wl.acquire();

        //Put your code here to create notification

        wl.release();
    }
}

Добавьте ниже разрешение в вашем Manifest.xml

<uses-permission android:name="android.permission.WAKE_LOCK" />
0 голосов
/ 05 октября 2018

Пожалуйста, поделитесь ReminderService.class, у вас должны быть завершены настройки уведомлений, чтобы вызвать это место.

<receiver android:name=".ReminderService">
    <intent-filter>
        <action android:name="android.intent.action.BOOT_COMPLETED" />
    </intent-filter>
</receiver>

Мой метод запущен здесь, и он не принимает обслуживание за

<receiver android:name=".AlarmReceiver">
        <intent-filter>
            <action android:name="android.intent.action.BOOT_COMPLETED" />
            <action android:name="android.intent.action.TIME_SET" />
            <action android:name="android.intent.action.TIMEZONE_CHANGED" />

            <category android:name="android.intent.category.HOME" />
        </intent-filter>
    </receiver>
...