如何解决Android Calender Provider-事件未创建/显示
我写了一个简单的应用程序,将日历事件插入到给定的android日历中。该代码不会消除任何错误,但在给定的压光机中也未显示通风孔。我想知道是否有人有什么想法?
我设置了以下权限:
<uses-permission android:name="android.permission.READ_CALENDAR" />
<uses-permission android:name="android.permission.WRITE_CALENDAR" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<permission android:name="android.permission.READ_CALENDAR" />
<permission android:name="android.permission.WRITE_CALENDAR" />
<permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
可以使用此功能找到压延机ID,该功能可以正常工作,该值被存储并稍后通过另一段代码调用:
protected void getCalenders()
{
Cursor cursor;
cursor = getContentResolver().query(Uri.parse("content://com.android.calendar/calendars"),new String[] { "_id","calendar_displayName" },null,null);
String calString = "";
// Get calendars name
calString = calString + "Calender count " + cursor.getCount();
cals = new ArrayList<String>();
if (cursor.getCount() > 0) {
cursor.movetoFirst();
String[] calendarNames = new String[cursor.getCount()];
// Get calendars id
int calendarIds[] = new int[cursor.getCount()];
for (int i = 0; i < cursor.getCount(); i++) {
cals.add(cursor.getInt(0) + ": " + cursor.getString(1));
cursor.movetoNext();
}
} else {
cals.add("0: No calenders found!");
}
// Create an ArrayAdapter using the string array and a default spinner layout
m_adapterForSpinnerWC = new ArrayAdapter<String>(getApplicationContext(),R.layout.spinner_item,cals);
// Specify the layout to use when the list of choices appears
m_adapterForSpinnerWC.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// Apply the adapter to the spinner
Spinner spn = findViewById(R.id.spCalerders);
spn.setAdapter(m_adapterForSpinnerWC);
}
事件插入如下:
public class updateCalender extends AsyncTask
{
Context curActivityContext = null;
public updateCalender(Context currentActivityContext){
curActivityContext = currentActivityContext;
}
public long pushEventToCalender(int calenderID,String title,String addInfo,String place,int status,long startDate,long endDate,int reminderTime,boolean needReminder,boolean needMailService) {
/***************** Event: note(without alert) *******************/
ContentResolver cr = getContentResolver();
Calendar beginTime = Calendar.getInstance();
ContentValues values = new ContentValues();
values.put(CalendarContract.Events.DTSTART,startDate);
values.put(CalendarContract.Events.DTEND,endDate);
values.put(CalendarContract.Events.TITLE,title);
values.put(CalendarContract.Events.DESCRIPTION,addInfo);
values.put(CalendarContract.Events.CALENDAR_ID,calenderID);
values.put(CalendarContract.Events.EVENT_TIMEZONE,TimeZone.getDefault().getID());
values.put(CalendarContract.Events.EVENT_LOCATION,place);
Uri uri = cr.insert(CalendarContract.Events.CONTENT_URI,values);
long eventID = Long.parseLong(uri.getLastPathSegment());
// Force a sync
Bundle extras = new Bundle();
extras.putBoolean(ContentResolver.SYNC_EXTRAS_EXPEDITED,true);
extras.putBoolean(ContentResolver.SYNC_EXTRAS_MANUAL,true);
AccountManager am = AccountManager.get(getApplicationContext());
Account[] acc = am.getAccountsByType("com.google");
Account account = null;
if (acc.length>0) {
account=acc[0];
ContentResolver.requestSync(account,"com.android.calendar",extras);
}
return eventID;
}
@Override
protected Object doInBackground(Object[] objects)
{
try {
int personnel_id = -100;
SharedPreferences settings = getSharedPreferences("UserInfo",0);
if (settings.contains("User_ID")) {
personnel_id = Integer.parseInt(settings.getString("User_ID","").toString());
}
if (personnel_id != -100 && Integer.parseInt(settings.getString("Calender ID","0")) > 0) {
SOAPClass soapc = new SOAPClass(getApplicationContext());
SimpleDateFormat format = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
ArrayList eventsList = soapc.getDairyEntries(personnel_id);
if (eventsList.size() > 2) {
long eventID = 0;
for (int i = 0; i < eventsList.size(); i=i+6)
{
Date sdate = format.parse(eventsList.get(i).toString().replace("anyType{}",""));
Date edate = format.parse(eventsList.get(i + 1).toString().replace("anyType{}",""));
Calendar beginTime = Calendar.getInstance();
beginTime.set(sdate.getYear(),sdate.getMonth(),sdate.getDay(),sdate.getHours(),sdate.getMinutes());
long smillies = beginTime.getTimeInMillis();
Calendar endTime = Calendar.getInstance();
endTime.set(edate.getYear(),edate.getMonth(),edate.getDay(),edate.getHours(),edate.getMinutes());
long emillies = endTime.getTimeInMillis();
eventID = pushEventToCalender(Integer.parseInt(settings.getString("Calender ID","0")),eventsList.get(i + 2).toString().replace("anyType{}",""),eventsList.get(i + 3).toString().replace("anyType{}",eventsList.get(i + 4).toString().replace("anyType{}",smillies,emillies,30,Boolean.FALSE,Boolean.FALSE);
// eventStartTimes[i] = eventsList.get(i).toString();
// eventEndTimes[i] = eventsList.get(i + 1).toString();
// eventLines[i] = eventsList.get(i + 2).toString();
// eventDescriptions[i] = eventsList.get(i + 3).toString();
// eventLocations[i] = eventsList.get(i + 4).toString();
// eventimage[i] = R.mipmap.ic_launcher;// "@mipmap/ic_launcher";
}
Uri shown = ContentUris.withAppendedId(CalendarContract.Events.CONTENT_URI,eventID);
Intent intent = new Intent(Intent.ACTION_VIEW).setData(shown);
startActivity(intent);
maketoastnUIThread( "Finished Importing Events To Calender " +settings.getString("Calender ID","0"));
}
else {
maketoastnUIThread( "No Events To Add.");
}
}else{
maketoastnUIThread( "No Calender Selected In Settings.");
}
}
catch (Exception e){
maketoastnUIThread( e.getMessage());
}
return null;
}
}
此外,当它尝试打开上一个插入的事件时,它表示未找到条目。有什么想法吗?
解决方法
在观看了varabe之后,我似乎找到了答案。我的日期设置不正确。
应该是这样的:
Calendar beginTime = Calendar.getInstance();
beginTime.set(sdate.getYear() + 1900,sdate.getMonth(),sdate.getDate(),sdate.getHours(),sdate.getMinutes());
long smillies = beginTime.getTimeInMillis();
Calendar endTime = Calendar.getInstance();
endTime.set(edate.getYear() + 1900,edate.getMonth(),edate.getDate(),edate.getHours(),edate.getMinutes());
long emillies = endTime.getTimeInMillis();
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。