4

After factory reset of the device. I'm trying to retrieve the calendars display names(by the code below), it returns that there is no calendars. but when opening the device Calendar application at least one time, the default phone calendar will be retrieved correctly.

Is there any way to retrieve the calendars (especially the default ) without opening the device Calendar application?

Thanks in advance.

Here is the code for retrieving calendars exist 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;
   }
naif.ult
  • 186
  • 6
  • I need help with creating tag "Xperia". quote from Sony developer site [how-do-i-get-in-contact](http://developer.sonymobile.com/about/how-do-i-get-in-contact/). 'For developers working with any of our phones, tools and SDKs, we recommend you to post any questions on Stack Overflow. We have a team of developers that constantly monitor, contribute and reply to questions regarding our products and tools at Stack Overflow. So if you have a question, just make sure you tag it with a proper product name or "Xperia". Then we should be able to catch your question, and provide you with guidance.' – naif.ult Dec 12 '12 at 15:31

2 Answers2

2

I solved the issue.

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's no need because of FLAG_ACTIVITY_NO_HISTORY:
ActivityManager activityManager = (ActivityManager) MainScreen.this.getSystemService(Context.ACTIVITY_SERVICE);
activityManager.killBackgroundProcesses("com.android.calendar");
naif.ult
  • 186
  • 6
1

I think the device calendar application must be installing a calendar when you open it which may not be available before you open it after a factory reset.

I think you don't want the user to have to open the calendar application. If you don't mind the calendar application being opened in background, you could consider opening it through a Service an then closing it soon so that the user won't notice and the device calendar would be available.

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

adarsh
  • 6,738
  • 4
  • 30
  • 52