[英]event not showing on android device calendar
我們正在開發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,表示它正在向設備日歷添加事件,但是日歷上沒有顯示事件。 我們該怎么做才能在設備的日歷上顯示添加的事件?
這是我的代碼(在結尾處包括“強制同步”),用於打開和寫入日歷。 也許您可以在那找到答案,它可以完美地工作。
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;
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.