Android   发布时间:2022-04-28  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了android – 如何创建Receiver以创建新事件大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我创建了一个连续添加日历约会的应用程序.但是我现在希望能够点击日历程序中的“新事件”按钮,例如. aCalendar并让我的程序弹出.我想我有点失落.

在我的@L_567_3@manifest.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文档:

要获得所需的功能,您需要为您的活动注册intent-filter.
此外,您的清单应包含读/写日历权限和事件编辑/插入intent-filter,例如

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://scheR_574_11845@as.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等.如果你打算创建自己的日历活动创建者,那么你需要优雅地处理所有这些额外的东西,以获得令人敬畏的用户体验.

大佬总结

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

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

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。