В следующем коде используется Alarmmanager с BroadcastReceiver, который поможет вам в достижении ваших потребностей.
В вашей деятельности:
Intent intent = new Intent(MainActivity.this, Receiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(MainActivity.this, REQUEST_CODE, intent, 0);
AlarmManager am = (AlarmManager)getSystemService(ALARM_SERVICE);
am.setRepeating(am.RTC_WAKEUP, System.currentTimeInMillis(), am.INTERVAL_DAY*7, pendingIntent);
System.currentTimeInMillis() — указывает, что будильник сработает в текущее время, вы можете передать постоянное время дня в миллисекундах.
Затем создайте класс Receiver примерно так:
public class Receiver extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
showNotification(context);
}
public void showNotification(Context context) {
Intent intent = new Intent(context, AnotherActivity.class);
PendingIntent pi = PendingIntent.getActivity(context, reqCode, intent, 0);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context)
.setSmallIcon(R.drawable.android_icon)
.setContentTitle("Title")
.setContentText("Some text");
mBuilder.setContentIntent(pi);
mBuilder.setDefaults(Notification.DEFAULT_SOUND);
mBuilder.setAutoCancel(true);
NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(reqCode, mBuilder.build());
}
}
Также вы должны зарегистрировать свой класс BroadcastReceiver в файле манифеста, как показано ниже. В вашем файле AndroidManifest.xml внутри тега
<receiver android:name="com.example.receivers.Receiver"></receiver>
Здесь «com.example.receivers.Receiver» — это мой пакет и имя получателя.
person
Prakash Jackson
schedule
28.11.2013
AlarmManager
и методsetRepeating()
с интервалом времениAlarmManger.DAY * 7
. - person Simon Dorociak   schedule 28.11.2013