如何设置提醒?

时间:2018-06-08 10:09:27

标签: android

我已尝试过stackoverflow的许多解决方案,但它们都没有工作......我想设置重复警报以获得待处理账单的通知......

到目前为止,我已尝试过这个,

@Override
public void onReceive(Context context, Intent intent) {
    Toast.makeText(context, "Alarm worked1111.", Toast.LENGTH_LONG).show();


    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        CharSequence name = "dddd";
        String description = "aaaa";
        int importance = NotificationManager.IMPORTANCE_DEFAULT;
        NotificationChannel channel = new NotificationChannel("a", name, importance);
        channel.setDescription(description);

        NotificationManager notificationManager =context.getSystemService(NotificationManager.class);
        notificationManager.createNotificationChannel(channel);
    }

     intent = new Intent(context, PendingSalesPurchaseActivity.class);
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
    PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);

    NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context, "a")
            .setSmallIcon(R.drawable.ic_search_commit)
            .setContentTitle("My notification")
            .setContentText("Hello World!")
            .setPriority(NotificationCompat.PRIORITY_DEFAULT)
                         .setContentIntent(pendingIntent)
            .setAutoCancel(true);
}
  

MyBroadcastReceiver类

{}

2 个答案:

答案 0 :(得分:0)

添加以下代码以设置通知。

    NotificationManager notificationManager =
            (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    notificationManager.notify(0, notificationBuilder.build());

每天重复闹铃,您需要使用setRepeating()

 alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pendingIntent );

当然,如果您不需要在确切的时间重复闹钟,那么最好使用setInexactRepeating()

 alarmManager.setInexactRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pendingIntent );
  

安排具有不准确触发时间要求的重复警报;   例如,每小时重复一次的警报,但不一定是   每小时的最高点。这些警报比电源更节能   传统上由setRepeating(int, long, long, PendingIntent)提供的严格重复,因为系统可以调整警报和#39}。交货   时间使它们同时发射,避免唤醒设备   从睡眠中得到的不必要。

答案 1 :(得分:0)

您需要在同一时间使用此代码进行日常通知。

  public static void setReminder(Context context, int hour, int min)
    {

        Calendar calendar = Calendar.getInstance();
        Calendar setcalendar = Calendar.getInstance();
        setcalendar.set(Calendar.HOUR_OF_DAY, hour);
        setcalendar.set(Calendar.MINUTE, min);
        setcalendar.set(Calendar.SECOND, 0);

        if(setcalendar.before(calendar))
            setcalendar.add(Calendar.DATE,1);

        // Enable a receiver

        ComponentName receiver = new ComponentName(context, cls);//Class Name from where it trigger
        PackageManager pm = context.getPackageManager();

        pm.setComponentEnabledSetting(receiver,
                PackageManager.COMPONENT_ENABLED_STATE_ENABLED,
                PackageManager.DONT_KILL_APP);


        Intent intent1 = new Intent(context, cls);
        PendingIntent pendingIntent = PendingIntent.getBroadcast(context, DAILY_REMINDER_REQUEST_CODE, intent1, PendingIntent.FLAG_UPDATE_CURRENT);
        AlarmManager am = (AlarmManager) context.getSystemService(ALARM_SERVICE);
        am.setInexactRepeating(AlarmManager.RTC_WAKEUP, setcalendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pendingIntent);

    }