鸿 网 互 联 www.68idc.cn

【边做项目边学Android】手机安全卫士11

来源:互联网 作者:佚名 时间:2014-12-31 13:01
这次主要实现设置安全号码的功能,即当发现手机SIM卡信息发生改变时,会自动给安全号码发送一条报警短信。主要包括选择联系人功能。涉及到的知识点包括:带返回

这次主要实现设置安全号码的功能,即当发现手机SIM卡信息发生改变时,会自动给安全号码发送一条报警短信。主要包括选择联系人功能。涉及到的知识点包括:带返回值的Intent,ListView数据适配器。

选择联系人功能
  • 界面
  • 用ListView来对读取的联系人进行展示 /mobilesafe/res/layout/select_contact.xml <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns: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> 获取手机联系人数据 联系人数据封装实体
  • /mobilesafe/src/com/liuhao/mobilesafe/domain/ContactInfo.java
  • package com.liuhao.mobilesafe.domain; /** * 联系人数据 * * @author liuhao * */ public class ContactInfo { private String 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; } } 获取联系人数据业务类
  • /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.获取联系人的id // 2.根据联系人的id获取联系人姓名 // 3.根据联系人id 数据的type,,获取对应的数据(电话,email等) List<ContactInfo> 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("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; } } 数据适配 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();// 获取到联系人数据 lv = (ListView) this.findViewById(R.id.lv_select_contact); lv.setAdapter(new SelectContactAdapter());// 将联系人数据适配到ListView中 lv.setOnItemClickListener(new OnItemClickListener() {// 设置点击每个条目时的响应事件 @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { String phone = infos.get(position).getPhone();// 获取到该联系人的号码 Intent intent = new Intent(); intent.putExtra("phone", phone);// 将号码数据设置到Intent中 setResult(0, intent);// Call this to set the result that your activity will return to its caller. 将Intent结果返回给调用者 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 info = infos.get(position); LinearLayout ll = new LinearLayout(SelectContactActivity.this); ll.setOrientation(LinearLayout.VERTICAL); TextView tv_name = new TextView(SelectContactActivity.this); tv_name.setText("联系人:" + info.getName()); tv_name.setTextColor(getResources().getColor(R.color.textcolor)); TextView tv_phone = new TextView(SelectContactActivity.this); tv_phone.setText("电话:" + info.getPhone()); tv_phone.setTextColor(getResources().getColor(R.color.textcolor)); ll.addView(tv_name); ll.addView(tv_phone); return ll; } } } Activity直接传递数据 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()){ case R.id.bt_select_contact: Intent intent = new Intent(this, SelectContactActivity.class); // 激活一个带返回值的界面 startActivityForResult(intent, 0); break; case R.id.bt_next: finish();// 用户点击“后退”时不会再看到这个界面 Intent intent4 = new Intent(this, SetupWizard4Activity.class); startActivity(intent4); // 设置Activity切换时的动画效果 overridePendingTransition(R.anim.alpha_in, R.anim.alpha_out); break; case R.id.bt_previous: finish();// 用户点击“后退”时不会再看到这个界面 Intent intent2 = new Intent(this, SetupWizard2Activity.class); startActivity(intent2); // 设置Activity切换时的动画效果 overridePendingTransition(R.anim.alpha_in, R.anim.alpha_out); break; } } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if(data != null){ String phone = data.getStringExtra("phone"); et_number.setText(phone); et_number.setTextColor(getResources().getColor(R.color.textcolor)); } } }

    网友评论
    <