日历事件显示在模拟器上,但不显示在设备上

时间:2019-11-21 10:22:55

标签: android android-emulator android-calendar

我遇到的情况是,我使用“ Android日历提供程序”编写了一段代码,应该将事件写入Android设备上的日历中。我已经分配了正确的权限,并使用了不同的Calendar_Id,但是该事件拒绝显示在设备的日历中。

杀手is是,当我在模拟器上运行它时,它才真正起作用-只是当我在设备上运行它时,这才是问题。

有人可以帮助我解决这个特定问题吗?

我设置的权限如下:

<uses-permission android:name="android.permission.READ_CALENDAR"/>
<uses-permission android:name="android.permission.WRITE_CALENDAR"/>

我的代码如下:

public class CalendarActivity extends AppCompatActivity {

    private static final int MY_PERMISSIONS_REQUEST_WRITE_CALENDER = 100;
    private static final int SETTING_REQUEST_CODE = 101;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        checkCalenderPermission();
    }


    private void checkCalenderPermission()
    {
        // Here, thisActivity is the current activity
        if (ContextCompat.checkSelfPermission(this,
                Manifest.permission.WRITE_CALENDAR)
                != PackageManager.PERMISSION_GRANTED) {

            // Permission is not granted
            // Should we show an explanation?
            System.out.println("Permission is not granted...");
            if (ActivityCompat.shouldShowRequestPermissionRationale(this,
                    Manifest.permission.WRITE_CALENDAR)) {
                // Show an explanation to the user *asynchronously* -- don't block
                // this thread waiting for the user's response! After the user
                // sees the explanation, try again to request the permission.
                System.out.println("Requesting permission and displaying reason...");
                startActivityForResult(new Intent(android.provider.Settings.ACTION_APPLICATION_DETAILS_SETTINGS, Uri.parse("package:" + BuildConfig.APPLICATION_ID)),SETTING_REQUEST_CODE);
            } else {
                // No explanation needed; request the permission
                System.out.println("Requesting permission and NOT displaying reason...");
                ActivityCompat.requestPermissions(this,
                        new String[]{Manifest.permission.WRITE_CALENDAR,Manifest.permission.READ_CALENDAR},
                        MY_PERMISSIONS_REQUEST_WRITE_CALENDER);

                // MY_PERMISSIONS_REQUEST_READ_CONTACTS is an
                // app-defined int constant. The callback method gets the
                // result of the request.
            }
        } else {
            // Permission has already been granted
            addCalenderEvent();
        }
    }



    private void addCalenderEvent()
    {
        System.out.println("Now into addCalendarEvent...");
        //You can change start and end dates with your ones and the dummy data as well

        Calendar startTime = Calendar.getInstance();

        Calendar endTime = Calendar.getInstance();

        endTime.add(Calendar.DAY_OF_MONTH,1);

        addAppointmentsToCalender(this,"Testing","ABCDFGHJK","Emulator", startTime);
    }



    public void addAppointmentsToCalender(Context context, String title, String desc, String place, Calendar calendar){

        System.out.println("Now into addAppointmentsToCalendar...");
        SimpleDateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy");

        String reminderDate = dateFormat.format(calendar.getTime());
        System.out.println("reminderDate = " + reminderDate);
        Log.e("Day before event start", reminderDate);
        String reminderDayStart = reminderDate + " 00:00:00";
        String reminderDayEnd = reminderDate + " 23:59:59";
        long startTimeInMilliseconds = 0, endTimeInMilliseconds = 0;

        try {
            SimpleDateFormat formatter = new SimpleDateFormat("MM/dd/yyyy hh:mm:ss");
            Date SDate = formatter.parse(reminderDayStart);
            Date EDate = formatter.parse(reminderDayEnd);
            startTimeInMilliseconds = SDate.getTime();
            endTimeInMilliseconds = EDate.getTime();
            Log.e("StartDate", startTimeInMilliseconds + " " + reminderDayStart);
            Log.e("EndDate", endTimeInMilliseconds + " " + reminderDayEnd);
        } catch (Exception e) {
            e.printStackTrace();
        }


        try {
            ContentResolver cr = context.getContentResolver();
            ContentValues values = new ContentValues();

            values.put(CalendarContract.Events.DTSTART, startTimeInMilliseconds);
            values.put(CalendarContract.Events.TITLE, title);
            values.put(CalendarContract.Events.DESCRIPTION, desc);

            values.put(CalendarContract.Events.CALENDAR_ID, 1);

            TimeZone timeZone = TimeZone.getDefault();
            values.put(CalendarContract.Events.EVENT_TIMEZONE, timeZone.getID());

            values.put(CalendarContract.Events.RRULE, "FREQ=HOURLY;COUNT=1");
            values.put(CalendarContract.Events.HAS_ALARM, 1);


            Uri eventUri;

            if (Build.VERSION.SDK_INT >= 8) {
                System.out.println("About to write event in later version...");
                eventUri = Uri.parse("content://com.android.calendar/events");
            } else {
                System.out.println("About to write event in older version...");
                eventUri = Uri.parse("content://calendar/events");
            }


            // insert event to calendar
            Uri uri = cr.insert(eventUri, values);
            Log.e("EventTest", uri.toString());
        } catch (Exception e) {
            e.printStackTrace();
        }



    }

}

我已经检查了Calendar_Ids并可以确认1存在。我还尝试了使用不同Calendar_Ids的代码,如果我检查的所有代码都存在的话。

我也在2种设备上尝试过此方法,但在两种设备上均不起作用。

您的帮助将不胜感激,因为我对此深感遗憾。

0 个答案:

没有答案