2012-12-05 31 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

但打開日曆應用程序將! –