在我的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>
试着改变它.
<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!"); } }
当我点击“新事件”按钮时,我没有在列表中获取我的应用程序.
我有一个环顾四周,认为我错过了一些简单的东西,或者我完全错在了错误的道路上.
感谢您提前帮助.
你无法使用广播接收器实现它,广播意图过滤器只有当某个应用广播特定意图,例如连接状态改变,屏幕关闭,电池电量低等是系统广播的正确示例时才会起作用.虽然创建日历活动不能是广播&这就是为什么你不能收到它.从关于广播接收器的android文档:
A broadcast receiver is a component that responds to system-wide
broadcast announcements. Many broadcasts originate from the system—for
example, a broadcast announcing that the screen has turned off, the
battery is low, or a picture was captured. Applications can also
initiate broadcasts—for example, to let other applications know that
some data has been downloaded to the device and is available for them
to use. Although broadcast receivers don’t display a user interface,
they may create a status bar notification to alert the user when a
broadcast event occurs. More commonly, though, a broadcast receiver is
just a “gateway” to other components and is intended to do a very
minimal amount of work. For instance, it might initiate a service to
perform some work based on the event.
要获得所需的功能,您需要为您的活动注册intent-filter.
此外,您的清单应包含读/写日历权限和事件编辑/插入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选择器对话框中显示你的应用程序,用于添加/编辑日历事件.
现在,在您的活动中,您可以使用getIntent()处理此意图并相应地执行操作.7000支持Calendar事件支持的附加功能.像Events.TITLE,Events.EVENT_LOCATION等.如果你打算创建自己的日历活动创建者,那么你需要优雅地处理所有这些额外的东西,以获得令人敬畏的用户体验.