其他分享
首页 > 其他分享> > android – 启动日历活动

android – 启动日历活动

作者:互联网

我只是想从我的Activity中启动Calendar Activity.我在Button OnClickListener中有以下代码:

Intent calIntent = new Intent(Intent.ACTION_INSERT);
calIntent.setData(Uri.parse("content://com.android.calendar/events/"));  
startActivity(calIntent);

但是,当我单击按钮时,我的设备挂起,然后我必须强制关闭不响应的应用程序.

解决方法:

这是因为Calendar 内容Uri因android的不同版本(API级别)而异.尝试使用此代码获取各个API级别的日历Uri.

/*
  * Determines if it's a pre 2.1 or a 2.2 calendar Uri, and returns the Uri
  */
 private String getCalendarUriBase(Context con) {
     String calendarUriBase = null;
     Uri calendars = Uri.parse("content://calendar/calendars");
     Cursor managedCursor = null;
     try {
         managedCursor = managedQuery(calendars, null, null, null, null);
     } catch (Exception e) {
         // eat
     }

     if (managedCursor != null) {
         calendarUriBase = "content://calendar/";
     } else {
         calendars = Uri.parse("content://com.android.calendar/calendars");
         try {
             managedCursor = managedQuery(calendars, null, null, null, null);
         } catch (Exception e) {
             // statement to print the stacktrace
         }

         if (managedCursor != null) {
             calendarUriBase = "content://com.android.calendar/";
         }

     }

     return calendarUriBase;
 }

标签:android,android-calendar
来源: https://codeday.me/bug/20190704/1375547.html