Android 日历内容提供程序查询不断崩溃
我有一个 PhoneGap 应用程序,我想通过内容提供程序访问 Android 日历。我的 PhoneGap JavaScript 函数可以成功调用并返回 Java 函数,但我只是无法让 Content Provider 调用正常工作。该应用程序编译没有问题并运行,但是一旦我调用相关的内容提供程序函数,它就会挂起一段时间并崩溃。下面是我发现的内容提供程序函数的修改版本: 包 com.george.myapp;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.TimeZone;
import android.app.Activity;
import android.content.ContentValues;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class EventsInCalender extends Activity {
//@Override
public void onCreate() {
//public void onCreate(Bundle savedInstanceState) {
//super.onCreate(savedInstanceState);
//setContentView(R.layout.event);
}
public String getCal()
{
String cal_name = "createdstring";
Cursor cursor = getContentResolver().query(Uri.parse("content://com.android.calendar/calendars"),new String[] { "_id", "displayName" }, "selected=1", null, null);
if (cursor != null && cursor.moveToFirst()) {
String[] calNames = new String[cursor.getCount()];
int[] calIds = new int[cursor.getCount()];
for (int i = 0; i < calNames.length; i++) {
// retrieve the calendar names and ids
calIds[i] = cursor.getInt(0);
calNames[i] = cursor.getString(1);
cursor.moveToNext();
}
cal_name = calNames[0];
cursor.close();
}
return cal_name;
}
}
I have a PhoneGap Application and I want to access the Android Calendar via the Content Provider. My PhoneGap JavaScript function can successfully call and return Java functions however I just haven't been able to get Content Provider calls to work. The app compiles without issues and run, but as soon as I call the related Content Provider function it hangs for a while and crashes. Below is a modified version of a Content Provider function I found:
package com.george.myapp;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.TimeZone;
import android.app.Activity;
import android.content.ContentValues;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class EventsInCalender extends Activity {
//@Override
public void onCreate() {
//public void onCreate(Bundle savedInstanceState) {
//super.onCreate(savedInstanceState);
//setContentView(R.layout.event);
}
public String getCal()
{
String cal_name = "createdstring";
Cursor cursor = getContentResolver().query(Uri.parse("content://com.android.calendar/calendars"),new String[] { "_id", "displayName" }, "selected=1", null, null);
if (cursor != null && cursor.moveToFirst()) {
String[] calNames = new String[cursor.getCount()];
int[] calIds = new int[cursor.getCount()];
for (int i = 0; i < calNames.length; i++) {
// retrieve the calendar names and ids
calIds[i] = cursor.getInt(0);
calNames[i] = cursor.getString(1);
cursor.moveToNext();
}
cal_name = calNames[0];
cursor.close();
}
return cal_name;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我会按照这个官方文档来处理Android中的日历内容提供程序: http:// /developer.android.com/guide/topics/providers/calendar-provider.html
I would follow this official documentation to handle Calendar Content Provider in Android: http://developer.android.com/guide/topics/providers/calendar-provider.html
看起来 Context 是问题所在,需要来自主 Activity,认为 Android 只向该 Activity 授予权限
Looks like Context was the problem, needed to come from the main activity, think Android only grants the permissions to that activity
您必须将 READ_CALENDAR 权限添加到 AndroidManifest.xml 文件中。
You must add permission READ_CALENDAR to AndroidManifest.xml file.