"Side do project side learn Android" mobile Security defender 11-Set wizard set security Number

Source: Internet
Author: User
Tags getcolor

This time the main implementation of the function of setting the security number, that is, when the mobile SIM card information is found to change, will automatically send a security number of an alarm message. This includes selecting the Contacts feature. The knowledge points involved include: the Intent,listview data adapter with the return value.

Select a contact feature
    • Interface
Use the ListView to show the contacts that are being read
/mobilesafe/res/layout/select_contact.xml<?xml version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns: Android= "http://schemas.android.com/apk/res/android"    android:layout_width= "Match_parent"    android: layout_height= "Match_parent"    android:background= "@color/background" android:orientation= "vertical"    >        <listview         android:id= "@+id/lv_select_contact"        android:layout_width= "Match_parent"        android: layout_height= "Match_parent"                ></ListView></LinearLayout>
Get phone contact Data contact Data encapsulate entity
    • /mobilesafe/src/com/liuhao/mobilesafe/domain/contactinfo.java
Package com.liuhao.mobilesafe.domain;/** * Contact Data *  * @author Liuhao *  */public class ContactInfo {    private St ring name;    Private String phone;    Public ContactInfo () {    } public    ContactInfo (string name, String phone) {        this.name = name;        This.phone = phone;    }    Public String GetName () {        return name;    }    public void SetName (String name) {        this.name = name;    }    Public String Getphone () {        return phone;    }    public void Setphone (String phone) {        this.phone = phone;    }}
Get Contact Data Business class
    • /mobilesafe/src/com/liuhao/mobilesafe/engine/contactinfoservice.java
Package Com.liuhao.mobilesafe.engine;import Java.util.arraylist;import Java.util.list;import Android.content.contentresolver;import Android.content.context;import Android.database.cursor;import Android.net.uri;import Com.liuhao.mobilesafe.domain.contactinfo;public class Contactinfoservice {private Context    Context    Public Contactinfoservice (Context context) {This.context = context;        } public list<contactinfo> Getcontactinfos () {Contentresolver resolver = Context.getcontentresolver (); 1. Get the ID of the contact//2. Get contact name based on Contact ID//3. Get the corresponding data (phone, email, etc.) according to the type of the contact ID data list<cont        actinfo> infos = new arraylist<contactinfo> ();                ContactInfo info;        Uri uri = uri.parse ("content://com.android.contacts/raw_contacts");                Uri Datauri = Uri.parse ("Content://com.android.contacts/data");        cursor cursor = resolver.query (URI, NULL, NULL, NULL, NULL);        while (Cursor.movetonext ()) {    info = new ContactInfo ();            String id = cursor.getstring (cursor.getcolumnindex ("_id"));            String name = cursor.getstring (Cursor.getcolumnindex ("display_name"));            if (name==null) continue;//System.out.println (name);                        Info.setname (name);            Cursor datacursor = resolver.query (Datauri, NULL, "Raw_contact_id=?", New String[]{id}, NULL);                while (Datacursor.movetonext ()) {String type = datacursor.getstring (Datacursor.getcolumnindex ("mimetype")); String phone = datacursor.                GetString (Datacursor.getcolumnindex ("data1"));                                if (Phone==null | | type==null) continue;                If the phone number is added if ("Vnd.android.cursor.item/phone_v2". Equals (Type)) {Info.setphone (phone);              }//System.out.println (phone);//System.out.println (type);// System.out.println ("-------");            } infos.add (info);            info = null;        Datacursor.close ();//System.out.println ("################### #3");        } cursor.close ();    return infos; }}
Data adaptation
Package Com.liuhao.mobilesafe.ui;import Java.util.list;import Com.liuhao.mobilesafe.r;import Com.liuhao.mobilesafe.domain.contactinfo;import Com.liuhao.mobilesafe.engine.contactinfoservice;import Android.app.activity;import Android.content.intent;import Android.os.bundle;import Android.view.View;import Android.view.viewgroup;import Android.widget.adapterview;import Android.widget.AdapterView.OnItemClickListener; Import Android.widget.baseadapter;import Android.widget.linearlayout;import Android.widget.listview;import    Android.widget.textview;public class Selectcontactactivity extends Activity {private ListView LV;    Private list<contactinfo> infos;                @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);                Setcontentview (r.layout.select_contact);        Contactinfoservice service = new Contactinfoservice (this); Infos = Service.getcontactinfos ();//Get to contact data LV = (ListView) This. Findviewbyid (R.id.lv_select_contact); Lv.setadapter (New Selectcontactadapter ());//applies the contact data to the ListView Lv.setonitemclicklistener (New Onitemclicklistener (                    {//Set the response event when clicking on each entry @Override public void Onitemclick (adapterview<?> parent, view view,                int position, long id) {String phone = infos.get (position). Getphone ();//Gets the number of the contact                Intent Intent = new Intent ();  Intent.putextra ("Phone", phone);//Set the number data to intent Setresult (0, intent);//Call the-set the result that Your activity would return to its caller.            Returns the intent result to the caller, finish ();            }                    });            } private class Selectcontactadapter extends baseadapter{@Override public int GetCount () {        return Infos.size ();        } @Override public Object getItem (int position) {return infos.get (position);     } @Override   public long getitemid (int position) {return position; } @Override public View getView (int position, view Convertview, ViewGroup parent) {ContactInfo I            NFO = Infos.get (position);            LinearLayout ll = new LinearLayout (selectcontactactivity.this);            Ll.setorientation (linearlayout.vertical);            TextView tv_name = new TextView (selectcontactactivity.this);            Tv_name.settext ("Contact:" + info.getname ());            Tv_name.settextcolor (Getresources (). GetColor (R.color.textcolor));            TextView Tv_phone = new TextView (selectcontactactivity.this);            Tv_phone.settext ("Tel:" + info.getphone ());            Tv_phone.settextcolor (Getresources (). GetColor (R.color.textcolor));            Ll.addview (Tv_name);            Ll.addview (Tv_phone);        return ll; }            }    }
Activity transmits data directly
Package Com.liuhao.mobilesafe.ui;import Com.liuhao.mobilesafe.r;import Android.app.activity;import Android.content.intent;import Android.os.bundle;import Android.view.view;import Android.view.View.OnClickListener ; Import Android.widget.button;import Android.widget.edittext;public class Setupwizard3activity extends Activity    Implements Onclicklistener {private Button bt_select_contact;    Private Button Bt_next;    Private Button Bt_prev;        Private EditText Et_number;        @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);                Setcontentview (R.LAYOUT.SETUP_WIZARD3);        Bt_select_contact = (Button) This.findviewbyid (r.id.bt_select_contact);        Bt_next = (Button) This.findviewbyid (R.id.bt_next);        Bt_prev = (Button) This.findviewbyid (r.id.bt_previous);                Et_number = (EditText) This.findviewbyid (R.id.et_setup3_phonenumber);        Bt_select_contact.setonclicklistener (this); bt_next.seTonclicklistener (this);    Bt_prev.setonclicklistener (this); } @Override public void OnClick (View v) {switch (V.getid ()) {r.id.bt_select_contact:i            Ntent Intent = new Intent (this, selectcontactactivity.class);            Activates an interface with a return value Startactivityforresult (intent, 0);        Break Case R.id.bt_next:finish ();//users will not see this interface when they click "Back" Intent intent4 = new Intent (This, setupwizard4acti            Vity.class);            StartActivity (INTENT4);            Sets the animation effect Overridependingtransition (r.anim.alpha_in, R.anim.alpha_out) when the activity is toggled;        Break Case R.id.bt_previous:finish ();//users will not see this interface when they click "Back" Intent Intent2 = new Intent (This, SetupWizard2            Activity.class);            StartActivity (Intent2);            Sets the animation effect Overridependingtransition (r.anim.alpha_in, R.anim.alpha_out) when the activity is toggled;                    Break }} @Override Protected void Onactivityresult (int requestcode, int resultcode, Intent data) {Super.onactivityresult (Requestcode, Resul                Tcode, data);            if (data! = NULL) {String phone = Data.getstringextra ("Phone");            Et_number.settext (phone);        Et_number.settextcolor (Getresources (). GetColor (R.color.textcolor)); }            }}

"Side do Project learning Android" Mobile Security defender 11-Setup Wizard settings security Number

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.