android studio关于在自己的移动日历上插入事件

时间:2015-04-29 13:06:33

标签: android

程序运行时出错。但是,该事件不会插入到我自己的手机日历中。有谁知道这个的原因?我的手机是pentech a880。或者还有另一种插入事件的方法?

package com.example.lean.myapplication;


public class MyActivity extends Activity {
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.list_fragment);
    Button button = (Button) findViewById(R.id.button);
    button.setOnClickListener(new View.OnClickListener() {
                                  @Override
                                  public void onClick(View v) {
                                      Intent intent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
                                      startActivityForResult(intent, 2);
                                  }
                              }

    );
    addToCalendar(getApplicationContext(), "hello","2015/10/10 10:05:33" );
}


    public static void addToCalendar(Context oContext, final String title, final String eventStartDate)
    {
        String eventUriString = null;

        long startDate = new Date(eventStartDate).getTime();
        long endDate = new Date(eventStartDate).getTime() + 1000 * 60 * 60; // For next 1hr
        TimeZone timeZone = TimeZone.getDefault();

        ContentValues eventValues = new ContentValues();

        if (Build.VERSION.SDK_INT >= 1 && Build.VERSION.SDK_INT < 13)
        {
            eventUriString = "content://com.android.calendar/events";

            eventValues.put("calendar_id", 1);
            eventValues.put("title", title);
            eventValues.put("description", "");
            eventValues.put("eventLocation", "");

            eventValues.put("dtstart", startDate);
            eventValues.put("dtend", endDate);
            eventValues.put("eventTimezone", TimeZone.getDefault().getID());
            eventValues.put("eventStatus", "");
            eventValues.put("visibility", 3);
            eventValues.put("transparency", 0);
            eventValues.put("hasAlarm", 1);

            Uri eventUri = oContext.getApplicationContext().getContentResolver().insert(Uri.parse("content://com.android.calendar/events"), eventValues);
            long eventID = Long.parseLong(eventUri.getLastPathSegment());

            String reminderUriString = "content://com.android.calendar/reminders";

            ContentValues reminderValues = new ContentValues();

            reminderValues.put("event_id", eventID);
            reminderValues.put("minutes", 5);
            reminderValues.put("method", 1);

            oContext.getApplicationContext().getContentResolver().insert(Uri.parse(reminderUriString), reminderValues);

        }
        else if (Build.VERSION.SDK_INT >= 14 )
        {
            eventValues.put(CalendarContract.Events.DTSTART, startDate);
            eventValues.put(CalendarContract.Events.DTEND, endDate);
            eventValues.put(CalendarContract.Events.TITLE, title);
            eventValues.put(CalendarContract.Events.DESCRIPTION, "");
            eventValues.put(CalendarContract.Events.CALENDAR_ID, 3);
            eventValues.put(CalendarContract.Events.EVENT_TIMEZONE, timeZone.getID());

            oContext.getApplicationContext().getContentResolver().insert(Uri.parse("content://com.android.calendar/events"), eventValues);
        }

        Toast.makeText(oContext, "Event Created on : " + startDate, Toast.LENGTH_SHORT).show();
    }


    @Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.my, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();
    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}

}

1 个答案:

答案 0 :(得分:0)

也许您没有要求权限

     <?xml version="1.0" encoding="utf-8"?>
     <manifest xmlns:android="http://schemas.android.com/apk/res/android"...>
         ...
         <uses-permission android:name="android.permission.READ_CALENDAR" />
         <uses-permission android:name="android.permission.WRITE_CALENDAR" />
         ...
     </manifest>