1.首先需要获取权限
<uses-permission android:name="android.permission.READ_CALENDAR" /><uses-permission android:name="android.permission.WRITE_CALENDAR" /><uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
主界面中直接调用获取所有的权限
private void checkPermission() {if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {//将需要的权限都放在里面String[] permissions = {Manifest.permission.WRITE_EXTERNAL_STORAGE,Manifest.permission.READ_EXTERNAL_STORAGE,Manifest.permission.CAMERA,Manifest.permission.RECORD_AUDIO,Manifest.permission.WRITE_CALENDAR,Manifest.permission.READ_CALENDAR};PermissionsUtils.getInstance().chekPermissions(this, permissions, permissionsResult);}}PermissionsUtils.IPermissionsResult permissionsResult = new PermissionsUtils.IPermissionsResult() {@Overridepublic void passPermissons() {selectCalendarInfo();}@Overridepublic void forbitPermissons() {finish();}};private void selectCalendarInfo() {}
2.自定义权限工具类
package com.peace.love.carpo_test.tool;import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Build;
import android.provider.Settings;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;import java.util.ArrayList;
import java.util.List;/*** 权限工具类*/public class PermissionsUtils {private final int mRequestCode = 100;//权限请求码public static boolean showSystemSetting = true;private PermissionsUtils() {}private static com.peace.love.carpo_test.tool.PermissionsUtils permissionsUtils;private IPermissionsResult mPermissionsResult;public static com.peace.love.carpo_test.tool.PermissionsUtils getInstance() {if (permissionsUtils == null) {permissionsUtils = new com.peace.love.carpo_test.tool.PermissionsUtils();}return permissionsUtils;}public void chekPermissions(AppCompatActivity context, String[] permissions, @NonNull IPermissionsResult permissionsResult) {mPermissionsResult = permissionsResult;if (Build.VERSION.SDK_INT < 23) {//6.0才用动态权限permissionsResult.passPermissons();return;}//创建一个mPermissionList,逐个判断哪些权限未授予,未授予的权限存储到mPerrrmissionList中List<String> mPermissionList = new ArrayList<>();//逐个判断你要的权限是否已经通过for (int i = 0; i < permissions.length; i++) {if (ContextCompat.checkSelfPermission(context, permissions[i]) != PackageManager.PERMISSION_GRANTED) {mPermissionList.add(permissions[i]);//添加还未授予的权限}}//申请权限if (mPermissionList.size() > 0) {//有权限没有通过,需要申请Log.d("有个别权限没有通过","OK");ActivityCompat.requestPermissions(context, permissions, mRequestCode);} else {//说明权限都已经通过,可以做你想做的事情去Log.d("权限全部通过","OK");permissionsResult.passPermissons();return;}}//请求权限后回调的方法//参数: requestCode 是我们自己定义的权限请求码//参数: permissions 是我们请求的权限名称数组//参数: grantResults 是我们在弹出页面后是否允许权限的标识数组,数组的长度对应的是权限名称数组的长度,数组的数据0表示允许权限,-1表示我们点击了禁止权限public void onRequestPermissionsResult(AppCompatActivity context, int requestCode, @NonNull String[] permissions,@NonNull int[] grantResults) {boolean hasPermissionDismiss = false;//有权限没有通过if (mRequestCode == requestCode) {for (int i = 0; i < grantResults.length; i++) {if (grantResults[i] == -1) {hasPermissionDismiss = true;}}//如果有权限没有被允许if (hasPermissionDismiss) {if (showSystemSetting) {showSystemPermissionsSettingDialog(context);//跳转到系统设置权限页面,或者直接关闭页面,不让他继续访问} else {mPermissionsResult.forbitPermissons();}} else {//全部权限通过,可以进行下一步操作。。。mPermissionsResult.passPermissons();}}}/*** 不再提示权限时的展示对话框*/AlertDialog mPermissionDialog;private void showSystemPermissionsSettingDialog(final AppCompatActivity context) {final String mPackName = context.getPackageName();if (mPermissionDialog == null) {mPermissionDialog = new AlertDialog.Builder(context).setMessage("已禁用权限,请手动授予").setPositiveButton("设置", new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface dialog, int which) {cancelPermissionDialog();Uri packageURI = Uri.parse("package:" + mPackName);Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS, packageURI);context.startActivity(intent);context.finish();}}).setNegativeButton("取消", new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface dialog, int which) {//关闭页面或者做其他操作cancelPermissionDialog();//mContext.finish();mPermissionsResult.forbitPermissons();}}).create();}mPermissionDialog.show();}//关闭对话框private void cancelPermissionDialog() {if (mPermissionDialog != null) {mPermissionDialog.cancel();mPermissionDialog = null;}}public interface IPermissionsResult {void passPermissons();void forbitPermissons();}}
3.Activity业务代码
private void initUrl() {if (Integer.parseInt(Build.VERSION.SDK) >= 8) {CALANDER_EVENT_URL = "content://com.android.calendar/events";} else {CALANDER_EVENT_URL = "content://calendar/events";}}private void addCalender(String title ,long s ,long e) {Log.d("进入创建日程接口","OK");Log.d("日历开始时间",String.valueOf(s));Log.d("日历结束时间",String.valueOf(e));//获取要出入的gmail账户的idString calId = "1";Cursor userCursor = getContentResolver().query(Uri.parse(CALANDER_EVENT_URL), null,null, null, null);if (null != userCursor && userCursor.getCount() > 0) {userCursor.moveToFirst();calId = userCursor.getString(userCursor.getColumnIndex(CalendarContract.Events.CALENDAR_ID)); //Events._ID
// Log.i(TAG, "calID: " + calId);}ContentValues event = new ContentValues();//标题Log.d("日历会议标题",title);event.put("title", title);//内容
// event.put("description", "一个产品能获得成功,是因为它真正帮人们解决了某个问题。这听上去过于基础,但这的的确确是打造一个好产品最为重要的因素。");//插入到账户event.put("calendar_id", calId);//开始时间Calendar mCalendar = Calendar.getInstance();mCalendar.set(Calendar.HOUR_OF_DAY, 15);long start = mCalendar.getTime().getTime();//截止时间,如果需要设置一周或者一个月可以改截止日期即可mCalendar.set(Calendar.HOUR_OF_DAY, 16);long end = mCalendar.getTime().getTime();Log.d("日历开始时间",String.valueOf(start));Log.d("日历结束时间",String.valueOf(end));//起始时间event.put(CalendarContract.Events.DTSTART,s * 1000);//截止时间event.put(CalendarContract.Events.DTEND, e * 1000);//控制是否事件触发报警,提醒如下event.put(CalendarContract.Events.HAS_ALARM, 1);//设置时区,否则会报错event.put(CalendarContract.Events.EVENT_TIMEZONE, TimeZone.getDefault().getID());//设置一个全天事件的条目//event.put("allDay", 1); // 0 for false, 1 for true//设置地理位置//event.put(CalendarContract.Events.EVENT_LOCATION, "Event Location");Uri newEvent = getContentResolver().insert(Uri.parse(CALANDER_EVENT_URL), event);long id = Long.parseLong(newEvent.getLastPathSegment());
// Log.i(TAG, "id: " + id);ContentValues values = new ContentValues();values.put("event_id", id);//提前10分钟有提醒values.put(CalendarContract.Reminders.MINUTES, 10);values.put(CalendarContract.Reminders.EVENT_ID, id);values.put(CalendarContract.Reminders.METHOD, CalendarContract.Reminders.METHOD_ALERT);ContentResolver cr1 = getContentResolver(); // 为刚才新添加的event添加reminder//这里在6.0中检查下权限信息if (ActivityCompat.checkSelfPermission(this, Manifest.permission.WRITE_CALENDAR) != PackageManager.PERMISSION_GRANTED) {return;}cr1.insert(CalendarContract.Reminders.CONTENT_URI, values); // 调用这个方法返回值是一个UriToast.makeText(this, "该会议已加入日历行程", Toast.LENGTH_LONG).show();}