谁能帮我下面的代码,问题出在
Uri.Builder builder = Uri.parse("content://com.android.calendar/instances/when").buildUpon();
引发 Unkown URL 非法参数异常,见下文
谢谢
格雷厄姆
代码获取所有日历的列表,使用日历ID遍历所有日历
package gb.org;
import java.util.Calendar;
import java.util.Date;
import java.util.HashSet;
import android.content.ContentResolver;
import android.content.ContentUris;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.text.format.DateUtils;
import android.os.SystemClock;
public class Example {
public static void readCalendar(Context context) {
ContentResolver contentResolver = context.getContentResolver();
// Fetch a list of all calendars synced with the device, their display names and whether the
// user has them selected for display.
final Cursor cursor = contentResolver.query(Uri.parse("content://com.android.calendar/calendars"),
(new String[] { "_id", "displayName", "selected" }), null, null, null);
HashSet<String> calendarIds = new HashSet<String>();
while (cursor.moveToNext()) {
final String _id = cursor.getString(0);
final String displayName = cursor.getString(1);
final Boolean selected = !cursor.getString(2).equals("0");
System.out.println("Calendar is " + _id + " Display Name: " + displayName + " Selected: " + selected);
//DETERMINE IF CALENDAR IS SELECTED IF YES THEN USE IT
calendarIds.add(_id);
}
// query the instance table
for (String id : calendarIds) {
Uri.Builder builder = Uri.parse("content://com.android.calendar/instances/when").buildUpon();
Cursor eventCursor = contentResolver.query(builder.build(),
new String[] { "title", "begin", "end", "allDay"}, "Calendars._id=" + id,
null, "startDay ASC, startMinute ASC");
while (eventCursor.moveToNext()) {
long timeNow=System.currentTimeMillis(); //the current time
final String title = eventCursor.getString(0);
final long begin = eventCursor.getLong(1);
final Date begin_DATE = new Date(begin);
//final Date end = new Date(eventCursor.getLong(2));
final Date nowDATE = new Date(timeNow);
final long end = eventCursor.getLong(2);
final Date end_DATE = new Date(end);
final long allDay = eventCursor.getLong(3);
//function to check the time
//is there a calendar event today when I run the application
//if yes then print a toast message
//if not then do nothing - maybe a toast do nothing!
//Calendar Cnow = Calendar.getInstance();
//System.out.println("Calendar now means:" + Cnow.getTimeInMillis()); //time now
if(timeNow >= begin && timeNow <=end){
//check time block for meeting
System.out.println("BLOCK BLOCK BLOCK ");
System.out.println("The meeting is: " + title + "begin is " + begin_DATE + "end is" + end_DATE );
//call blocker function
}
else{
System.out.println("NO BLOCK NO BLOCK NO BLOCK ");
//stop and restart service return to main screen with dialog box
}
//System.out.println("begin:" + begin + " End: " + end + "all day :" + allDay + "time NOW" + timeNow );
//assign to variables for begin and end
Date d = new Date();
//work with time and data
//System.out.println("current date" + d.getDate() + "current hour" + d.getHours()
// + "current min" + d.getMinutes() + "current second" + d.getSeconds());
}
}
}
}
例外>>
> 12-02 14:58:42.097: E/AndroidRuntime(12298): FATAL EXCEPTION: main
> 12-02 14:58:42.097: E/AndroidRuntime(12298): java.lang.RuntimeException: Unable to start activity
> ComponentInfo{gb.org/gb.org.main}: java.lang.IllegalArgumentException:
> Unknown URL content://com.android.calendar/instances/when
> 12-02 14:58:42.097: E/AndroidRuntime(12298): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1651)
> 12-02 14:58:42.097: E/AndroidRuntime(12298): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1667)
> 12-02 14:58:42.097: E/AndroidRuntime(12298): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
> 12-02 14:58:42.097: E/AndroidRuntime(12298): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:935)
> 12-02 14:58:42.097: E/AndroidRuntime(12298): at android.os.Handler.dispatchMessage(Handler.java:99)
> 12-02 14:58:42.097: E/AndroidRuntime(12298): at android.os.Looper.loop(Looper.java:123)
> 12-02 14:58:42.097: E/AndroidRuntime(12298): at android.app.ActivityThread.main(ActivityThread.java:3687)
> 12-02 14:58:42.097: E/AndroidRuntime(12298): at java.lang.reflect.Method.invokeNative(Native Method)
> 12-02 14:58:42.097: E/AndroidRuntime(12298): at java.lang.reflect.Method.invoke(Method.java:507)
> 12-02 14:58:42.097: E/AndroidRuntime(12298): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:842)
> 12-02 14:58:42.097: E/AndroidRuntime(12298): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
> 12-02 14:58:42.097: E/AndroidRuntime(12298): at dalvik.system.NativeStart.main(Native Method)
> 12-02 14:58:42.097: E/AndroidRuntime(12298): Caused by: java.lang.IllegalArgumentException: Unknown URL
> content://com.android.calendar/instances/when
> 12-02 14:58:42.097: E/AndroidRuntime(12298): at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:144)
> 12-02 14:58:42.097: E/AndroidRuntime(12298): at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:114)
> 12-02 14:58:42.097: E/AndroidRuntime(12298): at android.content.ContentProviderProxy.bulkQueryInternal(ContentProviderNative.java:330)
> 12-02 14:58:42.097: E/AndroidRuntime(12298): at android.content.ContentProviderProxy.query(ContentProviderNative.java:366)
> 12-02 14:58:42.097: E/AndroidRuntime(12298): at android.content.ContentResolver.query(ContentResolver.java:262)
> 12-02 14:58:42.097: E/AndroidRuntime(12298): at gb.org.Example.readCalendar(Example.java:62)
> 12-02 14:58:42.097: E/AndroidRuntime(12298): at gb.org.main.onCreate(main.java:12)
> 12-02 14:58:42.097: E/AndroidRuntime(12298): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
> 12-02 14:58:42.097: E/AndroidRuntime(12298): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1615)
> 12-02 14:58:42.097: E/AndroidRuntime(12298): ... 11 more
显现
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="gb.org"
android:versionCode="1"
android:versionName="1.0">
- <application android:icon="@drawable/icon" android:label="@string/app_name" android:debuggable="true">
- <activity android:name=".main" android:label="@string/app_name">
- <intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
<uses-sdk android:minSdkVersion="7" />
<uses-permission android:name="android.permission.READ_CALENDAR" />
<android.permission android:name= "android.permission.WRITE_CALENDAR" />
</manifest>