在网上感到一篇高人写的代码XE6下获取通讯录的,但是弄到XE7不能用了,原因是TJavaObjectArray变成泛型了,弄了半天没有搞懂TJavaObjectArray怎么使用,老是出错,不过研究了下Androidapi.JNI.Provider下的源代码,发现操作Android数据库的几个函数,坐下记录
下面是我修改的可以获取到数据库用户的代码:
uses FMX.Helpers.Android, Androidapi.JNI.JavaTypes, Androidapi.JNI.GraphicsContentViewText, FMX.Platform.Android, Androidapi.JNIBridge, Androidapi.JNI.Provider, Androidapi.Helpers; procedure QueryContact(AName: string; AList: TStrings); var cursorContactsPhone: JCursor; selection: string; projection:TJavaObjectArray<JString> ; FieldIndex: Integer; begin //if AList <> nil then //AList.Clear; //projection := nil; //if AName.Length > 0 then //begin ////projection := TJavaObjectArray.Create(1); ////ShowMessage(IntToStr(projection.Length)); //ShowMessage(JStringToString(TJContacts_PeopleColumns.JavaClass.DISPLAY_NAME)); //Exit ; // ////projection.Items[0] := TJContacts_PeopleColumns.JavaClass.DISPLAY_NAME; //Exit ; //selection := JStringToString(TJContacts_PeopleColumns.JavaClass.DISPLAY_NAME)+‘ LIKE "%‘ + AName + ‘%"‘; //end; // // Exit ; selection := JStringToString(TJContacts_PeopleColumns.JavaClass.DISPLAY_NAME)+‘ LIKE "%‘ + AName + ‘%"‘; //select projection from 联系人数据库 where selection cursorContactsPhone := SharedActivity.getContentResolver.query (TJCommonDataKinds_Phone.JavaClass.CONTENT_URI, nil{projection {要查询的字段名,nil的全部}, StringToJString(selection){Where条件}, nil { 这里是Where语句的条件参数们,我上面图方便,写死在Where条件中了,没使用参数 } , nil); if AList <> nil then while (cursorContactsPhone.moveToNext) do begin //获取字段的ColumnIndex FieldIndex := cursorContactsPhone.getColumnIndex (TJContacts_PeopleColumns.JavaClass.DISPLAY_NAME); //读字段内容 AList.Add(JStringToString(cursorContactsPhone.getString(FieldIndex))); end; cursorContactsPhone.close; end; procedure TForm1.btn1Click(Sender: TObject); var s: TStrings; begin s := TStringList.Create; QueryContact(‘王‘, s); //查询姓王的人 ShowMessage(s.Text); s.Free; end;
另外发现:
function update(uri: Jnet_Uri; values: JContentValues; where: JString; selectionArgs: TJavaObjectArray<JString>): Integer; cdecl; //修改数据库记录 function delete(url: Jnet_Uri; where: JString; selectionArgs: TJavaObjectArray<JString>): Integer; cdecl;//删除数据库记录
这样是不是就可以对联系人,短信,电话记录等随意修改那? 待验证.........
时间: 2024-10-07 11:28:55