Ответ 1
Вопрос относительно старый, но я надеюсь, что этот пост все же может быть релевантен для других.
TL; DR: используйте AlarmManager для планирования задачи, используйте IntentService, см. пример кода здесь;
Что это за тест-приложение (и инструкция):
Простое приложение helloworld, которое отправляет вам уведомление каждые 2 часа. Нажатие на уведомление - открывает вторичную активность в приложении; удаление дорожек уведомлений.
Когда вы должны использовать его:
Как только вам нужно выполнить какую-либо задачу по расписанию. Мой собственный случай: один раз в день я хочу получать новый контент с сервера, составлять уведомление на основе содержимого, которое я получил, и показывать его пользователю.
Что делать:
-
Сначала создайте 2 действия: MainActivity, которая запускает службу уведомлений и NotificationActivity, которая будет запущена, щелкнув уведомление:
activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:padding="16dp"> <Button android:id="@+id/sendNotifications" android:onClick="onSendNotificationsButtonClick" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Start Sending Notifications Every 2 Hours!" /> </RelativeLayout>
MainActivity.java
public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } public void onSendNotificationsButtonClick(View view) { NotificationEventReceiver.setupAlarm(getApplicationContext()); } }
и NotificationActivity - любая случайная деятельность, которую вы можете придумать. NB! Не забудьте добавить оба действия в AndroidManifest.
-
Затем создайте
WakefulBroadcastReceiver
широковещательный приемник, я назвал NotificationEventReceiver в коде выше.Здесь мы настроим
AlarmManager
для запускаPendingIntent
каждые 2 часа (или с любой другой частотой) и укажите обработанные действия для этого намерения в методеonReceive()
. В нашем случае - бодрствуйте запуститеIntentService
, который мы укажем на последующих шагах. ЭтотIntentService
будет генерировать уведомления для нас.Кроме того, этот приемник будет содержать некоторые вспомогательные методы, такие как создание PendintIntents, которые мы будем использовать позже
NB1! Поскольку я использую
WakefulBroadcastReceiver
, мне нужно добавить в свой манифест дополнительное разрешение:<uses-permission android:name="android.permission.WAKE_LOCK" />
NB2! Я использую его в бодрствовавшей версии широковещательного приемника, так как хочу, чтобы устройство не возвращалось спать во время моей операции
IntentService
. В привет-мире это не так важно (у нас нет долгой работы в нашем сервисе, но представьте себе, если вам нужно извлечь некоторые относительно огромные файлы с сервера во время этой операции). Подробнее об устройстве Awake здесь.NotificationEventReceiver.java
public class NotificationEventReceiver extends WakefulBroadcastReceiver { private static final String ACTION_START_NOTIFICATION_SERVICE = "ACTION_START_NOTIFICATION_SERVICE"; private static final String ACTION_DELETE_NOTIFICATION = "ACTION_DELETE_NOTIFICATION"; private static final int NOTIFICATIONS_INTERVAL_IN_HOURS = 2; public static void setupAlarm(Context context) { AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE); PendingIntent alarmIntent = getStartPendingIntent(context); alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, getTriggerAt(new Date()), NOTIFICATIONS_INTERVAL_IN_HOURS * AlarmManager.INTERVAL_HOUR, alarmIntent); } @Override public void onReceive(Context context, Intent intent) { String action = intent.getAction(); Intent serviceIntent = null; if (ACTION_START_NOTIFICATION_SERVICE.equals(action)) { Log.i(getClass().getSimpleName(), "onReceive from alarm, starting notification service"); serviceIntent = NotificationIntentService.createIntentStartNotificationService(context); } else if (ACTION_DELETE_NOTIFICATION.equals(action)) { Log.i(getClass().getSimpleName(), "onReceive delete notification action, starting notification service to handle delete"); serviceIntent = NotificationIntentService.createIntentDeleteNotification(context); } if (serviceIntent != null) { startWakefulService(context, serviceIntent); } } private static long getTriggerAt(Date now) { Calendar calendar = Calendar.getInstance(); calendar.setTime(now); //calendar.add(Calendar.HOUR, NOTIFICATIONS_INTERVAL_IN_HOURS); return calendar.getTimeInMillis(); } private static PendingIntent getStartPendingIntent(Context context) { Intent intent = new Intent(context, NotificationEventReceiver.class); intent.setAction(ACTION_START_NOTIFICATION_SERVICE); return PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT); } public static PendingIntent getDeleteIntent(Context context) { Intent intent = new Intent(context, NotificationEventReceiver.class); intent.setAction(ACTION_DELETE_NOTIFICATION); return PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT); } }
-
Теперь создайте
IntentService
для создания уведомлений.Здесь мы указываем
onHandleIntent()
, который является ответом на намерение NotificationEventReceiver, которое мы передали в методеstartWakefulService
.Если это действие "Удалить" - мы можем его зарегистрировать в нашей аналитике, например. Если он запустит назначение уведомления, то с помощью
NotificationCompat.Builder
мы создаем новое уведомление и покажем его с помощьюNotificationManager.notify
. При составлении уведомления мы также устанавливаем ожидающие намерения для кликов и удаления действий. Довольно легко.NotificationIntentService.java
public class NotificationIntentService extends IntentService { private static final int NOTIFICATION_ID = 1; private static final String ACTION_START = "ACTION_START"; private static final String ACTION_DELETE = "ACTION_DELETE"; public NotificationIntentService() { super(NotificationIntentService.class.getSimpleName()); } public static Intent createIntentStartNotificationService(Context context) { Intent intent = new Intent(context, NotificationIntentService.class); intent.setAction(ACTION_START); return intent; } public static Intent createIntentDeleteNotification(Context context) { Intent intent = new Intent(context, NotificationIntentService.class); intent.setAction(ACTION_DELETE); return intent; } @Override protected void onHandleIntent(Intent intent) { Log.d(getClass().getSimpleName(), "onHandleIntent, started handling a notification event"); try { String action = intent.getAction(); if (ACTION_START.equals(action)) { processStartNotification(); } if (ACTION_DELETE.equals(action)) { processDeleteNotification(intent); } } finally { WakefulBroadcastReceiver.completeWakefulIntent(intent); } } private void processDeleteNotification(Intent intent) { // Log something? } private void processStartNotification() { // Do something. For example, fetch fresh data from backend to create a rich notification? final NotificationCompat.Builder builder = new NotificationCompat.Builder(this); builder.setContentTitle("Scheduled Notification") .setAutoCancel(true) .setColor(getResources().getColor(R.color.colorAccent)) .setContentText("This notification has been triggered by Notification Service") .setSmallIcon(R.drawable.notification_icon); PendingIntent pendingIntent = PendingIntent.getActivity(this, NOTIFICATION_ID, new Intent(this, NotificationActivity.class), PendingIntent.FLAG_UPDATE_CURRENT); builder.setContentIntent(pendingIntent); builder.setDeleteIntent(NotificationEventReceiver.getDeleteIntent(this)); final NotificationManager manager = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE); manager.notify(NOTIFICATION_ID, builder.build()); } }
-
Почти сделано. Теперь я также добавляю широковещательный приемник для событий BOOT_COMPLETED, TIMEZONE_CHANGED и TIME_SET, чтобы переустановить мой AlarmManager, как только устройство было перезагружено или изменился часовой пояс (например, пользователь перелетел из США в Европу, и вы не хотите, чтобы уведомление всплывало посреди ночи, но был липким по местному времени:-)).
NotificationServiceStarterReceiver.java
public final class NotificationServiceStarterReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { NotificationEventReceiver.setupAlarm(context); } }
-
Нам также необходимо зарегистрировать все наши услуги, широковещательные приемники в AndroidManifest:
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="klogi.com.notificationbyschedule"> <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" /> <uses-permission android:name="android.permission.WAKE_LOCK" /> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:supportsRtl="true" android:theme="@style/AppTheme"> <activity android:name=".MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <service android:name=".notifications.NotificationIntentService" android:enabled="true" android:exported="false" /> <receiver android:name=".broadcast_receivers.NotificationEventReceiver" /> <receiver android:name=".broadcast_receivers.NotificationServiceStarterReceiver"> <intent-filter> <action android:name="android.intent.action.BOOT_COMPLETED" /> <action android:name="android.intent.action.TIMEZONE_CHANGED" /> <action android:name="android.intent.action.TIME_SET" /> </intent-filter> </receiver> <activity android:name=".NotificationActivity" android:label="@string/title_activity_notification" android:theme="@style/AppTheme.NoActionBar"/> </application> </manifest>
Что это!
Исходный код этого проекта можно найти здесь. Надеюсь, вы найдете эту статью полезной.