我正在寻找可以通过我不知道的日期和时间发出通知的代码,因此用户将确定当天,我必须在这一天到来时发送通知,如果用户点击重复必须重复,
我看到很多代码只是时间不是白天 任何人都可以帮我修改它们,这样我就能达到我想要的目的吗?
这是我得到的代码。
MainActivity
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
Intent notificationIntent = new Intent("android.media.action.DISPLAY_NOTIFICATION");
notificationIntent.addCategory("android.intent.category.DEFAULT");
PendingIntent broadcast = PendingIntent.getBroadcast(this, 100, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
Calendar cal = Calendar.getInstance();
cal.add(Calendar.SECOND, 15);
alarmManager.setExact(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), broadcast);
}
}
NotificationActivity
public class NotificationActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.notification_activity);
}
}
AlarmReceiver
public class AlarmReceiver extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
Intent notificationIntent = new Intent(context, NotificationActivity.class);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
stackBuilder.addParentStack(NotificationActivity.class);
stackBuilder.addNextIntent(notificationIntent);
PendingIntent pendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder builder = new NotificationCompat.Builder(context);
Notification notification = builder.setContentTitle("Demo App Notification")
.setContentText("New Notification From Demo App..")
.setTicker("New Message Alert!")
.setSmallIcon(R.mipmap.ic_launcher)
.setContentIntent(pendingIntent).build();
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0, notification);
}
}
答案 0 :(得分:0)
我认为您可以使用以下内容。
cal.set(日历。 DAY_OF_MONTH ,你的日子);
答案 1 :(得分:0)
根据用户输入创建这样的Calendar
实例:
Calendar calendar = Calendar.getInstance();
// Settings calendar for 01/05/2016 12:33:00 AM
calendar.set(Calendar.YEAR, 2016);
calendar.set(Calendar.MONTH, 4); // January has value 0
calendar.set(Calendar.DAY_OF_MONTH, 1);
calendar.set(Calendar.HOUR_OF_DAY, 12);
calendar.set(Calendar.MINUTE, 33);
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.AM_PM, Calendar.AM );
要重复使用setInexactRepeating()
在android文档中:
安排具有不准确触发时间要求的重复警报; 例如,每小时重复一次的警报,但不一定是 每小时的最高点。
例如,要在指定时间(日历)中每天重复闹铃,
// Milliseconds of 24 hours
long interval = 24 * 60 * 60 * 1000;
alarmManager.setInexactRepeating(AlarmManager.RTC_WAKEUP,
calendar.getTimeInMillis(), interval , broadcast);
并且setExact()
不会重复警报。
安排在规定时间准确交付警报。
完整的代码,包括通知和警报管理。
public class MyAlarmReceiver extends BroadcastReceiver
{
// The app's AlarmManager, which provides access to the system alarm services.
private AlarmManager alarmMgr = null;
private NotificationManager mNotificationManager;
NotificationCompat.Builder builder;
public TankAlarmReceiver()
{
}
@Override
public void onReceive(Context context, Intent intent)
{
mNotificationManager = (NotificationManager)
context.getSystemService(Context.NOTIFICATION_SERVICE);
Intent intent = new Intent(context, NotificationActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP);
// If you use intent extras, remember to call PendingIntent.getActivity() with the flag
// PendingIntent.FLAG_UPDATE_CURRENT, otherwise the same extras will be reused for every
// notification.
PendingIntent contentIntent = PendingIntent.getActivity(context, deviceId.hashCode(), intent,
PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(context)
.setSmallIcon(getNotificationIcon())
.setContentTitle(title)
.setStyle(new NotificationCompat.BigTextStyle()
.bigText(msg))
.setContentText(msg);
mBuilder.setContentIntent(contentIntent);
mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
}
/**
* Sets a repeating alarm that runs once a day at approximately 8:30 a.m. When the
* alarm fires, the app broadcasts an Intent to this WakefulBroadcastReceiver.
*
* @param settingsModel
*/
public void setAlarm(Context context, TankSettingsModel settingsModel)
{
if(alarmMgr == null)
alarmMgr = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
// Initialize calendar and PendingIntent here
long alarmIntervalInMin = 1 * 60 * 60 * 1000;
long triggerAtMillis = calendar.getTimeInMillis();
alarmMgr.setInexactRepeating(AlarmManager.RTC_WAKEUP,
triggerAtMillis, alarmIntervalInMin, pendingIntent);
// Enable {@code SampleBootReceiver} to automatically restart the alarm when the
// device is rebooted.
ComponentName receiver = new ComponentName(context, MyBootReceiver.class);
PackageManager pm = context.getPackageManager();
pm.setComponentEnabledSetting(receiver,
PackageManager.COMPONENT_ENABLED_STATE_ENABLED,
PackageManager.DONT_KILL_APP);
}
/**
* Cancels the alarm.
*
* @param deviceId
*/
public void cancelAlarm(Context context, String deviceId)
{
// Code for cancelling the alarm comes here...
}
}