The*_*r12 3 android calendar android-contentresolver android-cursor
我正在尝试以编程方式将事件添加到用户的日历中.我在StackOverflow和其他地方跟踪了无数的指南,但是没有一个会在我的日历中显示那个该死的事件.这是我目前的代码:
public void saveGamesToCalendar() {
showCalendarPopup();
}
private void showCalendarPopup() {
final ContentResolver cr;
final Cursor result;
final Uri uri;
List<String> listCals = new ArrayList<String>();
final String[] projection = new String[] {CalendarContract.Calendars._ID, CalendarContract.Calendars.ACCOUNT_NAME, CalendarContract.Calendars.CALENDAR_DISPLAY_NAME, CalendarContract.Calendars.NAME,};
uri = CalendarContract.Calendars.CONTENT_URI;
cr = context.getContentResolver();
result = cr.query(uri, projection, null, null, null);
if(result.getCount() > 0 && result.moveToFirst()) {
do {
listCals.add(result.getString(result.getColumnIndex(CalendarContract.Calendars.NAME)));
} while(result.moveToNext());
}
CharSequence[] calendars = listCals.toArray(new CharSequence[listCals.size()]);
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setTitle("Calendar to use:");
builder.setItems(calendars, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog,int itemCal) {
Log.e("SettingsActivity", "CalendarID: " + itemCal);
loopThroughGames(itemCal);
};
});
AlertDialog alert = builder.create();
alert.show();
}
private void loopThroughGames(int whichCalendar) {
ArrayList<Model_Game> games = memoryManager.getGames();
// for(Game e: games)
Log.e("Game", games.get(101).toString());
addGameToCalendar(games.get(101), whichCalendar);
Log.e("ID", "" + whichCalendar);
}
private void addGameToCalendar(Model_Game game,int whichCalendar) {
ContentResolver cr = context.getContentResolver();
ContentValues values = new ContentValues();
try {
values.put(CalendarContract.Events.CALENDAR_ID, whichCalendar);
values.put(CalendarContract.Events.TITLE, "Hockey- " + game.getLeague() + ": " + game.getTeamH() + " vs " + game.getTeamA());
values.put(CalendarContract.Events.EVENT_LOCATION, "Twin Rinks Ice Arena- " + game.getRink() + " Rink");
values.put(CalendarContract.Events.DTSTART, "" + game.getCalendarObject().getTimeInMillis());
//values.put(CalendarContract.Events.DTEND, "" + game.getCalendarObject().getTimeInMillis() + 5400000);
values.put(CalendarContract.Events.DURATION, "" + 5400000);
values.put(CalendarContract.Events.EVENT_TIMEZONE, TimeZone.getDefault().getID());
cr.insert(CalendarContract.Events.CONTENT_URI, values);
} catch(Exception e) {
Log.e("Error", e.getMessage());
toast(e.getMessage());
}
}
Run Code Online (Sandbox Code Playgroud)
基本上正在发生的是用户按下操作栏按钮,该按钮调用调用该saveGamesToCalendar()方法的ShowCalendarPopup()方法.此方法显示带有用户日历列表的标准对话框,当用户选择其中一个日历时,将loopThroughGames()使用所选日历的ID作为参数调用该方法.当一切正常时,此方法将把所有用户的游戏写入日历,但出于测试目的,它只写一个.该addGameToCalendar()方法采用游戏对象,该对象包含许多值,如时间,标题,位置,日期等,并使用Web上许多不同位置概述的标准方式将其插入日历中.
我没有收到此代码的错误消息(除了我自己发送到错误日志的那些),所以我不知道为什么这段代码不起作用.没有错误,只是游戏永远不会出现在我的日历中.我在清单中正确设置了权限,所以我知道这不是问题.
有没有人能解决这个恼人的问题?谢谢你的帮助!
[重新编辑]]这就是我所做的,它就像一个魅力......
Cursor cursor = null ;
String[] projection = new String[] {
CalendarContract.Calendars._ID,
CalendarContract.Calendars.ACCOUNT_NAME,};
ContentResolver cr = context2.getContentResolver();
cursor = cr.query(Uri.parse("content://com.android.calendar/calendars"), projection, null, null, null);
if ( cursor.moveToFirst() ) {
final String[] calNames = new String[cursor.getCount()];
calIds = new int[cursor.getCount()];
for (int i = 0; i < calNames.length; i++) {
calIds[i] = cursor.getInt(0);
calNames[i] = cursor.getString(1);
cursor.moveToNext();
}
}
try {
ContentValues values = new ContentValues();
// int apiLevel = android.os.Build.VERSION.SDK_INT;
// if(apiLevel<14){
// values.put("visibility", 0);
//
// }
values.put(Events.DTSTART, stTime);
values.put(Events.DTEND, enTime);
values.put(Events.TITLE, category);
values.put(Events.DESCRIPTION, description);
values.put(Events.CALENDAR_ID, calIds[0]);
values.put(Events.EVENT_LOCATION,place);
values.put(Events.EVENT_TIMEZONE,tZone);
mInsert = cr.insert(CalendarContract.Events.CONTENT_URI, values);
Toast.makeText(context2, "Event added Successfully",
Toast.LENGTH_LONG).show();
} catch (Exception e) {
e.printStackTrace();
Toast.makeText(context2, "Exception: " + e.getMessage(),
Toast.LENGTH_SHORT).show();
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5691 次 |
| 最近记录: |