【プロジェクトをしながらAndroidを学ぶ】携帯電話セキュリティ11-設定ガイドの設定セキュリティ番号
9978 ワード
今回は主に、携帯電話のSIMカードの情報が変わったことを発見した場合、自動的にセキュリティ番号にアラームメールを送信するセキュリティ番号設定機能を実現しました.主に連絡先の選択機能が含まれます.関連する知識点は、戻り値のあるIntent、ListViewデータアダプタです.
連絡先機能の選択インターフェース 読み出した連絡先をListViewで表示する
携帯電話の連絡先データの取得
連絡先データカプセル化エンティティ/mobilesafe/src/com/liuhao/mobilesafe/domain/ContactInfo.java
連絡先機能の選択
/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>
携帯電話の連絡先データの取得
連絡先データカプセル化エンティティ
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));
}
}
}