Sony Xperia T Calendar Release (ST26)

After factory reset the device. I am trying to get the names of the calendars displayed (by the code below), it returns that there are no calendars. but when you open the device’s Calendar application at least once, the default calendar will be restored correctly.

Is there a way to get calendars (especially the default) without opening the device’s Calendar app?

Thanks in advance.

Here is the code for getting calendars on the device:

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; } 
+4
source share
2 answers

I solved the problem.

using this code in my activity:

 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 no need because of FLAG_ACTIVITY_NO_HISTORY: ActivityManager activityManager = (ActivityManager) MainScreen.this.getSystemService(Context.ACTIVITY_SERVICE); activityManager.killBackgroundProcesses("com.android.calendar"); 
+2
source

I think that the device’s calendar application should set the calendar when it is opened, which may not be available before opening it after factory reset.

I think you do not want the user to open the calendar application. If you do not mind that the calendar application is open in the background, you can open it through the Service , and then close it soon so that the user does not notice and the device’s calendar is available.

android-codes-examples.blogspot.in/2011/11 / ... Check this link, is it useful?

+1
source

All Articles