将问题添加到手机日历 - Android

时间:2017-05-18 21:10:17

标签: java android calendar samsung-mobile

使用Android手机日历时遇到问题,特别是三星日历S Planner。

我想在用户点击按钮时向手机日历添加事件;首先,他必须从datePicker中选择日期,然后从timePicker中选择时间,最后他必须单击我之前提到的按钮。

这不是问题,因为它在我尝试过的所有设备中都能正常工作。问题是当我想将事件添加到三星默认日历(S Planner)时;当用户单击“保存”按钮时,应用程序将停止工作。

您可以在下面找到我的代码:

dia = datePicker.getDayOfMonth();
mes = datePicker.getMonth();
año = datePicker.getYear();
hora = timePicker.getHour();
minutos = timePicker.getMinute();

Calendar calendar = Calendar.getInstance();
calendar.set(año,mes,dia,hora,minutos);

 if (ActivityCompat.checkSelfPermission(getApplicationContext(), android.Manifest.permission.WRITE_CALENDAR) != PackageManager.PERMISSION_GRANTED) {

                        ActivityCompat.requestPermissions(activity_programas.this,
                                new String[]{Manifest.permission.WRITE_CALENDAR},
                                PERMISOCALENDARIO);
                    }

                    else if (permiso){

                        cr = getApplicationContext().getContentResolver();
                        cv = new ContentValues();
                        cv.put(CalendarContract.Events.TITLE, eventoCalendario);
                        cv.put(CalendarContract.Events.DTSTART, calendar.getTimeInMillis());
                        cv.put(CalendarContract.Events.DTEND, calendar.getTimeInMillis() + 60 * 60 * 1000);
                        cv.put(CalendarContract.Events.CALENDAR_ID, 1);
                        cv.put(CalendarContract.Events.EVENT_TIMEZONE, Calendar.getInstance().getTimeZone().getID());

                        Uri uri = cr.insert(CalendarContract.Events.CONTENT_URI, cv);

                        long eventID = Long.parseLong(uri.getLastPathSegment());
                        ContentValues reminders = new ContentValues();
                        reminders.put(CalendarContract.Reminders.EVENT_ID, eventID);
                        reminders.put(CalendarContract.Reminders.METHOD, CalendarContract.Reminders.METHOD_ALERT);
                        reminders.put(CalendarContract.Reminders.MINUTES, 10);

                        Uri uri2 = cr.insert(CalendarContract.Reminders.CONTENT_URI, reminders);

                        Toast.makeText(activity_programas.this, R.string.agendado, Toast.LENGTH_SHORT).show();



                    }else{
                        Toast.makeText(activity_programas.this, "Permiso no concedido", Toast.LENGTH_SHORT).show();
                        return;
                    }

此代码在其他设备中运行良好,问题在于S planner。

如果我更改下面的代码,它也可以完美地工作,即使在三星手机中也是如此(在下面的代码中我不从datePicker / timePicker中选择时间,我只使用当前时间)。 / p>

 if (ActivityCompat.checkSelfPermission(getApplicationContext(), android.Manifest.permission.WRITE_CALENDAR) != PackageManager.PERMISSION_GRANTED) {

                        ActivityCompat.requestPermissions(activity_programas.this,
                                new String[]{Manifest.permission.WRITE_CALENDAR},
                                PERMISOCALENDARIO);
                    }

                    else if (permiso){

                        cr = getApplicationContext().getContentResolver();
                        cv = new ContentValues();
                        cv.put(CalendarContract.Events.TITLE, eventoCalendario);
                        cv.put(CalendarContract.Events.DTSTART, Calendar.getInstance().getTimeInMillis);
                        cv.put(CalendarContract.Events.DTEND, Calendar.getInstance().getTimeInMillis + 60 * 60 * 1000);
                        cv.put(CalendarContract.Events.CALENDAR_ID, 1);
                        cv.put(CalendarContract.Events.EVENT_TIMEZONE, Calendar.getInstance().getTimeZone().getID());

                        Uri uri = cr.insert(CalendarContract.Events.CONTENT_URI, cv);

                        long eventID = Long.parseLong(uri.getLastPathSegment());
                        ContentValues reminders = new ContentValues();
                        reminders.put(CalendarContract.Reminders.EVENT_ID, eventID);
                        reminders.put(CalendarContract.Reminders.METHOD, CalendarContract.Reminders.METHOD_ALERT);
                        reminders.put(CalendarContract.Reminders.MINUTES, 10);

                        Uri uri2 = cr.insert(CalendarContract.Reminders.CONTENT_URI, reminders);

                        Toast.makeText(activity_programas.this, R.string.agendado, Toast.LENGTH_SHORT).show();



                    }else{
                        Toast.makeText(activity_programas.this, "Permiso no concedido", Toast.LENGTH_SHORT).show();
                        return;
                    }

我希望我很清楚,提前抱歉我的英语,对于我的编码,我是Android的新编程。

非常感谢, 德语。

0 个答案:

没有答案