3

我创建了一个广播接收器来挂钩日历 EVENT_REMINDER 并获取有关事件的信息

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    bReceiver = new HealtDroidBroadcastReceiver();
    IntentFilter inf = new IntentFilter("android.intent.action.EVENT_REMINDER");
    inf.addDataScheme("content");
    inf.addDataAuthority("com.android.calendar",null);
    registerReceiver(bReceiver,inf);
}

在广播接收器类中:

public void onReceive(Context context, Intent intent) { 
    Uri uri = intent.getData();
    String intentAction = intent.getAction();
    ContentResolver cr = context.getContentResolver();
    String sUri = uri.toString();
    Uri alertUri = Uri.parse(sUri);

    Cursor alertCursor = cr.query(alertUri, ALERT_PROJECTION,
            null /* selection */, null, null /* sort order */);

但是当 cr.query 时出现错误:

未知 URL 内容://com.android.calendar/1373401980000

4

1 回答 1

5

附加的 ID 不是 eventID,而是 alarmTime。在此处查看如何创建警报广播: scheduleAlarm()

您可以使用以下方法获取警报时间:

String alarmTime = uri.getLastPathSegment();

然后您可以使用它来查询 CalendarAlerts:

 String[] projection = new String[] {
          CalendarAlerts.EVENT_ID
  };

 String selection = CalendarContract.CalendarAlerts.ALARM_TIME + "=?";

 Cursor c = cr.query(CalendarContract.CalendarAlerts.CONTENT_URI_BY_INSTANCE, 
          projection, 
          selection, 
          new String[] {alarmTime},
          null);

...
于 2014-01-25T10:31:50.247 回答