你需要写一个IntentService
第一的。这是一个示例,您可以编写代码来显示通知processNotification
功能。
public class NotificationIntentService extends IntentService {
private static final String ACTION_START = "ACTION_START";
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;
}
@Override
protected void onHandleIntent(Intent intent) {
try {
String action = intent.getAction();
if (ACTION_START.equals(action))
processNotification();
} finally {
WakefulBroadcastReceiver.completeWakefulIntent(intent);
}
}
private void processNotification() {
Intent resultIntent=new Intent(this, MainActivity.class);
PendingIntent pIntent = PendingIntent.getActivity(this, 0, resultIntent, 0);
Notification nBuilder = new Notification.Builder(this)
.setContentTitle("Don't miss! ")
.setTicker("Notification!")
.setContentIntent(pIntent)
.setDefaults(Notification.DEFAULT_SOUND)
.setAutoCancel(true)
.setSmallIcon(R.drawable.my_logo)
.setContentText("7 days left till your appointment...")
.build();
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
nBuilder.flags |=Notification.FLAG_AUTO_CANCEL;
notificationManager.notify(1, nBuilder);
}
}
然后创建一个NotificationEventReceiver
public class NotificationEventReceiver extends WakefulBroadcastReceiver {
private static final String ACTION_START_NOTIFICATION_SERVICE = "ACTION_START_NOTIFICATION_SERVICE";
public static void setupAlarm(Context context, long interval) {
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
PendingIntent alarmIntent = getStartPendingIntent(context);
alarmManager.setRepeating(AlarmManager.RTC, System.currentTimeMillis(), interval, alarmIntent);
}
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
Intent serviceIntent = null;
if (ACTION_START_NOTIFICATION_SERVICE.equals(action)) {
serviceIntent = NotificationIntentService.createIntentStartNotificationService(context);
}
if (serviceIntent != null) {
startWakefulService(context, serviceIntent);
}
}
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);
}
}
And the NotificationServiceStarterReceiver
public final class NotificationServiceStarterReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
long interval = getIntent().getLongExtra("alarm_interval", 0);
NotificationEventReceiver.setupAlarm(context, interval);
}
}
将这些添加到您的AndroidManifest.xml
inside <application>
tag
<service
android:name="YourPackage.NotificationIntentService"
android:enabled="true"
android:exported="false" />
<receiver android:name="YourPackage.BroadcastReceiver.NotificationEventReceiver" />
<receiver android:name="YourPackage.BroadcastReceiver.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
您可以致电setupAlarm()
inside onCreate
功能。
NotificationEventReceiver.setupAlarm(getApplicationContext(), interval);
你需要添加WAKE_LOCK
您的清单中的许可。
<uses-permission android:name="android.permission.WAKE_LOCK" />
在这里你看到你可以通过interval
要显示的下一个通知。使用interval
明智地。您可以考虑将约会的当前状态保存在数据库中,然后在必要时通过传递下一个警报的适当间隔来触发警报。就是这个想法。
Update
因此,就您而言,您不想在用户注销时显示通知。所以在这种情况下,你可以考虑保留一个SharedPreference
存储登录状态。您可以致电processNotification
基于存储值的函数。
所以伪代码可能看起来像这样。
if(pref.getBoolean("login_status", false)) {
// If the login status is true, process the notification
processNotification();
} else {
// Do nothing
}