android 特定日期的通知

Notification on specific day on android

我正在寻找可以在我不知道的日期和时间发出通知的代码,因此用户将确定日期,我必须在这一天到来时发送通知,如果用户点击重复必须重复,

我看到很多代码只是为了时间而不是日期 任何人都可以帮助我修改它们以便我可以找到我正在寻找的东西吗?

这是我得到的代码。

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);
    }
}

报警接收器

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);
    }
}

我认为你可以使用以下内容。

cal.set(日历。DAY_OF_MONTH, 你的日子);

根据用户输入创建这样的 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 文档中所述:

Schedule a repeating alarm that has inexact trigger time requirements; for example, an alarm that repeats every hour, but not necessarily at the top of every hour.

例如每天在指定时间(日历)重复闹钟,

// Milliseconds of 24 hours
long interval = 24 * 60 * 60 * 1000;
alarmManager.setInexactRepeating(AlarmManager.RTC_WAKEUP,
                calendar.getTimeInMillis(), interval , broadcast);

并且setExact()不会重复报警。

Schedule an alarm to be delivered precisely at the stated time.


带有通知和警报管理的完整代码。

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...
    }
}