Mobile Security Guard ------ sim card binding on the mobile phone anti-theft page & amp; read contacts, ------ sim

Source: Internet
Author: User

Mobile Security Guard ------ sim card binding & reading contacts on the mobile phone anti-theft page, ------ sim

Implemented functions:

  • SIM card binding
  • Read contacts

Technical points:

  • SIM card binding
  • Get boot Broadcast
  • Read contacts
  • Use of SimpleAdapter
  • Data transfer between activities

SIM card binding

Ideas:

  • Create a TelephonyManager object

TelephonyManager manager = (TelephonyManager) context. getSystemService (Context. TELEPHONY_SERVICE );

  • Call getSimSerialNumber () to obtain the serial number of the SIM card.
    String number = manager. getSimSerialNumber ();

Then add the number to SharedPreferences to obtain the permission READ_PHONE_STATE.

Get boot Broadcast

We need to make a decision every time the program starts up, compare the SIM card data stored in SharedPreferences with the current SIM card data, if different, send an alert message

  • Get boot Broadcast
    Creates a class that inherits from BroadCastReceiver.
    Register this class in the list and add the following code:


<intent-filter>
      <action android: name = "android.intent.action.BOOT_COMPLETED" />
    </ intent-filter>
Override onReceive method:
Compare the sim card data stored in SharedPreferences with the current sim card data, if different, send an alarm message
To get permission: READ_BOOT_COMPLETE
Read contacts

This feature requires content provider technology

Create a ContentResolver object
ContentResolver resolver = context.getContentResolver ();
By querying the source code of the Android system, we learned:
1) Via content: //com.android.contacts/raw_contacts
You can get contact_id, which is the ID value of each contact
2) Via content: //com.android.contacts/data
There are three columns of contact_id mimetype data1 in the database
So, we can get the data in the table below through the contact_id in the table above
Specific implementation code:
 private static final String URI_ID = "content: //com.android.contacts/raw_contacts";
    private static final String URI_DATA = "content: //com.android.contacts/data";
    private static final String MIMETYPE_NAME = "vnd.android.cursor.item / name";
    private static final String MIMETYPE_PHONE = "vnd.android.cursor.item / phone_v2";
    List <Map <String, String >> data = new ArrayList <Map <String, String >> ();
        // Get a content parser
        ContentResolver resolver = context.getContentResolver ();
        // Query the user's address book information
        Uri 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 ())
        {
            String id = 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 = new HashMap <String, String> ();
                while (cursorData.moveToNext ())
                {
                    String mimetype = 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 ();
        return data;
    }
 Get permission: READ_CONTACT
Use of SimpleAdapter
The difficulty lies in two:
1. Look at some messy data types

List <Map <String, * >>
analysis:
1) Actually it is a List collection
2) Its elements are a bit special, it is a Map collection

2. 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});
Alright, it's already crying ...
One by one analysis:

The first parameter: Context context
The second parameter: the incoming messy data type
The third parameter: R.layout. *, Incoming is your custom single item layout
The fourth parameter: Key in the Map element of the second parameter
Fifth parameter: The id of the control is specified in the layout of the third parameter. This corresponds to the fourth parameter, as shown in the code:
name —-> R.id.tv_contact_item_name
phone —-> R.id.tv_contact_item_phone
Data transfer between activities:

(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); // The number 0 is the request code, which will be used later

—The second step—
In 2.class:
Intent intent = new Intent ();
intent.putExtra ("key", value);
setResult (0, intent); // The number 0 is the return code, which will be used later

-third step-
In 1.class:
Override the parent class method: onActivityResult

/ *
* requestCode is the request code
* resultCode is the return code
* data is the Intent returned from 2.class
* /
protected void onActivityResult (int requestCode, int resultCode, Intent data)
    {
        super.onActivityResult (requestCode, resultCode, data);
        if (data! = null)
        {
            XXX value = data.getXXXExtra ("key");
        }
    }
Unfinished technical points:

Use of two methods for serializing objects
Come on, good night!

Copyright statement: The original content that just came out of the pan, I hope it is helpful to you ~

Related Article

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.