【问题标题】:How to set reminders in android using background service?如何使用后台服务在android中设置提醒?
【发布时间】:2014-04-09 14:13:25
【问题描述】:

您好,我正在开发一个应用程序,我从用户那里获取提醒详细信息作为提醒日期和提醒名称,并将它们存储在数据库中。现在我想知道如何通过后台服务启动提醒?或者有没有其他方法,如果是请告诉我。我该怎么办?请帮我。建议一些东西或教程将是个好主意。 提前致谢!

【问题讨论】:

    标签: android service notifications broadcastreceiver reminders


    【解决方案1】:

    为此,您需要两件事

    1. 警报管理器:设置通知或警报的时间(每天、每周)
    2. 服务:在警报管理器响起时启动通知

    在您要设置提醒的活动课程中执行此操作

    Intent myIntent = new Intent(this , NotifyService.class);     
    AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
    pendingIntent = PendingIntent.getService(this, 0, myIntent, 0);
    
    //get time from database and initialise the variables.
    int minute;
    int hour;
    
    Calendar calendar = Calendar.getInstance();
    calendar.set(Calendar.SECOND, 0);
    calendar.set(Calendar.MINUTE, minute);
    calendar.set(Calendar.HOUR, hour);
    calendar.set(Calendar.AM_PM, Calendar.AM);    //set accordingly
    calendar.add(Calendar.DAY_OF_YEAR, 0);
    
    alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(),AlarmManager.INTERVAL_DAY, pendingIntent);
    

    这将在您设定的时间每天触发警报。

    现在,您应该创建一个 Service 作为 NotifyService 并将以下代码放入其 onCreate() 中:

    @Override
    public void onCreate() {
        NotificationManager mNM = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
        Notification notification = new Notification(R.drawable.notification_icon, "Notify Alarm strart", System.currentTimeMillis());
        Intent myIntent = new Intent(this , MyActivity.class);     
        PendingIntent contentIntent = PendingIntent.getActivity(this, 0, intent, 0);
        notification.setLatestEventInfo(this, "Notify label", "Notify text", contentIntent);
        mNM.notify(NOTIFICATION, notification);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-04-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多