2016-05-16 18 views
0

我對android.I很新,我正在開發一個應用程序,我可以自動設置事件。 這是我的代碼,我指http://developer.android.com/guide/topics/providers/calendar-provider.html呼叫需要在日曆中的內容URI的許可

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; 
    } 

我得到RedMark線下

Uri uri = cr.insert(CalendarContract.Events.CONTENT_URI,values); 

錯誤是說 呼叫需要可通過user.code被拒絕的權限應明確檢查是否有權限可用...

我已經添加了許可離子喜歡在我的清單文件android.permission.ACCESS_FINE_LOCATIONandroid.permission.ACCESS_COARSE_LOCATION

雖然我的應用程序運行,但是當我打電話日曆意向它給了我頁面不availble的

我也試着像Adding events date and time in android calendar

但還是它不工作。

在此先感謝..

+0

爲什麼你在你的表達式中添加'Location'權限..? –

+0

檢查此ref:http://stackoverflow.com/questions/22990189/content-provider-grant-uri-permission?rq = 1 –

+0

我需要在Manifest中添加哪些權限?我還添加了** android.permission.READ_CALENDAR **和** android.permission.WRITE_CALENDAR **。爲什麼它不工作? – Pranita

回答

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); 

} 
+0

什麼是變量** MY_PERMISSIONS_REQUEST_READ_CALENDAR ** – Pranita

+0

只是你應該聲明的一個常量。例如 private final int MY_PERMISSIONS_REQUEST_READ_CALENDAR = 123; –

+0

再次同樣的問題** WebPage不可用** – Pranita

0

可能是因爲缺少Calendar權限而出現此錯誤。將日曆權限添加到您的Manifest文件中。

android.permission.READ_CALENDAR

寫入到日曆:

android.permission.WRITE_CALENDAR

+0

我已經添加了這兩個permissions.still得到同樣的問題 – Pranita

0

由於這似乎仍然

要從日曆閱讀成爲許多編碼人員的問題...在這裏我的解決方案: 我有一個按鈕來啓動交互w ith日曆。這將首先打開帶有2個按鈕的消息框,其中用戶必須允許進一步的操作。如果他讓另一個消息框出現並詢問用戶是否允許讀取日曆

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); 
    } 
} 

ü以某種方式不需要請求寫入權限...在應用程序設置中,日曆只有一個權限,如果您調用讀取或寫入權限,它將被允許。如果用戶拒絕,他總是會得到他有沒有足夠的權限才能執行此操作敬酒消息許可

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

:在清單中添加這一點。他必須手動在App設置中允許該權限。 對於紅色下劃線的部分..它說,應用程序必須檢查用戶是否允許以下​​操作。如果妳點擊此行,按下Alt +輸入驗證碼將被插入:

​​

這用來檢查應用程序有權限和回報,如果沒有。