首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >无法接收android.intent.action.EVENT_REMINDER广播

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

Stack Overflow用户
提问于 2011-01-08 08:58:59
回答 5查看 6.4K关注 0票数 7

我想写一个当日历提醒发生时触发的应用程序。我知道没有官方记录的方法可以做到这一点,但我在日志中看到,当我的手机(Droid )上的日历闹钟响起时,AlertReceiver表示它收到了一个android.intent.action.EVENT_REMINDER:

代码语言:javascript
复制
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) }

因此,我设置了一个简单的BroadcastReceiver:

代码语言:javascript
复制
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!");
    }
}

有了这个清单:

代码语言:javascript
复制
<?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>

不幸的是,当我把这个放到我的手机上,并设置了一个带有提醒的日历事件时-当提醒提醒时,我仍然可以看到AlertReceiver日志条目,但不是我的。

我还在这里读到了一些需要通过代码而不是在清单中注册的系统意图。因此,我尝试了以下方法:

代码语言:javascript
复制
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"));
    }
}

使用此修改后的清单:

代码语言:javascript
复制
<?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> 

没有更好的结果。

你知道我可能遗漏了什么吗?或者任何其他想法,我可能能够捕获日历警报的发生?

谢谢你,伊森

EN

回答 5

Stack Overflow用户

回答已采纳

发布于 2011-01-08 09:23:40

你要做的不是Android SDK的一部分,主要是因为日历不是操作系统的一部分。

也就是说,您至少需要在<intent-filter>中添加一个<data>元素,因为Intent有一个Uri

但是,我可以合理地确定这是不会起作用的,因为Intent还特别标识了一个组件(com.android.calendar/.AlertReceiver)。Intent,它在一开始就在Intent中,因此AFAIK将只发送到该组件,忽略所有其他路由规则。可以想象,列出的组件只有在Intent解析后才会出现,但我不认为这些日志条目是这样工作的。

票数 1
EN

Stack Overflow用户

发布于 2013-05-19 18:32:26

您需要在意图过滤器中将数据方案设置为"content“。

使用manifest,在intent-filter中添加数据元素

代码语言:javascript
复制
        <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>

使用代码,在一个函数调用中添加数据方案

代码语言:javascript
复制
IntentFilter filter = new IntentFilter(CalendarContract.ACTION_EVENT_REMINDER);
filter.addDataScheme("content");   // this was missing
registerReceiver(myRemindersReceiver, filter);
票数 6
EN

Stack Overflow用户

发布于 2012-09-24 20:06:46

通过在意图过滤器中指定DataAuthority和DataScheme以及意图action.You需要将DataAuthority指定为"com.android.calendar“,将DataScheme指定为"content”,可以使广播意图“content”工作。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/4631693

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档