Unable to receive android.intent.action.EVENT_REMI

2019-01-23 08:19发布

问题:

I would like to write an application that is triggered when a calendar reminder occurs. I realize there is no officially documented way of doing this, but I have seen in the log that when my calendar alarm goes off on my phone (Droid X), AlertReceiver indicates that it has received an android.intent.action.EVENT_REMINDER:

01-03 11:03:00.029 D 1523 AlertReceiver onReceive: a=android.intent.action.EVENT_REMINDER Intent { act=android.intent.action.EVENT_REMINDER dat=content://com.android.calendar/129407058000 flg=0x4 cmp=com.android.calendar/.AlertReceiver (has extras) }

So, I set up a simple BroadcastReceiver:

package com.eshayne.android;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;

public class CalendarTest extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
       android.util.Log.i("CalendarTest", "CalendarTest.onReceive called!");
    }
}

with this manifest:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.eshayne.android"
    android:versionCode="1"
    android:versionName="1.0">
    <uses-permission android:name="android.permission.READ_CALENDAR" />
    <application android:icon="@drawable/icon" android:label="@string/app_name" android:debuggable="true">
        <receiver android:name="com.eshayne.android.CalendarTest">
            <intent-filter>
                <action android:name="android.intent.action.EVENT_REMINDER" />
            </intent-filter>
        </receiver>
    </application>
    <uses-sdk android:minSdkVersion="8" />
</manifest>

Unfortunately, when I put this on my phone and set up a calendar event with a reminder - when the reminder alerts, I still see the AlertReceiver log entry, but not mine.

I have also read here about some system intents that require registering via code rather than in the manifest. So, I tried the following instead:

package com.eshayne.android;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;

public class CalendarTestDisplay extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        registerReceiver(new BroadcastReceiver() {
                        @Override
                        public void onReceive(Context context, Intent intent) {
                            android.util.Log.i("CalendarTestDisplay", "received broadcast");
                        }           
                     },
                     new IntentFilter("android.intent.action.EVENT_REMINDER"));
    }
}

with this modified manifest:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.eshayne.android"
    android:versionCode="1"
    android:versionName="1.0">
    <uses-permission android:name="android.permission.READ_CALENDAR" />
        <application android:icon="@drawable/icon" android:label="@string/app_name" android:debuggable="true">
        <activity android:name=".CalendarTestDisplay"
              android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
    <uses-sdk android:minSdkVersion="8" />
</manifest> 

with no better result.

Any ideas what I may be missing? Or any other ideas of how I might be able to capture calendar alarm occurrences?

Thanks, Ethan

回答1:

Well, what you're trying to do is not part of the Android SDK, mostly because the calendar is not part of the operating system.

That being said, at minimum, you will need to add a <data> element to your <intent-filter>, since the Intent has a Uri.

However, I'm reasonably certain that this will not work, since the Intent also specifically identifies a component (com.android.calendar/.AlertReceiver). AFAIK, that was in the Intent at the outset, and therefore the Intent will only be delivered to that component, ignoring all other routing rules. It's conceivable the listed component only showed up after Intent resolution, but I don't think that's how those log entries work.



回答2:

You need to set data scheme to "content" in the intent filter.

Using manifest, add data element inside intent-filter

        <receiver android:name="com.eshayne.android.CalendarTest">
            <intent-filter>
                <data android:scheme="content"/> <!-- this was missing -->
                <action android:name="android.intent.action.EVENT_REMINDER" />
            </intent-filter>
        </receiver>

Using code, add datascheme in one function call

IntentFilter filter = new IntentFilter(CalendarContract.ACTION_EVENT_REMINDER);
filter.addDataScheme("content");   // this was missing
registerReceiver(myRemindersReceiver, filter);


回答3:

It is possible to make the broadcast intent "android.intent.action.EVENT_REMINDER" work by specifying DataAuthority and DataScheme in your intent filter along with your intent action.You need to specify DataAuthority as "com.android.calendar" and DataScheme as "content".



回答4:

the broadcast intent "android.intent.action.EVENT_REMINDER" only gets fired when an alarm notification needs to be posted for a reminder



回答5:

set a notification for your EVENT (Eg: 10 minutes before the event) in order for the broadcast intent "android.intent.action.EVENT_REMINDER" to work