android - Sony Xperia T (ST26) 日历帐户问题

标签 android calendar sony

设备恢复出厂设置后。 我试图检索日历显示名称(通过下面的代码),它返回没有日历。 但至少打开一次设备日历应用程序时,将正确检索默认手机日历。

有什么方法可以在不打开设备日历应用程序的情况下检索日历(尤其是默认日历)?

提前致谢。

以下是用于检索设备上存在的日历的代码:

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

最佳答案

我解决了这个问题。

在我的 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");

关于android - Sony Xperia T (ST26) 日历帐户问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13720178/

相关文章:

c# - 带有 C# 的索尼相机 API

android - 索尼安卓电视上支持的 CSS-GRID 浏览器

android - 在 android 中没有获取 http post 请求的响应正文?

java - facebook sdk java查询使用图形API或url/http从actor_id获取名称

java - Android 日历序列化与 Java 6 不兼容

javascript - 如何根据描述对全日历事件进行颜色编码

java - 无法使用智能手环 2 从 Google Fit API 获取心率数据

android - 如何区分谷歌眼镜和其他蓝牙设备?

java - 按钮 onClickListener 在 Android 中无法播放声音

ios - 在 UICollectionView 中加载和显示随机单元格