gpt4 book ai didi

java - 如何在 Android Studio 中静默地将事件添加到日历中?

转载 作者:行者123 更新时间:2023-12-01 11:11:41 25 4
gpt4 key购买 nike

我读了上一个问题Here ,但它并没有完全回答我的问题。我试图做一些简单的事情,比如按下按钮并输入一个事件,但程序不断崩溃。我的代码是:

package com.example.events;

import java.util.Calendar;

import android.content.ContentResolver;
import android.content.ContentValues;
import android.net.Uri;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.provider.CalendarContract;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

import com.example.events.R;

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

Button button = (Button) findViewById(R.id.button1);
button.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View arg0) {

// Perform action on click
addCalendarEvent();
}

});

}

public void addCalendarEvent() {

long calID = 1;
long startMillis = 0;
long endMillis = 0;
Calendar beginTime = Calendar.getInstance();
beginTime.set(2015, 8, 9, 7, 30);
startMillis = beginTime.getTimeInMillis();
Calendar endTime = Calendar.getInstance();
endTime.set(2015, 8, 9, 8, 45);
endMillis = endTime.getTimeInMillis();

ContentResolver cr = getContentResolver();
ContentValues values = new ContentValues();
values.put(CalendarContract.Events.DTSTART, startMillis);
values.put(CalendarContract.Events.DTEND, endMillis);
values.put(CalendarContract.Events.TITLE, "Jazzercise");
values.put(CalendarContract.Events.DESCRIPTION, "Group workout");
values.put(CalendarContract.Events.CALENDAR_ID, calID);
values.put(CalendarContract.Events.EVENT_TIMEZONE, "America/New_York");
Uri uri = cr.insert(CalendarContract.Events.CONTENT_URI, values);
}
}

我的 AndroidManifest.xml 看起来像这样

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

所以,基本上我想做的是按下该按钮并将事件添加到日历中,而不提示用户输入任何详细信息。任何帮助,将不胜感激。这是错误的堆栈跟踪:

08-29 18:14:44.108  13218-13218/com.example.events E/dalvikvm﹕ >>>>> Normal User
08-29 18:14:44.118 13218-13218/com.example.events E/dalvikvm﹕ >>>>> com.example.events [ userId:0 | appId:10255 ]
08-29 18:14:52.106 13218-13218/com.example.events E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.example.events, PID: 13218
java.lang.SecurityException: Permission Denial: opening provider com.android.providers.calendar.CalendarProvider2 from ProcessRecord{44f66d78 13218:com.example.events/u0a255} (pid=13218, uid=10255) requires android.permission.READ_CALENDAR or android.permission.WRITE_CALENDAR
at android.os.Parcel.readException(Parcel.java:1465)
at android.os.Parcel.readException(Parcel.java:1419)
at android.app.ActivityManagerProxy.getContentProvider(ActivityManagerNative.java:3299)
at android.app.ActivityThread.acquireProvider(ActivityThread.java:4843)
at android.app.ContextImpl$ApplicationContentResolver.acquireProvider(ContextImpl.java:2718)
at android.content.ContentResolver.acquireProvider(ContentResolver.java:1390)
at android.content.ContentResolver.insert(ContentResolver.java:1196)
at com.example.events.MainActivity.addCalendarEvent(MainActivity.java:60)
at com.example.events.MainActivity$1.onClick(MainActivity.java:33)
at android.view.View.performClick(View.java:4753)
at android.view.View$PerformClick.run(View.java:19562)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:146)
at android.app.ActivityThread.main(ActivityThread.java:5635)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1291)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1107)
at dalvik.system.NativeStart.main(Native Method)

最佳答案

您的堆栈跟踪表明您缺少权限。您需要将堆栈跟踪中提到的权限添加到您的 Android list 中。看到这个:How to add manifest permission to android application?

关于java - 如何在 Android Studio 中静默地将事件添加到日历中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32290820/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com