1

我按照此处的指南 在 Google 日历上添加事件,但它没有将事件添加到日历中并且我有错误:

打开跟踪文件时出错:没有这样的文件或目录 (2)

long eventID = Long.parseLong(uri.getLastPathSegment());

没用过,为什么?

代码主要:

    long calID = 3;
Calendar beginTime = Calendar.getInstance();

ContentResolver cr = getContentResolver();
ContentValues values = new ContentValues();
values.put(Events.DTSTART, beginTime.getTimeInMillis() + 11*60*1000);
values.put(Events.DTEND, beginTime.getTimeInMillis()+60*60*1000);
values.put(Events.TITLE, "Jazzercise");
values.put(Events.DESCRIPTION, "Group workout");
values.put(Events.CALENDAR_ID, calID);
values.put(Events.EVENT_TIMEZONE, "America/Los_Angeles");
Uri uri = cr.insert(Events.CONTENT_URI, values);

// get the event ID that is the last element in the Uri
long eventID = Long.parseLong(uri.getLastPathSegment());
// 
// ... do something with event ID
//
//  

显现:

    <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.inscal"
    android:versionCode="1"
    android:versionName="1.0" >
  <uses-sdk
        android:minSdkVersion="16"
        android:targetSdkVersion="16" />
    <uses-permission android:name="android.permission.READ_CALENDAR" />
    <uses-permission android:name="android.permission.WRITE_CALENDAR" />
    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.inscal.MainActivity"
            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>

</manifest>

我该如何解决这个问题?

4

1 回答 1

0

您好,请在您的 Androidmainfest 文件中添加此权限

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

这是使用 Intent 实现日历事件的代码:

Calendar cal = Calendar.getInstance();              
Intent intent = new Intent(Intent.ACTION_EDIT);
intent.setType("vnd.android.cursor.item/event");
intent.putExtra("beginTime", cal.getTimeInMillis());
intent.putExtra("allDay", true);
intent.putExtra("rrule", "FREQ=YEARLY");
intent.putExtra("endTime", cal.getTimeInMillis()+60*60*1000);
intent.putExtra("title", "A Test Event from android app");
startActivity(intent);
于 2013-03-02T16:54:56.233 回答