服务器之家

服务器之家 > 正文

Android编程设置提醒事件的方法

时间:2021-05-14 15:24     来源/作者:nosxcy

本文实例讲述了Android编程设置提醒事件的方法。分享给大家供大家参考,具体如下:

1、启动service

?
1
2
3
Intent intent = new Intent(this,AutoTaskService.class);
intent.putExtra("reminder_event", reminderModel);
startService(intent);

2、service file

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
public class AutoTaskService extends Service {
  private ReminderModel mReminderModel = null;
  @Override
  public IBinder onBind(Intent intent) {
    return null;
  }
  @Override
  public void onCreate() {
    super.onCreate();
  }
  @Override
  public void onDestroy() {
    super.onDestroy();
  }
  @Override
  public void onStart(Intent intent, int startId) {
    if(intent == null)
      return;
    if(intent.hasExtra("reminder_event")) {
      mReminderModel = (ReminderModel) intent.getSerializableExtra("reminder_event");
    }
    setReminderEvent();
    super.onStart(intent, startId);
  }
  /**
   * set reminder event
   */
  private void setReminderEvent() {
    if(mReminderModel == null)
      return;
    if(TextUtils.isEmpty(mReminderModel.reminderStartTime))
      return;
    if(TextUtils.isEmpty(mReminderModel.reminderTime))
      return;
    Calendar cal = getCalendarFromDate(mReminderModel.reminderStartTime);
    String[] array = mReminderModel.reminderTime.split("-");
    for(int i = 0; i < array.length; i++) {
      if(i == 0) {
        cal.set(Calendar.HOUR_OF_DAY, Integer.parseInt(array[0]));
      } else if (i == 1) {
        cal.set(Calendar.MINUTE, Integer.parseInt(array[1]));
      }
    }
    cal.set(Calendar.SECOND, 0);
    Intent intent = new Intent(AutoTaskService.this, AlarmReceiver.class);
    if(!TextUtils.isEmpty(mReminderModel.reminderPath)) {
      intent.putExtra("reminder_pic_path", mReminderModel.reminderPath);
    }
    PendingIntent pi = PendingIntent.getBroadcast(this, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);
   // 获取AlarmManager对象
    AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
   // AlarmManager.RTC_WAKEUP休眠时会运行,如果是AlarmManager.RTC,在休眠时不会运行
    am.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), pi);
  }
  /**
   * @param date format is 2012-9-18
   * @return Calendar value is after set date's value
   */
  private Calendar getCalendarFromDate(final String date) {
    int year = 0;
    int month = 0;
    int day = 0;
    try {
      String[] array = date.split("-");
      int[] arrayInt = new int[array.length];
      for (int i = 0; i < array.length; i++) {
        arrayInt[i] = Integer.parseInt(array[i]);
        if(i == 0) {
          year = arrayInt[0];
        } else if(i == 1){
          month = arrayInt[1];
        } else if(i == 2){
          day = arrayInt[2];
        }
      }
    } catch (Exception e) {
      e.printStackTrace();
    }
    Calendar cal = Calendar.getInstance();
    if(year > 0 && month >= 0 && day >= 0) {
      cal.set(year, month - 1, day);
    }
    return cal;
  }
}

3、定时接收器

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
public class AlarmReceiver extends BroadcastReceiver {
  @Override
  public void onReceive(Context context, Intent intent) {
    String path = null;
    if(intent.hasExtra("reminder_pic_path")) {
      path = intent.getStringExtra("reminder_pic_path");
    }
    Log.i("======> AlarmReceiver", path);
    // 启动通知activity
    Intent it = new Intent(context, FingerPaint.class);
    it.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    if(!TextUtils.isEmpty(path)) {
      it.putExtra("reminder_pic_path", path);
    }
    context.startActivity(it);
  }
}

希望本文所述对大家Android程序设计有所帮助。

标签:

相关文章

热门资讯

2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全
2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全 2019-12-26
yue是什么意思 网络流行语yue了是什么梗
yue是什么意思 网络流行语yue了是什么梗 2020-10-11
背刺什么意思 网络词语背刺是什么梗
背刺什么意思 网络词语背刺是什么梗 2020-05-22
苹果12mini价格表官网报价 iPhone12mini全版本价格汇总
苹果12mini价格表官网报价 iPhone12mini全版本价格汇总 2020-11-13
2021德云社封箱演出完整版 2021年德云社封箱演出在线看
2021德云社封箱演出完整版 2021年德云社封箱演出在线看 2021-03-15
返回顶部