The functions implemented:
- The binding of the SIM card
- Reading contacts
Technical points:
- SIM card Bindings
- Get a start-up broadcast
- Reading contacts
- Use of Simpleadapter
- Data transfer between activity
SIM card Bindings
Ideas:
- Create a Telephonymanager object
Telephonymanager manager = (Telephonymanager) context.getsystemservice (Context.telephony_service);
- Call Getsimserialnumber () to get the SIM card serial number
String number = Manager.getsimserialnumber ();
Then add number to Sharedpreferences, get permission: read_phone_state
Get a start-up broadcast
We have to make a decision every time the program is turned on, the SIM card stored in the Sharedpreferences data and the current SIM card data to compare, if different send alarm SMS
- Get a start-up broadcast
Create a class that inherits from Broadcastreceiver
Register this class with the list of listings and add the following code:
<intent-filter> <action android:name="android.intent.action.BOOT_COMPLETED"/> </intent-filter>
重写onReceive方法:把SharedPreferences中存储的sim卡数据和当前sim卡数据进行对比,如果不同则发送报警短信要获取权限:READ_BOOT_COMPLETE
Reading contacts
This feature requires the use of content provider technology
- Create a Contentresolver object
Contentresolver resolver = Context.getcontentresolver ();
- By querying the source code of the Android system, we learned that:
1) through Content://com.android.contacts/raw_contacts
You can get to contact_id that is, the ID value for each contact.
2) through Content://com.android.contacts/data
The database has contact_id mimetype data1 three columns
So, we can get the data from the following table by contact_id in the table above
Specific implementation code:
private static finalStringuri_id ="Content://com.android.contacts/raw_contacts"; private static finalStringUri_data="Content://com.android.contacts/data"; private static finalStringMimetype_name="Vnd.android.cursor.item/name"; private static finalStringMimetype_phone="Vnd.android.cursor.item/phone_v2"; list<map<String,String>> data =Newarraylist<map<String,String>> ();//Get a content parserContentresolver resolver = Context.getcontentresolver ();//query user's address book informationUri uriid = Uri.parse (uri_id); Uri uridata = Uri.parse (Uri_data); Cursor Cursorid = Resolver.query (Uriid,New String[]{"contact_id"},NULL,NULL,NULL); while(Cursorid.movetonext ()) {Stringid = cursorid.getstring (0);if(id! =NULL) {Cursor Cursordata = Resolver.query (Uridata,New String[]{"Data1","MimeType"},"contact_id=?",New String[]{id},NULL); map<String,String> map =Newhashmap<String,String> (); while(Cursordata.movetonext ()) {StringMimeType = cursordata.getstring (1);if(Mimetype_name.equals (MIMETYPE)) {Map.put ("Name", Cursordata.getstring (0)); }if(Mimetype_phone.equals (MIMETYPE)) {Map.put ("Phone", Cursordata.getstring (0)); }} data.add (map); Cursordata.close (); }} cursorid.close ();returnData }
获取权限:READ_CONTACT
Use of Simpleadapter
The difficulty lies in two:
1. Look at some messy data types
List<Map<String,*>>
Analysis:
1) Actually it's a list collection
2) Its elements are a bit special, it's a map collection
2. The pit father's constructor parameters
new SimpleAdapter(ContectActivity.this,data,R.layout.contect_item, new String[]{"name","phone"},new int[]{R.id.tv_contact_item_name,R.id.tv_contact_item_phone});
Well, it's been crying.
Analyze individually:
- First parameter: Context contexts
- Second parameter: The very messy data type passed in
- The third parameter: r.layout.*, the single item layout that you have customized is passed in
- Fourth parameter: Key in the map element of the second parameter
- The fifth parameter: The ID of the control specified in the layout of the third parameter, which corresponds to the fourth parameter, as shown in the code:
- Name-->r.id.tv_contact_item_name
- Phone-->r.id.tv_contact_item_phone
Transfer of data between activity:
(1) Jump from 1–>2, transfer data from 1–>2
In 1.class:
Intent Intent = new Intent (this,2.class)
Intent.putextras ("key", value);
In 2.class:
Intent Intent = Getintent ();
Intent.getserializableextra ("key"); You can get the data from 1.
(2) Jump from 2–>1, transfer data from 2–>1
-First Step-
In 1.class:
Intent Intent = new Intent (this,2.class);
Startactivityforresult (Intent, 0); Where the number 0 is the request code, in a moment will be used
-Second Step-
In 2.class:
Intent Intent = new Intent ();
Intent.putextra ("key", value);
Setresult (0,intent); Where the number 0 is the return code, in a moment will be used
-Step Three-
In 1.class:
Overriding the parent class method: Onactivityresult
/**requestCode 为请求码*resultCode 为返回码*data为2.class传回来的Intent*/protectedvoidonActivityResult(intint resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); ifnull) { value = data.getXXXExtra("key"); } }
Unfinished Technical points:
- The use of two methods of serializing objects
Come on, good night!
Copyright notice: Just out of the original content of the pot, I hope you have help ~
Mobile Security defender------mobile phone anti-theft page SIM card bindings & Read Contacts