一、高级工具的开发分为:手机号码归属地查询、常用号码查询、短信备份、还原(XML编程)。
二、为EditText 添加文本改变事件得到的效果是文本变化时进行动态获取,类似于AJAX
et_phone_in_phone_address_query
.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start,
int before, int count) {
if (s.length() > 2) {
query();
}
}
@Override
public void beforeTextChanged(CharSequence s, int start,
int count, int after) {
// TODO Auto-generated method stub
}
@Override
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub
}
});
三、查询归属地的query方法
public void query() {
String phoneNo = et_phone_in_phone_address_query.getText().toString()
.trim();
if (phoneNo.length() == 0) {
Animation animation = AnimationUtils.loadAnimation(this,
R.anim.shake);
et_phone_in_phone_address_query.setAnimation(animation);
// 格式不正确震动输入框
animation.start();
vibrator.vibrate(new long[] { 200, 300, 400, 500, 600, 800 }, -1);
} else {
String address = DaoPhoneAddressQuery.phoneAddressQuery(phoneNo);
if (address != null) {
tv_result_in_phone_address_query.setText("号码归属地:" + address);
return;
}
}
tv_result_in_phone_address_query.setText("抱歉,暂时查不到该号码的归属地记录!");
}
四、查询手机归属地Dao实现类
public class DaoPhoneAddressQuery {
private static String DB_PATH = "/data/data/com.example.phoneguard/files/address.db";
/**
*
* @param phoneNo
* @param type
* @return
*/
public static String query(String phoneNo, int type) {
SQLiteDatabase database = SQLiteDatabase.openDatabase(DB_PATH, null,
SQLiteDatabase.OPEN_READONLY);
Cursor c = null;
if (type == 0) {// 表示查询手机号码
c = database
.rawQuery(
"select d2.location from data2 d2 where d2.id=(select d1.outkey from data1 d1 where d1.id=? );",
new String[] { phoneNo });
} else if (type == 1) {// type==1表示查询电话号码
c = database
.rawQuery(
"select d2.location from data2 d2 where d2.area=? group by d2.location",
new String[] { phoneNo });
}
while (c != null && c.moveToNext()) {
phoneNo = c.getString(0);
c.close();
database.close();
return phoneNo;
}
database.close();
return "";
}
public static String phoneAddressQuery(String phoneNo) {
String address = null;
if (!TextUtil.isBlank(phoneNo)) {
String regex = "^1[34568]\\d{5,9}$";
if (phoneNo.matches(regex)) {
address = DaoPhoneAddressQuery
.query(phoneNo.substring(0, 7), 0);
} else if (phoneNo.matches("^0\\d{2,10}$")) {
address = DaoPhoneAddressQuery
.query(phoneNo.substring(1, 3), 1);
if (TextUtil.isBlank(address) && phoneNo.length() >= 4) {
address = DaoPhoneAddressQuery.query(
phoneNo.substring(1, 4), 1);
}
address = address.replace("电信", "").replace("移动", "")
.replace("联通", "");
}
}
return address;
}
}
五、输入的手机号为空的时候振动器的使用
vibrator.vibrate(new long[] { 200, 300, 400, 500, 600, 800 }, -1);
在常用号码查询里面用到了ExpandableListView(可展开ListView)适配器代码如下,需要注意的就四个方法:1.分组的个数2.每个组内子节点的个数3.分组的视图4.子节点的视图
public class CommomNumberAdapter extends BaseExpandableListAdapter {
private Context context;
private LayoutInflater inflater;
private TextView tv_title_in_elv;
private TextView tv_name_in_elv;
private TextView tv_phone_in_elv;
public CommomNumberAdapter(Context context) {
this.context = context;
inflater = LayoutInflater.from(context);
}
@Override
public int getGroupCount() {
return DaoCommonNumberQuery.queryGroupCount();
}
@Override
public int getChildrenCount(int groupPosition) {
return DaoCommonNumberQuery.queryChildCount(groupPosition + 1);
}
@Override
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
View v;
if (convertView != null && convertView instanceof View) {
v = convertView;
} else {
v = inflater.inflate(R.layout.item_group_elv, null);
}
tv_title_in_elv = (TextView) v.findViewById(R.id.tv_title_in_elv);
String groupName = DaoCommonNumberQuery
.queryGroupName(groupPosition + 1);
tv_title_in_elv.setText(groupName);
return v;
}
@Override
public View getChildView(int groupPosition, int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
View v;
if (convertView != null && convertView instanceof View) {
v = convertView;
} else {
v = inflater.inflate(R.layout.item_child_elv, null);
}
tv_name_in_elv = (TextView) v.findViewById(R.id.tv_name_in_elv);
tv_phone_in_elv = (TextView) v.findViewById(R.id.tv_phone_in_elv);
Map<String, String> info = DaoCommonNumberQuery.queryChildInfo(
childPosition + 1, (groupPosition + 1));
for (Entry<String, String> e : info.entrySet()) {
tv_name_in_elv.setText(e.getValue());
tv_phone_in_elv.setText(e.getKey());
}
return v;
}
@Override
public Object getGroup(int groupPosition) {
// TODO Auto-generated method stub
return null;
}
@Override
public Object getChild(int groupPosition, int childPosition) {
// TODO Auto-generated method stub
return null;
}
@Override
public long getGroupId(int groupPosition) {
// TODO Auto-generated method stub
return 0;
}
@Override
public long getChildId(int groupPosition, int childPosition) {
// TODO Auto-generated method stub
return 0;
}
@Override
public boolean hasStableIds() {
// TODO Auto-generated method stub
return false;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
// TODO Auto-generated method stub
return false;
}
}
六、短信的备份和还原,我在XML编程一文中进行了阐述,这里需要注意的是,备份和还原时需要传入一个借口的实现类,这样的做的方便是在单个条目备份完毕时调用使用者自定义的方法进行更新视图。