我想在android上运行通知取决于用户插入的日期和时间 我知道如何从活动开始通知,但我认为这不是我想要的, 任何人都可以帮我一些代码吗?
答案 0 :(得分:0)
以下是您如何实现这一目标,您必须在用户选择的特定时间添加闹钟
public class TimerReceiverSyncInterval extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Log.d("TAG", "Sync OnReceive--");
scheduleAlarms(context);
context.startService(new Intent(context, NotificationServiceSyncInterval.class));
}
public static void scheduleAlarms(Context paramContext) {
Calendar calendar = Calendar.getInstance();
String[] splited = Utility.mPrefs.getSyncTime().split("\\s+");
if (Utility.mPrefs.getSyncTime().contains("Minute")) {
calendar.set(Calendar.MINUTE, Integer.parseInt(splited[0]));
} else if (Utility.mPrefs.getSyncTime().contains("Hour")) {
calendar.set(Calendar.HOUR, Integer.parseInt(splited[0]));
}
AlarmManager localAlarmManager = (AlarmManager) paramContext.getSystemService(Context.ALARM_SERVICE);
PendingIntent localPendingIntent = PendingIntent.getService(paramContext, 0,
new Intent(paramContext, NotificationServiceSyncInterval.class), PendingIntent.FLAG_UPDATE_CURRENT);
if (Utility.mPrefs.getSyncTime().contains("Minute")) {
localAlarmManager.setRepeating(AlarmManager.RTC, calendar.getTimeInMillis(),
Integer.parseInt(splited[0]) * (60 * 1000), localPendingIntent);
} else if (Utility.mPrefs.getSyncTime().contains("Hour")) {
localAlarmManager.setRepeating(AlarmManager.RTC, calendar.getTimeInMillis(),
Integer.parseInt(splited[0]) * (60 * 60 * 1000), localPendingIntent);
}
}
}
用于处理/呼叫通知
public class NotificationServiceSyncInterval extends IntentService {
public NotificationServiceSyncInterval() {
super("Sync Tracker Online");
}
public NotificationServiceSyncInterval(String paramString) {
super(paramString);
}
@Override
protected void onHandleIntent(Intent intent) {
Log.d("TAG", "Sync Handler call");
callNotification();
}
public static void callNotification() {
//Notification code
}
}