2014-09-19 80 views
1

我們正在研究android應用程序,我們正試圖在後臺服務中默默添加事件。使用以下代碼是相同的:安卓設備日曆上未顯示的事件

package com.red_folder.phonegap.plugin.backgroundservice.sample; 

import java.text.SimpleDateFormat; 
import java.util.Calendar; 
import java.util.Date; 
import java.util.TimeZone; 


import org.json.JSONException; 
import org.json.JSONObject; 
import org.json.JSONArray; 

import android.annotation.SuppressLint; 
import android.content.ContentResolver; 
import android.content.ContentValues; 
import android.content.Context; 
import android.content.Intent; 
import android.content.ContentUris; 
import android.net.Uri; 
import android.os.Bundle; 
import android.provider.CalendarContract; 
import android.provider.CalendarContract.Events; 
import android.provider.CalendarContract.Reminders; 
import android.util.Log; 

import org.apache.cordova.CallbackContext; 
import org.apache.cordova.CordovaPlugin; 
import org.apache.cordova.PluginResult; 

import com.red_folder.phonegap.plugin.backgroundservice.BackgroundService; 


@SuppressWarnings("deprecation") 
public class MyService extends BackgroundService { 

    private final static String TAG = MyService.class.getSimpleName(); 

    private String mHelloTo = "World"; 

    @SuppressLint("NewApi") 
    @SuppressWarnings("static-access") 
    @Override 
    protected JSONObject doWork() { 
     JSONObject result = new JSONObject(); 



      Log.d(TAG, "Start calendar insertion"); 

      JSONObject jsonArray; 

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


      ContentResolver cr = getContentResolver(); 
      ContentValues values = new ContentValues(); 
      values.put(Events.DTSTART, startMillis); 
      values.put(Events.DTEND, endMillis); 
      values.put(Events.TITLE, "Jazzercise"); 
      values.put(Events.DESCRIPTION, "Group workout"); 
      values.put(Events.CALENDAR_ID, calID); 
      values.put(Events.VISIBLE, 0); 
      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()); 




      Log.d(TAG, "End calendar insertion"); 


     return result; 
    } 

    @Override 
    protected JSONObject getConfig() { 
     JSONObject result = new JSONObject(); 

     try { 
      result.put("HelloTo", this.mHelloTo); 
     } catch (JSONException e) { 
     } 

     return result; 
    } 

    @Override 
    protected void setConfig(JSONObject config) { 
     try { 
      if (config.has("HelloTo")) 
       this.mHelloTo = config.getString("HelloTo"); 
     } catch (JSONException e) { 
     } 

    }  

    @Override 
    protected JSONObject initialiseLatestResult() { 
     // TODO Auto-generated method stub 
     return null; 
    } 

    @Override 
    protected void onTimerEnabled() { 
     // TODO Auto-generated method stub 

    } 

    @Override 
    protected void onTimerDisabled() { 
     // TODO Auto-generated method stub 

    } 


} 

該代碼被返回一個EVENTID這意味着它被添加到事件日曆裝置,但沒有事件被顯示在日曆。 我們可以做什麼來顯示設備日曆上添加的事件?

+0

你有<使用許可權的android:NAME = 「android.permission.READ_CALENDAR」/> \t <使用許可權的android:NAME = 「android.permission.WRITE_CALENDAR」/> 權限? – Wildcopper 2014-09-19 12:23:47

+0

@Wildcopper是這已經添加。 – 2014-09-19 12:24:55

回答

2

這裏是我的代碼(包括最後的「強制同步」)在日曆中打開和寫入。也許你可以在那裏找到你的答案,它的工作完美無瑕。

import java.util.Date; 

import android.accounts.Account; 
import android.accounts.AccountManager; 
import android.content.ContentResolver; 
import android.content.ContentValues; 
import android.database.Cursor; 
import android.net.Uri; 
import android.os.Bundle; 
import android.provider.CalendarContract.Calendars; 
import android.provider.CalendarContract.Events; 

public class TVCalendar { 

    public static final String TAG = "TVCalendar"; 

    // Projection array. Creating indices for this array instead of doing 
    // dynamic lookups improves performance. 
    public static final String[] EVENT_PROJECTION = new String[] { 
     Calendars._ID,       // 0 
     Calendars.ACCOUNT_NAME,     // 1 
     Calendars.CALENDAR_DISPLAY_NAME,   // 2 
     Calendars.OWNER_ACCOUNT     // 3 
    }; 

    // The indices for the projection array above. 
    private static final int PROJECTION_ID_INDEX = 0; 
    private static final int PROJECTION_ACCOUNT_NAME_INDEX = 1; 
    private static final int PROJECTION_DISPLAY_NAME_INDEX = 2; 
    private static final int PROJECTION_OWNER_ACCOUNT_INDEX = 3; 

    static public class CalendarEntry { 
     String room; 
     String name; 
     Date start; 
     Date end; 
     String description; 
     public CalendarEntry (String pRoom, String pName, Date pStart, Date pEnd, String pDesc) { 
      room=pRoom; 
      name=pName; 
      start=pStart; 
      end=pEnd; 
      description=pDesc; 
     } 
    } 

    static String lastCalName=""; 
    static long  lastCalId=-1; 

    static public long openCalendar(String calName,String owner) { 
     if ((lastCalId>0) && (lastCalName.equals(calName))) 
      return lastCalId; 
     // Run query 
     TVLog.i(TAG, "Querying Calendar "+calName+" owned by "+owner); 
     Cursor cur = null; 
     ContentResolver cr = TVPrefs.mainActivity.getContentResolver(); 
     Uri uri = Calendars.CONTENT_URI; 
//  String selection = "((" + Calendars.ACCOUNT_NAME + " = ?) AND (" 
//        + Calendars.ACCOUNT_TYPE + " = ?) AND (" 
//        + Calendars.OWNER_ACCOUNT + " = ?))"; 
//  String[] selectionArgs = new String[] {owner, "com.google", 
//    owner}; 
     // Submit the query and get a Cursor object back. 
     cur = cr.query(uri, EVENT_PROJECTION, null, null, null); 
     // Use the cursor to step through the returned records 
     while (cur.moveToNext()) { 
      long calID = 0; 
      String displayName = null; 
      String accountName = null; 
      String ownerName = null; 

      // Get the field values 
      calID = cur.getLong(PROJECTION_ID_INDEX); 
      displayName = cur.getString(PROJECTION_DISPLAY_NAME_INDEX); 
      accountName = cur.getString(PROJECTION_ACCOUNT_NAME_INDEX); 
      ownerName = cur.getString(PROJECTION_OWNER_ACCOUNT_INDEX); 

      TVLog.i(TAG, "Display: "+displayName+" Account: "+accountName+" Owner: "+ownerName); 

      if (displayName.equals(calName)) { 
       lastCalId=calID; 
       lastCalName=calName; 
       return calID; 
      } 

     } 
     return -1; 
    } 

    static public boolean addEvent(String calName, String ownerName, String timeZone, CalendarEntry entry) { 
     long calId=openCalendar(calName,ownerName); 
     TVLog.i(TAG, "Calendar ID: "+calId); 
     if (calId>=0) { 
      // Write to the Calendar 
      ContentResolver cr = TVPrefs.mainActivity.getContentResolver(); 
      ContentValues values = new ContentValues(); 
      values.put(Events.DTSTART, entry.start.getTime()); 
      values.put(Events.DTEND, entry.end.getTime()); 
      values.put(Events.TITLE, entry.name); 
      values.put(Events.DESCRIPTION, entry.description); 
      values.put(Events.CALENDAR_ID, calId); 
      values.put(Events.EVENT_TIMEZONE, timeZone); 
      values.put(Events.EVENT_LOCATION, entry.room); 
      values.put(Events.ACCESS_LEVEL, Events.ACCESS_PUBLIC); 
      //Uri uri 
      cr.insert(Events.CONTENT_URI, values); 

      // Force a sync 
      Bundle extras = new Bundle(); 
      extras.putBoolean(ContentResolver.SYNC_EXTRAS_EXPEDITED, true); 
      extras.putBoolean(ContentResolver.SYNC_EXTRAS_MANUAL, true); 
      AccountManager am = AccountManager.get(TVPrefs.mainActivity); 
      Account[] acc = am.getAccountsByType("com.google"); 
      Account account = null; 
      if (acc.length>0) { 
       account=acc[0]; 
       ContentResolver.requestSync(account, "com.android.calendar", extras); 
      } 

      return true; 

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

     } 
     return false; 
    } 
} 
+0

這些方法是靜態的btw,不要「按原樣」使用它,它對我的​​應用程序很特別。 – Wildcopper 2014-09-19 12:30:20

+0

儘管這只是一個奇怪的問題,但現在事件在日曆中顯示爲生日! – MRX 2017-06-19 06:39:04