简体   繁体   English

无法接收android.intent.action.EVENT_REMINDER广播

[英]Unable to receive android.intent.action.EVENT_REMINDER broadcast

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: 我意识到没有正式记录这样做的方法,但我在日志中看到当我的日历闹钟在我的手机(Droid X)上消失时,AlertReceiver表示它已收到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: 所以,我设置了一个简单的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. 不幸的是,当我把它放在我的手机上并设置一个带有提醒的日历活动时 - 当提醒提醒时,我仍然会看到AlertReceiver日志条目,但不是我的。

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 谢谢,Ethan

You need to set data scheme to "content" in the intent filter. 您需要在intent过滤器中将数据方案设置为“content”。

Using manifest, add data element inside intent-filter 使用manifest,在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 使用代码,在一个函数调用中添加datascheme

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

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. 那么,你想要做的不是Android SDK的一部分,主要是因为日历不是操作系统的一部分。

That being said, at minimum, you will need to add a <data> element to your <intent-filter> , since the Intent has a Uri . 话虽如此,至少需要在<intent-filter>添加一个<data>元素,因为Intent有一个Uri

However, I'm reasonably certain that this will not work, since the Intent also specifically identifies a component ( com.android.calendar/.AlertReceiver ). 但是,我有理由相信这不会起作用,因为Intent还专门识别一个组件( 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. AFAIK,一开始就在Intent中,因此只会将Intent传递给该组件,而忽略所有其他路由规则。 It's conceivable the listed component only showed up after Intent resolution, but I don't think that's how those log entries work. 可以想象,列出的组件仅在Intent解析后出现,但我不认为这些日志条目的工作方式。

通过在意图过滤器中指定DataAuthority和DataScheme以及您的意图操作,可以使广播意图“android.intent.action.EVENT_REMINDER”工作。您需要将DataAuthority指定为“com.android.calendar”,将DataScheme指定为“内容”。

广播意图“android.intent.action.EVENT_REMINDER”仅在需要为提醒发布警报通知时才会被触发

为您的EVENT设置通知(例如:事件前10分钟),以便广播意图“android.intent.action.EVENT_REMINDER”工作

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM