我們正在研究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這意味着它被添加到事件日曆裝置,但沒有事件被顯示在日曆。 我們可以做什麼來顯示設備日曆上添加的事件?
你有<使用許可權的android:NAME = 「android.permission.READ_CALENDAR」/> \t <使用許可權的android:NAME = 「android.permission.WRITE_CALENDAR」/> 權限? – Wildcopper 2014-09-19 12:23:47
@Wildcopper是這已經添加。 – 2014-09-19 12:24:55