我已经看到有很多例子可以在Android中编写联系人而不是在delphi中编写联系人。 我尝试复制和翻译此代码,但没有成功。 你们有没有关于如何使用delphi在Android中输入几个数字和电子邮件的简单联系方式的任何建议? 那也很了解如何访问联系人及其照片。 谢谢。
答案 0 :(得分:0)
1.(Delphi 10)尝试 TAddressBook 。您可以在c:\ Program Files(x86)\ Embarcadero \ Studio \ 18.0 \ Samples \ Object Pascal \ Multi-Device Samples \ Device Sensors and Services \ Address Book \ Contacts \ Contacts_Delphi.dproj
中查看示例项目。在过程TForm1.FillContactList(来源:TAddressBookSource); 替换AddressBook1.AllContacts(来源,联系人);至 AddressBook1.AllContacts( nil ,联系人);获得所有联系人。
你可以在这里查看TAddressBook的来源:\ source \ fmx \ FMX.AddressBook.Android.pas
2.以下是有关如何为早期版本的Delphi插入,删除和更新联系人的示例。基于http://www.codeproject.com/Articles/578823/Android-Contact-Operations-Insert-Search-Delete。
procedure TContactsManager.InsertToContacts(const displayName, firstName, lastName, phone, eMail: string);
var
ops: JArrayList;
res: TJavaObjectArray<JContentProviderResult>;
rawContactInsertIndex: integer;
const
DATA_MIMETYPE = 'mimetype';
COMMON_DATA_TYPE = 'data2';
begin
ops := TJArrayList.JavaClass.init;
rawContactInsertIndex := ops.size;
ops.add
(
TJContentProviderOperation.JavaClass.newInsert
(
TJContactsContract_RawContacts.JavaClass.CONTENT_URI
).withValue
(
TJSyncStateContract_Columns.JavaClass.ACCOUNT_TYPE, nil
).withValue
(
TJSyncStateContract_Columns.JavaClass.ACCOUNT_NAME, nil
).build
);
// name
ops.add
(
TJContentProviderOperation.JavaClass.newInsert
(
TJContactsContract_Data.JavaClass.CONTENT_URI
).withValueBackReference
(
TJContacts_Entity.JavaClass.RAW_CONTACT_ID,
rawContactInsertIndex
).withValue
(
StringToJString(DATA_MIMETYPE),
TJCommonDataKinds_StructuredName.JavaClass.CONTENT_ITEM_TYPE
).withValue
(
TJCommonDataKinds_StructuredName.JavaClass.DISPLAY_NAME,
StringToJString(displayName)
).withValue
(
TJCommonDataKinds_StructuredName.JavaClass.FAMILY_NAME,
StringToJString(lastName)
).withValue
(
TJCommonDataKinds_StructuredName.JavaClass.GIVEN_NAME,
StringToJString(firstName)
).build
);
// phone
ops.add
(
TJContentProviderOperation.JavaClass.newInsert
(
TJContactsContract_Data.JavaClass.CONTENT_URI
).withValueBackReference
(
TJContacts_Entity.JavaClass.RAW_CONTACT_ID,
rawContactInsertIndex
).withValue
(
StringToJString(DATA_MIMETYPE),
TJCommonDataKinds_Phone.JavaClass.CONTENT_ITEM_TYPE
).withValue
(
TJCommonDataKinds_Phone.JavaClass.NUMBER,
StringToJString(phone)
).withValue
(
StringToJString(COMMON_DATA_TYPE),
StringToJString(IntToStr(TJCommonDataKinds_Phone.JavaClass.TYPE_MOBILE))
).build
);
// E-Mail
ops.add
(
TJContentProviderOperation.JavaClass.newInsert
(
TJContactsContract_Data.JavaClass.CONTENT_URI
).withValueBackReference
(
TJContacts_Entity.JavaClass.RAW_CONTACT_ID,
rawContactInsertIndex
).withValue
(
StringToJString(DATA_MIMETYPE),
TJCommonDataKinds_Email.JavaClass.CONTENT_ITEM_TYPE
).withValue
(
TJCommonDataKinds_Email.JavaClass.ADDRESS,
StringToJString(eMail)
).withValue
(
StringToJString(COMMON_DATA_TYPE),
StringToJString(IntToStr(TJCommonDataKinds_Email.JavaClass.TYPE_WORK))
).build
);
try
res := SharedActivityContext.getContentResolver.applyBatch(TJContactsContract.JavaClass.AUTHORITY, ops);
except
on E: Exception do
ShowMessage(E.Message);
end;
end;
procedure TContactsManager.DeleteContact(const aLookupID: string);
var
wDataUri: JNet_URI;
begin
wDataUri := TJNet_URI.JavaClass.withAppendedPath
(
TJContactsContract_Contacts.JavaClass.CONTENT_LOOKUP_URI,
StringToJString(aLookupID)
);
try
TAndroidHelper.Context.getContentResolver.delete(wDataUri, nil, nil);
except
on E: Exception do
ShowMessage('getContentResolver Delete error: ' + E.Message);
end;
end;
procedure TContactsManager.UpdateContact(const aLookupID, aPhoneNumber,
aDislpayName, aFirstName, aLastName, aEmail: string);
var
ops: JArrayList;
wJDislpayName,
wJFirstName,
wJLastName,
wJPhone,
wJEmail,
wJQueryStr: JString;
wJQueryParams: TJavaObjectArray<JString>;
wSubCursor: JCursor;
wfilter: TJavaObjectArray<JString>;
wDataUri: JNet_URI;
begin
if Trim(aLookupID) = '' then
begin
ShowMessage('Contact ID is null!!!');
Exit;
end;
wJQueryStr := StringToJString('mimetype = ? AND lookup = ?');
wDataUri := TJContactsContract_Data.JavaClass.CONTENT_URI;
// Names
wfilter := TJavaObjectArray<JString>.Create(3);
wfilter[0] := TJCommonDataKinds_StructuredName.JavaClass.DISPLAY_NAME;
wfilter[1] := TJCommonDataKinds_StructuredName.JavaClass.FAMILY_NAME;
wfilter[2] := TJCommonDataKinds_StructuredName.JavaClass.GIVEN_NAME;
wJQueryParams := TJavaObjectArray<JString>.Create(2);
wJQueryParams[0] := TJCommonDataKinds_StructuredName.JavaClass.CONTENT_ITEM_TYPE;
wJQueryParams[1] := StringToJString(aLookupID);
with SharedActivityContext.getContentResolver do
wSubCursor := Query(wDataUri, wfilter, wJQueryStr, wJQueryParams, nil);
try
if wSubCursor.getCount > 0 then
begin
// Getting only first row, as we retrieving only names, a contact can have only one first name, one familyname,
// for phone number for example, we need to loop on each row (while (wSubCursor.moveToNext) do) because a contact can have many phone number
wSubCursor.moveToNext;
wJDislpayName := wSubCursor.getString(0);
wJLastName := wSubCursor.getString(1);
wJFirstName := wSubCursor.getString(2);
end;
finally
wSubCursor.close;
wSubCursor := nil;
end;
if Trim(aDislpayName) <> '' then
wJDislpayName := StringToJString(Trim(aDislpayName));
if Trim(aFirstName) <> '' then
wJFirstName := StringToJString(Trim(aFirstName));
if Trim(aLastName) <> '' then
wJLastName := StringToJString(Trim(aLastName));
ops := TJArrayList.JavaClass.init;
wJQueryParams := TJavaObjectArray<JString>.Create(2);
wJQueryParams[0] := TJCommonDataKinds_StructuredName.JavaClass.CONTENT_ITEM_TYPE;
wJQueryParams[1] := StringToJString(aLookupID);
ops.add
(
TJContentProviderOperation.JavaClass.newUpdate
(
TJContactsContract_Data.JavaClass.CONTENT_URI
).withSelection
(
wJQueryStr, wJQueryParams
).withValue
(
TJCommonDataKinds_StructuredName.JavaClass.DISPLAY_NAME,
wJDislpayName
).withValue
(
TJCommonDataKinds_StructuredName.JavaClass.GIVEN_NAME,
wJFirstName
).withValue
(
TJCommonDataKinds_StructuredName.JavaClass.FAMILY_NAME,
wJLastName
).build
);
// Phone
wfilter := TJavaObjectArray<JString>.Create(1);
wfilter[0] := TJCommonDataKinds_Phone.JavaClass.NUMBER;
wJQueryParams := TJavaObjectArray<JString>.Create(2);
wJQueryParams[0] := TJCommonDataKinds_Phone.JavaClass.CONTENT_ITEM_TYPE;
wJQueryParams[1] := StringToJString(aLookupID);
with SharedActivityContext.getContentResolver do
wSubCursor := Query(wDataUri, wfilter, wJQueryStr, wJQueryParams, nil);
try
if wSubCursor.getCount > 0 then
begin
// Getting only first phone number , a contact can have multiple phone numbers, so if needed,
// we need to loop on each row (while (wSubCursor.moveToNext) do)
wSubCursor.moveToNext;
wJPhone := wSubCursor.getString(0);
end;
finally
wSubCursor.close;
wSubCursor := nil;
end;
if Trim(aPhoneNumber) <> '' then
begin
wJPhone := StringToJString(Trim(aPhoneNumber));
wJQueryParams := TJavaObjectArray<JString>.Create(2);
wJQueryParams[0] := TJCommonDataKinds_Phone.JavaClass.CONTENT_ITEM_TYPE;
wJQueryParams[1] := StringToJString(aLookupID);
ops.add
(
TJContentProviderOperation.JavaClass.newUpdate
(
TJContactsContract_Data.JavaClass.CONTENT_URI
).withSelection
(
wJQueryStr, wJQueryParams
).withValue
(
TJCommonDataKinds_Phone.JavaClass.NUMBER,
wJPhone
).build
);
end;
// E-Mail
wfilter := TJavaObjectArray<JString>.Create(1);
wfilter[0] := TJCommonDataKinds_Email.JavaClass.ADDRESS;
wJQueryParams := TJavaObjectArray<JString>.Create(2);
wJQueryParams[0] := TJCommonDataKinds_Email.JavaClass.CONTENT_ITEM_TYPE;
wJQueryParams[1] := StringToJString(aLookupID);
with SharedActivity.getContentResolver do
wSubCursor := Query(wDataUri, wfilter, wJQueryStr, wJQueryParams, nil);
Try
if wSubCursor.getCount > 0 then
begin
// Getting only first email , a contact can have many emails, so if needed,
// we need to loop on each row (while (wSubCursor.moveToNext) do)
wSubCursor.moveToNext;
wJEmail := wSubCursor.getString(0);
end;
Finally
wSubCursor.close;
wSubCursor := nil;
End;
if Trim(aEmail) <> '' then
begin
wJEmail := StringToJString(Trim(aEmail));
wJQueryParams := TJavaObjectArray<JString>.Create(2);
wJQueryParams[0] := TJCommonDataKinds_Email.JavaClass.CONTENT_ITEM_TYPE;
wJQueryParams[1] := StringToJString(aLookupID);
ops.add
(
TJContentProviderOperation.JavaClass.newUpdate
(
TJContactsContract_Data.JavaClass.CONTENT_URI
).withSelection
(
wJQueryStr, wJQueryParams
).withValue
(
TJCommonDataKinds_Email.JavaClass.ADDRESS,
wJEmail
).build
);
end;
if ops.size > 0 then
try
SharedActivityContext.getContentResolver.applyBatch(TJContactsContract.JavaClass.AUTHORITY, ops);
// TAndroidHelper.Context.getContentResolver.applyBatch(TJContactsContract.JavaClass.AUTHORITY, ops); // DELPHI DX10
except
on E: Exception do
ShowMessage('getContentResolver ApplyBatch for Update error: ' + E.Message);
end;
end;