2012-12-05 28 views
4

设备出厂重置后。 我想检索日历显示名称(通过下面的代码),它返回,没有日历。 但是当打开设备日历应用程序至少一次时,默认电话日历将被正确检索。Sony Xperia T(ST26)日历帐户问题

有没有办法检索日历(尤其是默认),而无需打开设备日历应用程序?

在此先感谢。

这里是设备上获取日历的代码存在:

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; 
    } 
+0

我需要创建标签 “的Xperia” 的帮助。来自索尼开发者网站的报价[我怎么接触](http://developer.sonymobile.com/about/how-do-i-get-in-contact/)。 '对于使用我们的任何手机,工具和SDK的开发人员,我们建议您在Stack Overflow上发布任何 问题。我们有一个开发团队经常监控,贡献 ,并在Stack Overflow上回复关于我们的产品和工具的问题。所以,如果你有一个 的问题,只要确保你用适当的产品名称或“Xperia”标记。那么我们应该是 能够回答你的问题,并为你提供指导。 –

回答

2

我解决了这个问题。

使用此代码在我的活动:

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"); 
1

我认为设备的日历应用程序必须安装当你打开它日历您工厂后打开它之前可能无法使用重启。

我认为你不希望用户不得不打开日历应用程序。如果您不介意日历应用程序在后台打开,则可以考虑通过Service打开它,然后立即关闭,以便用户不会注意到设备日历将可用。

android-codes-examples.blogspot.in/2011/11/...检查此链接是否有用?

+0

谢谢。但我不想中断用户体验。是否可以关闭我的应用程序/服务中的另一个应用程序? –

+0

是的,服务不会中断用户体验,因为它在后台,并且由于控件仍在您的应用中,您可以停止它。所以启动它,然后马上停止。 – adarsh

+0

但打开日历应用程序将! –