4

デバイスの工場出荷時のリセット後。カレンダーの表示名を(以下のコードで)取得しようとすると、カレンダーがないことが返されます。ただし、デバイスのカレンダーアプリケーションを少なくとも1回開くと、デフォルトの電話カレンダーが正しく取得されます。

デバイスのカレンダーアプリケーションを開かずにカレンダー(特にデフォルト)を取得する方法はありますか?

前もって感謝します。

デバイスに存在するカレンダーを取得するためのコードは次のとおりです。

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

2 に答える 2

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");
于 2012-12-18T16:39:28.777 に答える
1

デバイスカレンダーアプリケーションは、開いたときにカレンダーをインストールしている必要があると思います。これは、工場出荷時のリセット後に開く前に使用できない場合があります。

ユーザーがカレンダーアプリケーションを開く必要はないと思います。カレンダーアプリケーションがバックグラウンドで開かれていてもかまわない場合はService、ユーザーが気付かないように、デバイスカレンダーが利用できるように、アプリケーションを開いてからすぐに閉じることを検討できます。

android-codes-examples.blogspot.in/2011/11/…このリンクをチェックしてください、それは役に立ちますか?

于 2012-12-12T15:23:12.783 に答える