我对android很新。我正在开发一个应用程序,我自动设置事件。 这是我的代码,我指的是http://developer.android.com/guide/topics/providers/calendar-provider.html
public boolean setReminder(int SelectedHour,int SelectedMinute,int NewHour,int NewMinute)
{
try{
long calID = 3;
long startMillis = 0;
long endMillis = 0;
Calendar StartTime = Calendar.getInstance();
StartTime.set(StartTime.YEAR,StartTime.MONTH,StartTime.DAY_OF_MONTH,SelectedHour,SelectedMinute);
startMillis = StartTime.getTimeInMillis();
Calendar EndTime = Calendar.getInstance();
int NewMonth = StartTime.MONTH+1;int NewDay = StartTime.DAY_OF_MONTH;
EndTime.set(StartTime.YEAR,NewMonth,NewDay,NewHour,NewMinute);
endMillis = EndTime.getTimeInMillis();
TimeZone tz = TimeZone.getDefault();
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, tz.getID());
//Getting Problem in Line Below
Uri uri = cr.insert(CalendarContract.Events.CONTENT_URI,values);
// get the event ID that is the last element in the Uri
long eventID = Long.parseLong(uri.getLastPathSegment());
Uri.Builder builder = CalendarContract.CONTENT_URI.buildUpon();
builder.appendPath("time");
ContentUris.appendId(builder, startMillis);
Intent intent = new Intent(Intent.ACTION_VIEW).setData(builder.build());
startActivity(intent);
return true;
}
catch (Exception ex)
{
ex.printStackTrace();
return false;
}
我在
Uri uri = cr.insert(CalendarContract.Events.CONTENT_URI,values);
错误在说 调用需要权限,user.code可能会拒绝该权限,应明确检查权限是否可用...
我已经在我的清单文件中添加了 android.permission.ACCESS_FINE_LOCATION 和 android.permission.ACCESS_COARSE_LOCATION 等权限
虽然我的应用程序运行但是当我调用Calender Intent时它给了我 网页不可用 。
我也尝试过Adding events date and time in android calendar
但仍无效。
提前感谢..
答案 0 :(得分:0)
这意味着您要求的功能在android> = 6中可能会要求Runtime Permissions - 用户在运行时必须允许的权限。请点击链接了解如何实施正确的流程。
// Here, thisActivity is the current activity
if (ContextCompat.checkSelfPermission(thisActivity,
Manifest.permission.READ_CALENDAR)
!= PackageManager.PERMISSION_GRANTED) {
// Should we show an explanation?
if (ActivityCompat.shouldShowRequestPermissionRationale(thisActivity,
Manifest.permission.READ_CALENDAR)) {
// Show an expanation to the user *asynchronously* -- don't block
// this thread waiting for the user's response! After the user
// sees the explanation, try again to request the permission.
} else {
// No explanation needed, we can request the permission.
ActivityCompat.requestPermissions(thisActivity,
new String[]{Manifest.permission.READ_CALENDAR},
MY_PERMISSIONS_REQUEST_READ_CALENDAR;
// MY_PERMISSIONS_REQUEST_READ_CALENDAR is an
// app-defined int constant. The callback method gets the
// result of the request.
}
}else{
Uri uri = cr.insert(CalendarContract.Events.CONTENT_URI,values);
}
答案 1 :(得分:0)
由于缺少日历权限,可能是您收到此错误。将日历权限添加到您的清单文件中。
从日历中读取:
<强> android.permission.READ_CALENDAR 强>
要写入日历:
<强> android.permission.WRITE_CALENDAR 强>
答案 2 :(得分:0)
因为这对许多程序员来说似乎仍然是一个问题...在这里我的解决方案: 我有一个按钮来开始与日历的交互。这将首先打开一个带有2个按钮的消息框,其中用户必须允许进一步的操作。如果他允许另一个Messagebox出现并要求用户允许阅读日历
private void addMessageBox() {
AlertDialog.Builder dlgAlert = new AlertDialog.Builder(getContext());
dlgAlert.setMessage("Events mit lokalem Kalender synchronisieren?");
dlgAlert.setTitle("Zugriff auf Kalender");
dlgAlert.setCancelable(true);
dlgAlert.setPositiveButton("Erlauben",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
permissions = new Permissions(getActivity());
if (!permissions.checkPermissionReadCalendar()) {
permissions.requestPermissionReadCalendar(Permissions.CALENDAR_PERMISSION_REQUEST_READ);
} else {
try {
addToLocalCalendar();
} catch (ParseException e) {
e.printStackTrace();
}
}
}
});
dlgAlert.setNegativeButton("Verbieten",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
//return true;
}
});
dlgAlert.create().show();
}
我使用在此论坛中找到的自定义类控制权限:
Permissions permissions;
上课:
public class Permissions {
public static final int EXTERNAL_STORAGE_PERMISSION_REQUEST_WRITE = 0;
public static final int EXTERNAL_STORAGE_PERMISSION_REQUEST_READ = 1;
public static final int CALENDAR_PERMISSION_REQUEST_READ = 2;
Activity activity;
Context mContext;
public Permissions(Activity activity) {
this.activity = activity;
this.mContext = activity;
}
public boolean checkPermissionReadCalendar(){
int result = ContextCompat.checkSelfPermission(activity, Manifest.permission.READ_CALENDAR);
if (result == PackageManager.PERMISSION_GRANTED){
return true;
} else {
return false;
}
}
public void requestPermissionReadCalendar(int requestCode){
if (ActivityCompat.shouldShowRequestPermissionRationale(activity, Manifest.permission.READ_CALENDAR)){
Toast.makeText(mContext.getApplicationContext(), "Kalenderzugriff nicht möglich. Bitte erlaube Kalenderzugriff in den App Einstellungen.", Toast.LENGTH_LONG).show();
} else {
ActivityCompat.requestPermissions(activity,new String[]{Manifest.permission.READ_CALENDAR},requestCode);
}
}
U以某种方式不需要请求写入权限...在应用程序设置中,只有一个日历权限,如果您调用读取或写入权限,则允许该权限。在清单中添加:
<uses-permission android:name="android.permission.READ_CALENDAR" />
<uses-permission android:name="android.permission.WRITE_CALENDAR" />
如果用户拒绝该权限,他将始终收到他没有足够权利执行此操作的Toast消息。他必须手动允许App设置中的权限。 对于红色下划线部分..它表示应用程序必须检查用户是否允许以下操作。如果您单击此行并按Alt + Enter,将插入此代码:
if (ActivityCompat.checkSelfPermission(getContext(), Manifest.permission.READ_CALENDAR) != PackageManager.PERMISSION_GRANTED) {
// TODO: Consider calling
// ActivityCompat#requestPermissions
// here to request the missing permissions, and then overriding
// public void onRequestPermissionsResult(int requestCode, String[] permissions,
// int[] grantResults)
// to handle the case where the user grants the permission. See the documentation
// for ActivityCompat#requestPermissions for more details.
return true;
}
检查应用程序是否具有权限,如果没有则返回。
答案 3 :(得分:0)
我们需要检查并请求这两个权限。
if (ActivityCompat.checkSelfPermission(activity!!, Manifest.permission.READ_CALENDAR) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(activity!!, arrayOf(Manifest.permission.READ_CALENDAR), 30);
}
if (ActivityCompat.checkSelfPermission(activity!!, Manifest.permission.WRITE_CALENDAR) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(activity!!, arrayOf(Manifest.permission.WRITE_CALENDAR), 35);
}
如果我们同时请求 READ 和 WRITE Calendar 权限。不会再出现这个错误。