簡體   English   中英

無法接收android.intent.action.EVENT_REMINDER廣播

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

我想編寫一個在日歷提醒發生時觸發的應用程序。 我意識到沒有正式記錄這樣做的方法,但我在日志中看到當我的日歷鬧鍾在我的手機(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) }

所以,我設置了一個簡單的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!");
    }
}

有了這個清單:

<?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日志條目,但不是我的。

我還在這里閱讀了一些需要通過代碼而不是清單注冊的系統意圖。 所以,我嘗試了以下代碼:

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"));
    }
}

使用此修改后的清單:

<?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> 

沒有更好的結果。

我可能會缺少什么想法? 或者關於我如何能夠捕獲日歷警報事件的任何其他想法?

謝謝,Ethan

您需要在intent過濾器中將數據方案設置為“content”。

使用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>

使用代碼,在一個函數調用中添加datascheme

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

那么,你想要做的不是Android SDK的一部分,主要是因為日歷不是操作系統的一部分。

話雖如此,至少需要在<intent-filter>添加一個<data>元素,因為Intent有一個Uri

但是,我有理由相信這不會起作用,因為Intent還專門識別一個組件( com.android.calendar/.AlertReceiver )。 AFAIK,一開始就在Intent中,因此只會將Intent傳遞給該組件,而忽略所有其他路由規則。 可以想象,列出的組件僅在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