gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 18:20:51 25 4
gpt4 key购买 nike

是否可以通过编程方式将 Android 的默认应用程序(如 Calendar)集成到我们的应用程序中。谁能详细解释一下....

我使用的两个类是: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 权限。

最佳答案

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

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

关于android - 如何以编程方式将 android 的默认应用程序集成到我们的应用程序中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5547606/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com