【问题标题】:how to integrate default app of android in our app programatically如何以编程方式将android的默认应用程序集成到我们的应用程序中
【发布时间】:2011-04-05 05:27:44
【问题描述】:

是否可以将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 权限。

【问题讨论】:

  • 你需要更具体。
  • 我的意思是,如果我想以编程方式在我的应用程序中使用默认的日历应用程序或其他 Android 的默认应用程序。它怎么可能....

标签: android


【解决方案1】:

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

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

例子:

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

【讨论】:

  • 我使用了第二篇文章,因为它看起来最合适。它在设备上工作,但也不显示日历。任何想法..?
  • 如果你能告诉我你想要完成什么,我可能会更好地帮助你:)
  • 我只想显示默认日历并通过我的应用程序控制其事件。希望你明白我的意思..
  • 您使用的是哪个内容提供者 内容提供者 URI 旧:content://calendar/ 新:content://com.android.calendar/
  • 在上述代码中,将“content://calendar/”替换为“content://com.android.calendar/”
【解决方案2】:
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);

【讨论】:

  • 找不到你。实际上我们可以用上面的代码做什么。你能解释一下吗..?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-06-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多