步骤1:
将以下包添加到您要将新联系人插入到联系人列表中的java类中。 您可以将以下包添加到您的活动的类中以轻松测试功能。 然后你可以将代码移动到任何java类。
1
2
3
4
5
6
7
8
9
10
|
import
android
.
content
.
ContentProviderOperation
;
import
android
.
content
.
ContentProviderResult
;
import
android
.
content
.
Context
;
import
android
.
content
.
OperationApplicationException
;
import
android
.
os
.
RemoteException
;
import
android
.
provider
.
ContactsContract
;
import
android
.
provider
.
ContactsContract
.
CommonDataKinds
.
Phone
;
import
android
.
provider
.
ContactsContract
.
CommonDataKinds
.
StructuredName
;
import
android
.
provider
.
ContactsContract
.
Data
;
import
android
.
provider
.
ContactsContract
.
RawContacts
;
|
第2步:
现在在您的java类中添加以下函数WritePhoneContact(),其中已经添加了包(在上面的步骤1中提到)。 一旦你将下面的函数添加到你的java类,你必须调用该函数来添加一个新的联系人到Android联系人列表。 要测试功能,您可以在Activity的类中添加以下函数(代码片段)。
注意:您可以在代码中找到详细的内联代码描述。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
|
public
void
WritePhoneContact
(
String
displayName
,
String
number
,
Context
cntx
/*App or Activity Ctx*/
)
{
Context
contetx
=
cntx
;
//Application's context or Activity's context
String
strDisplayName
=
displayName
;
// Name of the Person to add
String
strNumber
=
number
;
//number of the person to add with the Contact
ArrayList
<
ContentProviderOperation
>
cntProOper
=
new
ArrayList
<
ContentProviderOperation
>
(
)
;
int
contactIndex
=
cntProOper
.
size
(
)
;
//ContactSize
//Newly Inserted contact
// A raw contact will be inserted ContactsContract.RawContacts table in contacts database.
cntProOper
.
add
(
ContentProviderOperation
.
newInsert
(
RawContacts
.
CONTENT_URI
)
//Step1
.
withValue
(
RawContacts
.
ACCOUNT_TYPE
,
null
)
.
withValue
(
RawContacts
.
ACCOUNT_NAME
,
null
)
.
build
(
)
)
;
//Display name will be inserted in ContactsContract.Data table
cntProOper
.
add
(
ContentProviderOperation
.
newInsert
(
Data
.
CONTENT_URI
)
//Step2
.
withValueBackReference
(
Data
.
RAW_CONTACT_ID
,
contactIndex
)
.
withValue
(
Data
.
MIMETYPE
,
StructuredName
.
CONTENT_ITEM_TYPE
)
.
withValue
(
StructuredName
.
DISPLAY_NAME
,
strDisplayName
)
// Name of the contact
.
build
(
)
)
;
//Mobile number will be inserted in ContactsContract.Data table
cntProOper
.
add
(
ContentProviderOperation
.
newInsert
(
Data
.
CONTENT_URI
)
//Step 3
.
withValueBackReference
(
ContactsContract
.
Data
.
RAW_CONTACT_ID
,
contactIndex
)
.
withValue
(
Data
.
MIMETYPE
,
Phone
.
CONTENT_ITEM_TYPE
)
.
withValue
(
Phone
.
NUMBER
,
strNumber
)
// Number to be added
.
withValue
(
Phone
.
TYPE
,
Phone
.
TYPE_MOBILE
)
.
build
(
)
)
;
//Type like HOME, MOBILE etc
try
{
// We will do batch operation to insert all above data
//Contains the output of the app of a ContentProviderOperation.
//It is sure to have exactly one of uri or count set
ContentProviderResult
[
]
contentProresult
=
null
;
contentProresult
=
contetx
.
getContentResolver
(
)
.
applyBatch
(
ContactsContract
.
AUTHORITY
,
cntProOper
)
;
//apply above data insertion into contacts list
}
catch
(
RemoteException
exp
)
{
//logs;
}
catch
(
OperationApplicationException
exp
)
{
//logs
}
}
|
代码说明:
1
|
WritePhoneContact
(
String
displayName
,
String
number
,
Context
cntx
/*App or Activity Ctx*/
)
|
String displayName:联系人的姓名(例如John),需要在Android联系人列表中添加。
字符串number:需要使用联系人(本示例中为John)添加的人员的移动号码(示例:9999999999)。
上下文cntx:这不是你的Activity的上下文,或者你可以提供你的应用程序的上下文。 只是为了测试的目的,你可以从你的Activity的onCreate()函数调用上面的函数,就像下面的步骤3。
步骤3:
调用上面提到的函数WritePhoneContact()如下。
1
2
3
4
5
6
7
8
9
10
11
|
@
Override
protected
void
onCreate
(
Bundle
savedInstanceState
)
{
super
.
onCreate
(
savedInstanceState
)
;
setContentView
(
R
.
layout
.
activity_contacts_backup_restore
)
;
// Pass below variable <em>cntx</em> into the WritePhoneContact() function as third variable.
<
em
>
Context
cntx
<
/
em
>
=
getApplicationContext
(
)
;
// get application context
//Now call below function to do the real task for you.
WritePhoneContact
(
"John"
,
"9999999999"
,
cntx
)
;
}
|
注意:如果您要调用本机“联系人活动”以插入新联系人,则可以按照以下代码。
1
2
3
4
|
//Add below packages in your java class
import
android
.
net
.
Uri
;
import
android
.
content
.
Intent
;
import
android
.
provider
.
ContactsContract
;
|
1
2
3
4
5
6
7
|
//Then add below code for inserting a new contact using intent
//create a new intent for inserting contact
Intent
contactIntent
=
new
Intent
(
ContactsContract
.
Intents
.
SHOW_OR_CREATE_CONTACT
,
ContactsContract
.
Contacts
.
CONTENT_URI
)
;
contactIntent
.
setData
(
Uri
.
parse
(
"tel:+919999999999"
)
)
;
//Add the mobile number here
contactIntent
.
putExtra
(
ContactsContract
.
Intents
.
Insert
.
PHONE
,
"John"
)
;
//ADD contact name here
//Below Start activity function will display the Add contacts native screen along with your input data
startActivity
(
contactIntent
)
;
|