如何在android中使用时间选择器创建每日自定义通知?

时间:2017-09-30 23:36:14

标签: android notifications alarmmanager android-notifications custom-notificaiton

我想要的: -

  • 制作一个应用程序,用户可以在给定时间内发出通知。
  • 它也应该在后台运行。
  • 我可以在SharedPreferences数据库上保存时间小时和分钟24小时格式,以便我可以再次覆盖时间。
  • 其他部分,如何不发出通知?

但我得到了什么

我做了这个,但我面临几个问题: -

  1. 我可以发出通知,但只有当我的应用处于运行状态时才会发出通知。
  2. 无法将变量Xhours和Xminute存储到SharedPrefrences DB中。
  3. 我的广播接收器类和通知类工作正常,但在应用未运行时无法获取通知。 访问此:> link查看截图
  4.   

    请帮助,谢谢。

    我的settings_aztro.java活动

    cat file1.csv file2.csv > file3.csv
    hdfs dfs -put file3.csv /path/to/input
    

    我的接收者课程

        public void TimePickerDialog() {
    
    
            TimeTv_settings.setOnClickListener(new View.OnClickListener() {
                @SuppressWarnings("deprecation")
                @Override
                public void onClick(View v) {
    
                    showDialog(DIALOG_ID);
                }
            });
    
        }
    
        @SuppressWarnings("deprecation")
        @Override
        protected Dialog onCreateDialog(int id) {
            if (id == DIALOG_ID) {
                return new TimePickerDialog(settings_aztro.this, mTimePickerListener, Xhour, Xminute, false);
            } else {
                Toast.makeText(this, "Unable to locate time picker", Toast.LENGTH_SHORT).show();
                return null;
    
            }
    
        }
    
    
    
    
        protected TimePickerDialog.OnTimeSetListener mTimePickerListener = new TimePickerDialog.OnTimeSetListener() {
            @Override
            public void onTimeSet(TimePicker view, int hourOfDay, int minute)
            {
                // This will give 24hr value for the notification
                Xhour = hourOfDay;
                Xminute = minute;
    
                // This will show the 12 hour time on the TextView
                YHour = hourOfDay;
                Yminute = minute;
    
    
                if (YHour == 0) {
                    YHour += 12;
                    format = "AM";
                } else if (YHour == 12) {
                    format = "PM";
                } else if (YHour > 12) {
                    YHour -= 12;
                    format = "PM";
                } else {
                    format = "AM";
                }
                SharedPreferences sharedPreferences = getSharedPreferences("timeinfo", Context.MODE_ENABLE_WRITE_AHEAD_LOGGING);
                SharedPreferences.Editor editor = sharedPreferences.edit();
                editor.putInt("keyHour",Xhour);
                editor.putInt("keyMinute",Xminute);
                editor.commit();
                TimeTv_settings.setText(YHour + ":" + Yminute + " " + format);
                if(NotifyMe==true)
                {
                    SharedPreferences xsharedPreferences = getSharedPreferences("timeinfo", Context.MODE_ENABLE_WRITE_AHEAD_LOGGING);
                    savedHour = xsharedPreferences.getInt("keyHour", Integer.parseInt(""));
                    savedMinute = xsharedPreferences.getInt("keyMinute",Integer.parseInt(""));
    
                    Toast.makeText(settings_aztro.this, "YHour: "+YHour+"Ymin: "+Yminute+" "+format+"\tNotify: "+NotifyMe, Toast.LENGTH_SHORT).show();
                    generateCustomNotification();
                }
                else
                {
                    NotifyMe = false;
    
                }
            }
        };
    
    public void generateCustomNotification()
    {
    
        CustomCalendar.set(Calendar.HOUR_OF_DAY,savedHour);
        CustomCalendar.set(Calendar.MINUTE,savedMinute);
        CustomCalendar.set(Calendar.SECOND,0);
    
        Intent CallReciverIntent = new Intent(settings_aztro.this,NotificationBReceiver.class);
        PendingIntent pIntent = PendingIntent.getBroadcast(settings_aztro.this,0,CallReciverIntent,PendingIntent.FLAG_UPDATE_CURRENT);
    
        AlarmManager alarmManager = (AlarmManager) settings_aztro.this.getSystemService(ALARM_SERVICE);
        alarmManager.set(AlarmManager.RTC_WAKEUP,CustomCalendar.getTimeInMillis(),pIntent);
        Log.d("samay","Hour-> "+Xhour+" Minute-> "+Xminute+"Time in milis -> "+CustomCalendar.getTimeInMillis());
    
    }
    
    
    public void saveNotificationTime()
    {
    
    }
    
    
    
        @Override
        public void onClick(View v)
        {
    
            switch (v.getId())
            {
                case R.id.About_settingsTv2:
                    Toast.makeText(this, "C", Toast.LENGTH_SHORT).show();
                    generateCustomNotification();
                    Log.d("Timeqq","Hour-> "+Xhour+" Minute-> "+Xminute);
                    Xhour = 0;Xminute=0;
                    break;
    
                case R.id.FacebookSignin_settings:
    
                    Log.d("newtime","hour= "+Xhour+" minute= "+Xminute+"Time in milis -> "+CustomCalendar.getTimeInMillis()+"Notify "+NotifyMe);
                    Log.d("newtime","hour= "+savedHour+" minute= "+savedMinute+"Time in milis -> "+CustomCalendar.getTimeInMillis()+"Notify "+NotifyMe);
                    break;
    
    
            }
    
    
        }
    
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
        {
            if(switch_settings.isChecked())
            {
                NotifyMe = true;
                TimeRLayout.setVisibility(View.VISIBLE);
                Toast.makeText(this, "On", Toast.LENGTH_SHORT).show();
                Toast.makeText(this, "Hr -> "+Xhour+"Min -> "+Xminute, Toast.LENGTH_SHORT).show();
                Log.d("Waqt","Hour-> "+Xhour+" Minute-> "+Xminute);
    
            }
            else if(!switch_settings.isChecked())
            {
                NotifyMe = false;
                Toast.makeText(this, "Off", Toast.LENGTH_SHORT).show();
                if(NotifyMe==false)
                {
                    TimeRLayout.setVisibility(View.GONE);
    
                }
                else
                {
    
                }
    
            }
        }
    

    清单文件

     public class NotificationBReceiver extends BroadcastReceiver
    {
        int MID = 98;
        @Override
        public void onReceive(Context context, Intent intent)
        {
    
    long when = System.currentTimeMillis();
            NotificationManager nManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
            Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
            Intent notificationIntent = new Intent(context, list_of_signs.class);
            notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    
            PendingIntent pendingIntent = PendingIntent.getActivity(context, 10,
                    notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
    
            NotificationCompat.Builder NotificationBuilder =  new NotificationCompat.Builder(context)
                    .setSmallIcon(R.drawable.aztro2)
                    .setSound(defaultSoundUri)
                    .setContentTitle("Hello! Check Your Today's Horoscope.")
                    .setAutoCancel(true)
                    .setWhen(when)
                    .setContentIntent(pendingIntent)
                    .setVibrate(new long[]{1000, 1000, 1000, 1000, 1000});
            nManager.notify(MID, NotificationBuilder.build());
            MID++;;
    

1 个答案:

答案 0 :(得分:1)

好的,这是Alarm Manager的模式。在此实现中,没有使用静态定义的广播接收器,也不应该用于调度任务,根据Android O最佳实践,简而言之, FORGET关于广播接收器如果您想要背景通知。

1)创建意向服务(ex.ReminderAlarmService)以发布您的通知。此服务将以alarmManager使用的待处理意图启动。 onHandleIntent应该创建通知并显示它:ex。

ReminderAlarmService.class
 @Override
    protected void onHandleIntent(Intent intent) {

        //..... Create your notification here
        manager.notify(NOTIFICATION_ID, notification);

        //..... Persist your data here in sharedPreferences here too, with or without user action
    }

2)在服务中,创建一个公共util方法来返回“操作”待定意图以启动上述意图服务(也称为深层链接意图)ex。

public static PendingIntent getReminderPendingIntent(Context context, Uri uri) {
    Intent action = new Intent(context, ReminderAlarmService.class);
    action.setData(uri);
    return PendingIntent.getService(context, 0, action, PendingIntent.FLAG_UPDATE_CURRENT);
}

3)创建一个util类以获取警报管理器的实例,使用它来设置时间和使用上述方法创建的“操作”意图。操作意图启动意图服务,该服务执行onHandleIntent中的任何操作。离。

public class AlarmScheduler {

    public static void scheduleAlarm(Context context, long alarmTime, Uri reminderTask) {
        AlarmManager manager = AlarmManagerProvider.getAlarmManager(context);

        PendingIntent operation =
                ReminderAlarmService.getReminderPendingIntent(context, reminderTask);

        manager.setExact(AlarmManager.RTC, time, operation);
    }
}

总体设计流程:

  1. AlarmManager使用待定意图安排警报,以便从时间选择器作为参数启动您的服务。
  2. 当时间到达时,您的意图服务将由待定意图启动
  3. 在意向服务中,构建并显示您的通知
  4. 当您的意向服务完成作业时,服务会自行停止,清理。