2013-04-11 32 views
3

我已經創建了一個應用程序,它可以將約會連續添加到日曆中。不過,我現在希望能夠點擊日曆程序中的「新事件」按鈕,例如。 aCalendar並讓我的程序彈出。我覺得我有點失落。如何創建接收器以創建新事件

在我的AndroidManifest.xml

<receiver 
     android:name="com.johnny.CalendarAdd" 
     android:enabled="true" 
     > 
     <intent-filter> 
      <data android:pathPrefix="vnd.android.cursor.item/event" /> 
      <action android:name="android.intent.action.EDIT" /> 
     </intent-filter> 
    </receiver> 

tryed將其更改爲。

<activity android:name="CalendarAdd"> 
     <intent-filter> 
     <data android:pathPrefix="vnd.android.cursor.item/event" /> 
     <action android:name="android.intent.action.EDIT" /> 
     </intent-filter> 
    </activity> 

在類文件

package com.johnny; 

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

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

} 
} 

當我點擊「新活動」按鈕,我沒有得到我的應用程序列表。 我有一個環視,並認爲我錯過了一些簡單的東西,或者我完全在錯誤的道路上。

感謝您的任何幫助提前。

+0

如下建議說,所以我改變了上面的代碼示例,以顯示我已經tryed。 – John 2013-04-11 04:15:45

回答

0
  <activity android:name="com.johny.CalendarTest" 
     android:label="@string/app_name" > 

     <intent-filter> 
      <action android:name="android.intent.action.EDIT" /> 
      <category android:name="android.intent.category.DEFAULT" /> 
      <category android:name="android.intent.category.ALTERNATIVE" /> 

      <data android:host="com.android.calendar" /> 
      <data android:host="calendar" /> 
      <data android:scheme="content" /> 
     </intent-filter> 
     <intent-filter> 
      <action android:name="android.intent.action.EDIT" /> 
      <category android:name="android.intent.category.DEFAULT" /> 
      <category android:name="android.intent.category.ALTERNATIVE" /> 

      <data android:mimeType="vnd.android.cursor.item/event" /> 
     </intent-filter> 
    </activity> 

這是我使用和準備工作的確切代碼。 大部分的答案幫助獲得答覆。 獎勵歸功於那些付出最大努力的人,並取得了成果。

0

將您在<activity>中定義的<intent-filter>代替。應向用戶提供可處理該操作的應用程序列表(包括您的應用程序),以便他們可以根據需要選擇您的應用程序。

<receiver> s用於接收BroadcastIntents,它們有點不同。

1

使用<receiver>爲您最初做的,android:name應該是com.johnny.CalendarTest,如果你想註冊您的接收器,因爲您的接收器類名稱爲CalendarTest

See here

android:name Required name of the class implementing the receiver, deriving from BroadcastReceiver.

+0

是的你是正確的,因爲這是我的代碼中的一個錯誤。然而,這並沒有給我新的任命意圖。感謝您一直以來的幫助。任何其他想法。 – John 2013-04-25 04:53:03

+0

'android.intent.action.INSERT'如何? – StoneBird 2013-04-25 05:00:57

+0

沒有這樣的運氣。無論如何,謝謝 – John 2013-04-25 06:13:40

1

但是我現在希望能夠單擊在 日曆程序如 「新活動」 按鈕。 aCalendar並讓我的程序彈出。

您是否嘗試過在你的<intent-filter>各種actioncategory過濾聲明。

例如:

<intent-filter> 
     <action android:name="android.intent.action.VIEW" /> 
     <action android:name="android.intent.action.EDIT" /> 
     <action android:name="android.intent.action.PICK" /> 
     <category android:name="android.intent.category.DEFAULT" /> 
     <category android:name="android.intent.category.BROWSABLE" /> 
     <category android:name="android.intent.category.OPENABLE" /> 
     <category android:name="android.intent.category.DEFAULT" /> 
     <data android:mimeType="application/myapp" /> 
    </intent-filter> 

看看是否有這些的幫助你。

參見:http://developer.android.com/reference/android/content/Intent.html

1

CalendarController,我幾乎可以肯定的是,這些目的都與特定的組件集明確意圖。例如,在launchEditEvent方法中。

private void launchEditEvent(long eventId, long startMillis, long endMillis, boolean edit) { 
    Uri uri = ContentUris.withAppendedId(Events.CONTENT_URI, eventId); 
    Intent intent = new Intent(Intent.ACTION_EDIT, uri); 
    intent.putExtra(EXTRA_EVENT_BEGIN_TIME, startMillis); 
    intent.putExtra(EXTRA_EVENT_END_TIME, endMillis); 
    intent.setClass(mContext, EditEventActivity.class); 
    intent.putExtra(EVENT_EDIT_ON_LAUNCH, edit); 
    mEventId = eventId; 
    mContext.startActivity(intent); 
} 

意圖被設置爲由EditEventActivity處理。所以在你的申請中不可能收到這樣的意圖。我認爲日曆應用使其成爲明確的意圖,因爲它將此視爲應用內事件。如果這不是一個明確的意圖,您的代碼應該工作,因爲日曆應用程序中的組件與您所做的幾乎相同。

<intent-filter> 
     <action android:name="android.intent.action.EDIT" /> 
     <action android:name="android.intent.action.INSERT" /> 
     <category android:name="android.intent.category.DEFAULT" /> 
     <data android:mimeType="vnd.android.cursor.item/event" /> 
    </intent-filter> 
    <intent-filter> 
     <action android:name="android.intent.action.EDIT" /> 
     <action android:name="android.intent.action.INSERT" /> 
     <category android:name="android.intent.category.DEFAULT" /> 
     <data android:mimeType="vnd.android.cursor.dir/event" /> 
    </intent-filter> 
0

aCalendar新事件意圖就是這樣。

{ACT = android.intent.action.EDIT典型值= vnd.android.cursor.item /事件FLG = 0x20000000 CMP = org.withouthat.acalendar/.edit.EditActivity(具有額外)}

而Google日曆應用新事件意圖就是這樣。

{行爲= android.intent.action.VIEW CMP = com.android.calendar/.event.EditEventActivity

他們明確意圖。 您無法在您的應用中收到此意向。 更多關於意圖點擊here

4

不能使用廣播接收器實現它,廣播意向過濾器只在工作時,一些應用程序播放特定的意圖,如連接狀態得到了改變,屏幕得到了關閉時,電池電量低等等都是系統廣播的正確例子。雖然創建日曆事件不能是廣播&這就是爲什麼你不能接收它。

從機器人文檔中有關廣播接收機:

的廣播接收機是響應系統範圍 廣播通知的部件。許多廣播源自系統 - 例如,廣播宣佈屏幕已關閉,電池電量不足或拍攝了照片。應用程序還可以啓動廣播 - 例如,讓其他應用程序知道某些數據已下載到設備,並且可供其使用 。儘管廣播接收器不顯示用戶界面,但他們可能會創建狀態欄通知,以在發生廣播事件時提醒用戶。然而,更普遍的情況是,廣播接收器僅僅是一個到其他組件的「網關」,並且打算做很少量的工作。例如,它可能會啓動一項服務,以 爲基礎執行一些工作。

爲了達到所需的功能,您需要爲您的活動註冊intent-filter。 此外,您的清單應包含讀/寫日曆權限和事件編輯/插入意圖過濾器,例如

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
      package="com.example.CalanderTest" 
      android:versionCode="1" 
      android:versionName="1.0"> 
    <uses-sdk android:minSdkVersion="8"/> 

    <uses-permission android:name="android.permission.READ_CALENDAR" /> 
    <uses-permission android:name="android.permission.WRITE_CALENDAR" /> 

    <application android:label="@string/app_name" android:icon="@drawable/ic_launcher"> 
     <activity android:name="MyActivity" 
        android:label="@string/app_name"> 
      <intent-filter> 
       <action android:name="android.intent.action.EDIT" /> 
       <action android:name="android.intent.action.INSERT" /> 
       <category android:name="android.intent.category.DEFAULT" /> 
       <data android:mimeType="vnd.android.cursor.item/event" /> 
      </intent-filter> 
     </activity> 
    </application> 
</manifest> 

爲測試它下面做一些測試按鈕的onclick: -

@Override 
public void onClick(View view) { 
    super.onClick(view);  
    if (view.getId()==R.id.create_event) 
    { 
     Intent i = new Intent(Intent.ACTION_INSERT); 
     i.setType("vnd.android.cursor.item/event"); 
     startActivity(i); 
    } 
} 

在我的測試按鈕的點擊我得到了菜單就像在下面的圖片,所以上面的代碼工作正常顯示你在App選擇器對話框中添加/編輯日曆事件的應用程序。 enter image description here

現在在您的活動中,您可以使用getIntent()處理此意圖並相應地執行操作。 You can have a look here由日曆事件支持的附加功能。 Like Events.TITLE,Events.EVENT_LOCATION等如果您要創建自己的日曆事件創建者,那麼您需要優雅地處理所有這些額外事件,以獲得絕佳的用戶體驗。

+1

做得好,你已經努力爲你應得的獎勵。 – John 2013-05-02 00:31:14

+1

確實;我正在爲此答案添加書籤 – 2013-05-02 00:33:39

0

舊的答案告訴我的方向,但我必須做一個小的修改,使其工作:

<activity 
     android:name=".YourActivity" 
     android:exported="true"> 
    <intent-filter> 
      <action android:name="android.intent.action.INSERT" /> 
      <action android:name="android.intent.action.EDIT" /> 
      <category android:name="android.intent.category.DEFAULT" /> 
      <data android:mimeType="vnd.android.cursor.dir/event" /> 
    </intent-filter> 
</activity>