Android phone preset contact/service number

Source: Internet
Author: User

Implement preset contacts (including names and numbers) to the mobile phone. Ensure that the contacts are read-only and cannot be deleted or edited.

The code is divided into two parts:
Part One inserts the preset contacts into the database;
In Part Two mode, you cannot delete or edit a preset contact on the contact details and contact multi-choice interface.
[Note] If you do not need to restrict the delete/edit operations of the preset contact, add only part of the Part One code and remove the statement contactvalues in step 3 "add function. put (rawcontacts. is_sdn_contact,-1 );
 

File: Alps \ packages \ apps \ contacts \ SRC \ com \ mediatek \ contacts \ simcontact \ abstractstartsimservice. Java
 
1. Import package
Import Android. provider. contactscontract. phonelookup;
 
2. Add variable
Private Static Boolean sisrunningnumbercheck = false;
Private Static final int insert_preset_number_count = xxx; // Number of Preset contacts
Private Static final string insert_preset_name [] = {"XXX1", "xxx2",...}; // name of each preset contact
Private Static final string insert_preset_number [] = {"XXX1", "xxx2",...}; // The number of each preset contact
 
3. Add a function (write the preset contact information to the database ):
Private void importdefaultreadonlycontact (){
New thread (New runnable (){
 
@ Override
Public void run (){
Log. I (TAG, "isrunningnumbercheck before:" + sisrunningnumbercheck );
If (sisrunningnumbercheck ){
Return;
}
Sisrunningnumbercheck = true;
For (INT I = 0; I <insert_preset_number_count; I ++)
{
Log. I (TAG, "isrunningnumbercheck after:" + sisrunningnumbercheck );
Uri uri = URI. withappendedpath (phonelookup. content_filter_uri, Uri
. Encode (insert_preset_number [I]);
Log. I (TAG, "getcontactinfobyphonenumbers (), uri =" + URI );
 
Cursor contactcursor = getcontentresolver (). Query (Uri, new string [] {
Phonelookup. display_name, phonelookup. photo_id
}, Null );
Try {
If (contactcursor! = NULL & contactcursor. getcount ()> 0 ){
Return;
} Else {
Final arraylist <contentprovideroperation> operationlist = new arraylist <contentprovideroperation> ();
Contentprovideroperation. Builder = contentprovideroperation
. Newinsert (rawcontacts. content_uri );
Contentvalues contactvalues = new contentvalues ();
Contactvalues. Put (rawcontacts. account_name,
Accounttype. account_name_local_phone );
Contactvalues. Put (rawcontacts. account_type,
Accounttype. account_type_local_phone );
Contactvalues. Put (rawcontacts. indicate_phone_sim,
Contactscontract. rawcontacts. indicate_phone );
Contactvalues. Put (rawcontacts. is_sdn_contact,-1 );
Builder. withvalues (contactvalues );
Builder. withvalue (rawcontacts. aggregation_mode,
Rawcontacts. aggregation_mode_disabled );
Operationlist. Add (builder. Build ());
 
Builder = contentprovideroperation. newinsert (data. content_uri );
Builder. withvaluebackreference (phone. raw_contact_id, 0 );
Builder. withvalue (data. mimetype, phone. content_item_type );
Builder. withvalue (phone. type, phone. type_mobile );
Builder. withvalue (phone. Number, insert_preset_number [I]);
Builder. withvalue (data. is_primary, 1 );
Operationlist. Add (builder. Build ());
 
Builder = contentprovideroperation. newinsert (data. content_uri );
Builder. withvaluebackreference (structuredname. raw_contact_id, 0 );
Builder. withvalue (data. mimetype, structuredname. content_item_type );
Builder. withvalue (structuredname. display_name, insert_preset_name [I]);
Operationlist. Add (builder. Build ());
 
Try {
Getcontentresolver (). applybatch (
Contactscontract. Authority, operationlist );
} Catch (RemoteException e ){
Log. E (TAG, String. Format ("% s: % s", E. tostring (), E. getmessage ()));
} Catch (operationapplicationexception e ){
Log. E (TAG, String. Format ("% s: % s", E. tostring (), E. getmessage ()));
}
 
}
} Finally {
// When this service start, but the contactsprovider has not been started yet.
// The contactcursor perhaps null, but not always. (First load will weekup the provider)
// So add null block to avoid nullpointerexception
If (contactcursor! = NULL ){
Contactcursor. Close ();
}
}
} //
Log. I (TAG, "isrunningnumbercheck insert:" + sisrunningnumbercheck );
Sisrunningnumbercheck = false;
}
}). Start ();
 
}

4. onstart calls this function:
 
Public void onstart (intent, int startid ){
.....
// Add By MTK --- preset contacts
Importdefaultreadonlycontact ();

Log ("[onstart]" + intent + ", startid" + startid );
If (intent = NULL ){
Return;
}
.....
}
 
 
Part Two
1. File: defacontcontactlistadapter. Java path: Alps \ packages \ apps \ contacts \ SRC \ com \ Android \ contacts \ list
The configureselection function contains five rawcontacts. is_sdn_contact + "= 0", all of which are changed to rawcontacts. is_sdn_contact + "<1"
 
2. File: profileandcontactsloader. Java path: Alps \ packages \ apps \ contacts \ SRC \ com \ Android \ contacts \ list
The loadsdn function contains rawcontacts. is_sdn_contact + "= 0", both of which are changed to rawcontacts. is_sdn_contact + "<1"
 
3. File: contact. Java path: Alps \ packages \ apps \ contacts \ SRC \ com \ Android \ contacts \ Model
Add the following functions:
// Add By MTK --- preset contacts
Public Boolean isreadonlycontact (){
Return missdncontact =-1;
}
 
4. File: contactloaderfragment. Java path: Alps \ packages \ apps \ contacts \ SRC \ com \ Android \ contacts \ detail
Modify the iscontacteditable function:
Public Boolean iscontacteditable (){
Return mcontactdata! = NULL &&! Mcontactdata. isdirectoryentry ()
&&! Mcontactdata. issdncontacts ()&&! Mcontactdata. isreadonlycontact ();
}
 
5. File: contactentrylistadapter. Java path: Alps \ packages \ apps \ contacts \ SRC \ com \ Android \ contacts \ list
Add the following code at the end of the file:
Public Boolean showreadonlycontact = true;
Public void setshowreadonlycontact (Boolean candelete ){
Showreadonlycontact = candelete;
}
 
6. File: contactentrylistfragment. Java path: Alps \ packages \ apps \ contacts \ SRC \ com \ Android \ contacts \ list
Introduce the following package:
Import com. mediatek. Contacts. List. contactsmultideletionfragment;
 
In the oncreateloader function, add the following statement to the penultimate sentence madapter. configureloader (loader, directoryid:
Madapter. setshowreadonlycontact (this instanceof contactsmultideletionfragment )? False: True );
 
8 7. file: multicontactsbasepickeradapter. java path: Alps \ packages \ apps \ contacts \ SRC \ com \ mediatek \ contacts \ list the Final Statement loader in the configureselection function. setselection (selection. tostring (); previously added the statement:
If (! Showreadonlycontact ){
Selection. append ("and" + contacts. is_sdn_contact + "= 0 ");
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.