我正在尝试从Android Webview HTML页面添加联系人。我使用Javascript接口从HTML / Javascript调用Android函数。下面是JSInterface.java中的代码,它将从Javascript中调用,
public void addContact(String name, String mobile){
String msg = "";
ContentValues cv = new ContentValues();
cv.put(Contacts.People.NAME, name);
Uri newPersonUri = Contacts.People.createPersonInMyContactsGroup(mContext.getContentResolver(), cv);
if (newPersonUri != null) {
ContentValues mobileValues = new ContentValues();
Uri mobileUri = Uri.withAppendedPath(newPersonUri,Contacts.People.Phones.CONTENT_DIRECTORY);
mobileValues.put(Contacts.Phones.NUMBER,mobile);
mobileValues.put(Contacts.Phones.TYPE,Contacts.Phones.TYPE_MOBILE);
Uri phoneUpdate = mContext.getContentResolver().insert(mobileUri, mobileValues);
if (phoneUpdate == null) {
msg = "Failed to add Contact";
}
}
msg = "Contact added successfully";
Toast.makeText(mContext, msg, Toast.LENGTH_SHORT).show();
}
当我从javascript调用该函数时,应用程序突然中止。以下异常跟踪打印在LogCat中,
12-08 12:59:59.610: I/dalvikvm(1259): java.lang.IllegalStateException: Failed to find the My Contacts group
12-08 12:59:59.610: I/dalvikvm(1259): at android.provider.Contacts$People.addToMyContactsGroup(Contacts.java:491)
12-08 12:59:59.610: I/dalvikvm(1259): at android.provider.Contacts$People.createPersonInMyContactsGroup(Contacts.java:568)
12-08 12:59:59.610: I/dalvikvm(1259): at com.android.contactmanager.JSInterface.addContact(JSInterface.java:89)
12-08 12:59:59.610: I/dalvikvm(1259): at android.webkit.WebViewCore.nativeTouchUp(Native Method)
12-08 12:59:59.620: I/dalvikvm(1259): at android.webkit.WebViewCore.nativeTouchUp(Native Method)
12-08 12:59:59.620: I/dalvikvm(1259): at android.webkit.WebViewCore.access$3900(WebViewCore.java:55)
12-08 12:59:59.620: I/dalvikvm(1259): at android.webkit.WebViewCore$EventHub$1.handleMessage(WebViewCore.java:1370)
12-08 12:59:59.620: I/dalvikvm(1259): at android.os.Handler.dispatchMessage(Handler.java:99)
我现在没有Android设备,所以我使用Android模拟器测试应用程序..在Android模拟器中“保存联系人”工作吗?如果是这样,我的代码有什么问题?任何帮助?
TIA。
答案 0 :(得分:1)
我找到了解决这个问题的方法。在Android模拟器中保存联系人工作得很好。我参考了以下博客将联系人保存到设备,
http://saigeethamn.blogspot.com/2011/05/contacts-api-20-and-above-android.html
为了所有人的利益在此发布:)
彼得