设备恢复出厂设置后.
我正在尝试检索日历显示名称(通过下面的代码),它返回没有日历.
但至少打开设备日历应用程序一次时,将正确检索默认电话日历.
有没有办法在不打开设备日历应用程序的情况下检索日历(尤其是默认日历)?
提前致谢.
以下是检索设备上存在的日历的代码:
private Uri getCalendarUri() { return Uri.parse(Integer.parseInt(Build.VERSION.SDK) > 7 ? "content://com.androID.calendar/calendars" : "content://calendar/calendars"); } private String[] getCalendars(Context context) { String[] res = null; ContentResolver contentResolver = context.getContentResolver(); Cursor cursor = null; try { cursor = contentResolver.query( getCalendarUri(), Integer.parseInt(Build.VERSION.SDK) > 13 ? new String[]{"_ID", "calendar_displayname"} : new String[]{"_ID", "displayname"}, null, null, "_ID ASC"); if (cursor.getCount() > 0) { res = new String[cursor.getCount()]; int i = 0; while (cursor.movetoNext()) { res[i] = cursor.getString(0) + ": " + cursor.getString(1); i++; } } } catch (Exception e) { e.printstacktrace(); } finally { if (cursor != null) cursor.close(); } return res; }
解决方法:
我解决了这个问题.
在我的活动中使用此代码:
private static boolean calendar_opened = false;private voID openCalendar() { String[] calendars = getCalendars(this); if (!calendar_opened && calendars != null && calendars.length <= 0) { new Timer().schedule(new TimerTask() { @OverrIDe public voID run() { runOnUiThread(new Runnable() { public voID run() { try { //bring back my activity to foreground final Intent tmpIntent = (Intent) MainScreen.this.getIntent().clone(); tmpIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_top | Intent.FLAG_ACTIVITY_SINGLE_top); tmpIntent.setClass(MyExams.getInstance(), MainScreen.class); PendingIntent.getActivity(MyExams.getInstance(), 0, tmpIntent, PendingIntent.FLAG_UPDATE_CURRENT).send(); } catch (Exception e) { } } }); } }, 100 );//time is your dissection Intent i = new Intent(); i.setClassname("com.androID.calendar", "com.androID.calendar.LaunchActivity"); i.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY); startActivity(i); calendar_opened = true; }}//After my activity is on foreground I killed the calendar using this code, even there's no need because of FLAG_ACTIVITY_NO_HISTORY:ActivityManager activityManager = (ActivityManager) MainScreen.this.getSystemService(Context.ACTIVITY_SERVICE);activityManager.killBackgroundProcesses("com.androID.calendar");
总结 以上是内存溢出为你收集整理的android – 索尼Xperia T(ST26)日历账号问题全部内容,希望文章能够帮你解决android – 索尼Xperia T(ST26)日历账号问题所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)