开发者

calendar , triggering , web service access in android

Hello m I am new to android.. I have samsung galaxy 3 and android 2.1 installed in it.. I want to build an widget as follows..

1.I want to access calendar of phone.. (googled a lot found diffrent answers but none wokrin..) (jimblackler.net/blog/?p=151 not working).. since no public api is available it will be ok even if am able to use google calendar(the one from web).. letme know whichever is easier to implement. I just want to read events..

2.How do I trigger eve开发者_JAVA百科nts on a specific time.. say when it is 3:00hrs it should call a function.. Is it possible ? if so how

3.I want to send two strings to a server and recieve andother text back as result. which would be the easiest way to code ?? have an c# web service..? or just use a php script on server accepting few parameters?

thanks a lot. :)


First Question: Calendar access: This is based on the content://calendar/calendars URI. First, make sure your application defines the permission android.permission.READ_CALENDAR.

Get the calendars IDs and display names:

ContentResolver myContentResolver = getContentResolver();

final Cursor cursor = myContentResolver.query(Uri.parse("content://calendar/calendars"),
        (new String[] { "_id", "displayName", "selected" }), null, null, null);

while (cursor.moveToNext()) {

    final String _id = cursor.getString(0);
    final String displayName = cursor.getString(1);
    final Boolean selected = !cursor.getString(2).equals("0");
}

Work on a specific calendar:

Uri.Builder myUriBuilder = Uri.parse("content://calendar/instances/when").buildUpon();
long currentTime = new Date().getTime();
ContentUris.appendId(myUriBuilder , currentTime  - DateUtils.WEEK_IN_MILLIS);
ContentUris.appendId(myUriBuilder, currentTime + DateUtils.WEEK_IN_MILLIS);

Cursor eventCursor = myContentResolver.query(myUriBuilder.build(),
        new String[] { "title", "begin", "end", "allDay"}, "Calendars._id=" + id,
        null, "startDay ASC, startMinute ASC");

while (eventCursor.moveToNext()) {
    // fields:
    // Event title - String - 0
    String eventTitle = eventCursor.getString(0);
    // Event start time - long - 1
    Date startTime = new Date(eventCursor.getLong(1));
    // Event end time - login - 2
    Date endTime = new Date(eventCursor.getLong(2));
    // Is it an All Day Event - String - 3 (0 is false, 1 is true)
    Boolean allDay = !eventCursor.getString(3).equals("0");

}


Second Question: Scheduling Events: Basically, you should use the AlarmManager class, see the android SDK for details. Example to set the event 3 hours from now:

 Calendar cal = Calendar.getInstance();
 cal.add(Calendar.HOUR, 3);
 Intent intent = new Intent(this, MyAlarmHandler.class);
 intent.putExtra("alarm_message", "Wake up!");
 int requestCode = 3824723; // this is not currently used by Android.
 PendingIntent pIntent = PendingIntent.getBroadcast(this, requestCode, intent, PendingIntent.FLAG_UPDATE_CURRENT);
 AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
 alarmManager.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), pIntent);


Third Question: Yes, I suggest the PHP approach

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜