如何以编程方式在我们的应用程序中集成android的默认应用程序

时间:2011-04-05 05:27:44

标签: android

是否可以以编程方式在我们的应用中集成Android的默认应用(例如日历)。 任何人都可以详细解释我......

我使用的两个类是: 的 MainActivity.java

public class MainActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        Example.readCalendar(this);
    }
}

和另一个类 Example.java

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://calendar/calendars"),
                (new String[] { "_id", "displayName", "selected" }), null, null, null);
        // For a full list of available columns see http://tinyurl.com/yfbg76w

        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("Id: " + _id + " Display Name: " + displayName + " Selected: " + selected);
            calendarIds.add(_id);
        }

        // For each calendar, display all the events from the previous week to the end of next week.        
        for (String id : calendarIds) {
            Uri.Builder builder = Uri.parse("content://calendar/instances/when").buildUpon();
            long now = new Date().getTime();
            ContentUris.appendId(builder, now - DateUtils.WEEK_IN_MILLIS);
            ContentUris.appendId(builder, now + DateUtils.WEEK_IN_MILLIS);

            Cursor eventCursor = contentResolver.query(builder.build(),
                    new String[] { "title", "begin", "end", "allDay"}, "Calendars._id=" + id,
                    null, "startDay ASC, startMinute ASC"); 
            // For a full list of available columns see http://tinyurl.com/yfbg76w

            while (eventCursor.moveToNext()) {
                final String title = eventCursor.getString(0);
                final Date begin = new Date(eventCursor.getLong(1));
                final Date end = new Date(eventCursor.getLong(2));
                final Boolean allDay = !eventCursor.getString(3).equals("0");

                System.out.println("Title: " + title + " Begin: " + begin + " End: " + end +
                        " All Day: " + allDay);
            }
        }
    }


}

我也添加了READ_CALENDAR权限。

2 个答案:

答案 0 :(得分:4)

这些文章可能会对您有所帮助。

  1. Working with the Android Calendar
  2. Accessing the internal calendar database inside Google Android applications
  3. Calendar API (android.provider.Calendar)
  4. 示例:

    static String contentProvider;
    static Uri remindersUri;
    static Uri eventsUri;
    static Uri calendars;
    
    if(Build.VERSION.RELEASE.contains(”2.2″))
        contentProvider = “com.android.calendar”;
    else
        contentProvider = “calendar”;
    
    remindersUri = Uri.parse(String.format(”content://%s/reminders”,contentProvider));
    eventsUri = Uri.parse(String.format(”content://%s/events”,contentProvider));
    calendars = Uri.parse(String.format(”content://%s/calendars”,contentProvider));
    

答案 1 :(得分:-1)

Intent intent = new Intent(Intent.ACTION_EDIT);  
intent.setType("vnd.android.cursor.item/event");
intent.putExtra("title", "Some title");
intent.putExtra("description", "Some description");
intent.putExtra("beginTime", eventStartInMillis);
intent.putExtra("endTime", eventEndInMillis);
startActivity(intent);