请提供一个具有添加日程、查看日历的安卓日历代码
时间: 2024-03-08 19:51:01 浏览: 89
android日历代码
好的,以下是一个简单的Android日历应用的代码示例,包含添加日程和查看日历的功能。具体实现是通过使用系统自带的CalendarProvider来实现的。
首先是布局文件calendar_activity.xml:
```xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="@+id/add_event_button"
android:text="Add Event"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<Button
android:id="@+id/view_calendar_button"
android:text="View Calendar"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
```
接下来是Java代码CalendarActivity.java:
```java
import android.content.ContentValues;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.CalendarContract;
import android.provider.CalendarContract.Events;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import java.util.Calendar;
public class CalendarActivity extends AppCompatActivity {
private static final String[] EVENT_PROJECTION = new String[] {
CalendarContract.Events._ID,
CalendarContract.Events.TITLE,
CalendarContract.Events.DESCRIPTION,
CalendarContract.Events.DTSTART,
CalendarContract.Events.DTEND,
CalendarContract.Events.EVENT_LOCATION,
CalendarContract.Events.EVENT_TIMEZONE
};
private static final int PROJECTION_ID_INDEX = 0;
private static final int PROJECTION_TITLE_INDEX = 1;
private static final int PROJECTION_DESCRIPTION_INDEX = 2;
private static final int PROJECTION_DTSTART_INDEX = 3;
private static final int PROJECTION_DTEND_INDEX = 4;
private static final int PROJECTION_EVENT_LOCATION_INDEX = 5;
private static final int PROJECTION_EVENT_TIMEZONE_INDEX = 6;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.calendar_activity);
Button addEventButton = findViewById(R.id.add_event_button);
Button viewCalendarButton = findViewById(R.id.view_calendar_button);
addEventButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
addCalendarEvent();
}
});
viewCalendarButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
viewCalendar();
}
});
}
private void addCalendarEvent() {
// Create a new calendar instance and set the time
Calendar beginTime = Calendar.getInstance();
beginTime.set(2019, 0, 1, 9, 0);
Calendar endTime = Calendar.getInstance();
endTime.set(2019, 0, 1, 10, 0);
// Create a new event
ContentValues values = new ContentValues();
values.put(Events.TITLE, "New Year's Day");
values.put(Events.DESCRIPTION, "Celebrate the start of the New Year.");
values.put(Events.DTSTART, beginTime.getTimeInMillis());
values.put(Events.DTEND, endTime.getTimeInMillis());
values.put(Events.EVENT_LOCATION, "New York City");
values.put(Events.CALENDAR_ID, 1);
values.put(Events.EVENT_TIMEZONE, Calendar.getInstance().getTimeZone().getID());
// Insert the event into the calendar
Uri uri = getContentResolver().insert(Events.CONTENT_URI, values);
// Display a toast message indicating that the event was added
Toast.makeText(this, "Event added to calendar", Toast.LENGTH_SHORT).show();
}
private void viewCalendar() {
// Specify the date range for the events to retrieve
Calendar beginTime = Calendar.getInstance();
beginTime.set(2019, 0, 1, 0, 0);
Calendar endTime = Calendar.getInstance();
endTime.set(2019, 0, 31, 23, 59);
// Define the URI for the events
Uri uri = CalendarContract.Events.CONTENT_URI;
String selection = "((" + CalendarContract.Events.DTSTART + " >= ?) AND (" + CalendarContract.Events.DTEND + " <= ?))";
String[] selectionArgs = new String[] {Long.toString(beginTime.getTimeInMillis()), Long.toString(endTime.getTimeInMillis())};
// Retrieve the events from the calendar provider
Cursor cursor = getContentResolver().query(uri, EVENT_PROJECTION, selection, selectionArgs, null);
// Display the events in a toast message
StringBuilder builder = new StringBuilder();
while (cursor.moveToNext()) {
String title = cursor.getString(PROJECTION_TITLE_INDEX);
String description = cursor.getString(PROJECTION_DESCRIPTION_INDEX);
String location = cursor.getString(PROJECTION_EVENT_LOCATION_INDEX);
long start = cursor.getLong(PROJECTION_DTSTART_INDEX);
long end = cursor.getLong(PROJECTION_DTEND_INDEX);
builder.append(title).append("\n").append(description).append("\n").append(location).append("\n").append(start).append("\n").append(end).append("\n\n");
}
Toast.makeText(this, builder.toString(), Toast.LENGTH_LONG).show();
// Close the cursor
cursor.close();
}
}
```
这个示例中,我们首先在布局文件中定义了两个按钮,一个用于添加日程,一个用于查看日历。在Java代码中,我们使用了系统自带的CalendarProvider来实现这两个功能。具体来说,我们通过使用ContentValues对象来创建一个新的日程事件,并将其插入到日历中。在查看日历时,我们使用ContentResolver对象来查询日历事件,并将结果显示在Toast消息中。
需要注意的是,为了使用CalendarProvider,我们需要在AndroidManifest.xml文件中添加以下权限:
```xml
<uses-permission android:name="android.permission.READ_CALENDAR"/>
<uses-permission android:name="android.permission.WRITE_CALENDAR"/>
```
同时,我们还需要在同一个文件中添加以下provider:
```xml
<provider
android:name="com.android.calendar.CalendarProvider2"
android:authorities="com.android.calendar"
android:exported="false"
android:readPermission="android.permission.READ_CALENDAR"
android:writePermission="android.permission.WRITE_CALENDAR"
tools:ignore="ExportedContentProvider"/>
```
这样就完成了一个简单的Android日历应用的开发。
阅读全文