android – 如何创建Receiver以创建新事件

android – 如何创建Receiver以创建新事件,第1张

概述我创建了一个连续添加日历约会的应用程序.但是我现在希望能够点击日历程序中的“新事件”按钮,例如. aCalendar并让我的程序d出.我想我有点失落. 在我的AndroidManifest.xml中 <receiver android:name="com.johnny.CalendarAdd" android:enabled="true" > 我创建了一个连续添加日历约会的应用程序.但是我现在希望能够点击日历程序中的“新事件”按钮,例如. aCalendar并让我的程序d出.我想我有点失落.

在我的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一些测试按钮: –

@OverrIDepublic 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等.如果你打算创建自己的日历活动创建者,那么你需要优雅地处理所有这些额外的东西,以获得令人敬畏的用户体验.

总结

以上是内存溢出为你收集整理的android – 如何创建Receiver以创建新事件全部内容,希望文章能够帮你解决android – 如何创建Receiver以创建新事件所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1122050.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-29
下一篇 2022-05-29

发表评论

登录后才能评论

评论列表(0条)

保存