一. 首先建立两个资料表:
BlackInfoDBHelper 加入插入一个记录的方法
public void insertCaller(String number) {
String cmd_exec = "insert into block_phone values (NULL, '" + number +
"', datetime('now'))";
Log.i(TAG, "SQL Command = " + cmd_exec);
try {
mDataBase.execSQL(cmd_exec);
} catch (Exception e) {
} finally {
}
}
在监听电话的 Receiver 中加入若为黑名单电话即转存资料
private class MyPhoneStateListener extends PhoneStateListener {
public void onCallStateChanged(int state, String incomingNumber) {
Log.i(TAG, state + " incoming number: " + incomingNumber);
/*
if("12345".equals(incomingNumber)) {
Log.i(TAG, "Block it!!!");
endcall();
}
*/
if(dbhelper.isBlackNumber(incomingNumber)) {
Log.i(TAG, "blocked " + incomingNumber);
if(state == 1)
dbhelper.insertCaller(incomingNumber);
endcall();
}
}
}
二. 定义第二个 Activity 用来显示信息 - ManageBlockInfoActivity
三. 定义 ManageBlockInfoActivity 画面的 layout - activity_manage_block_info.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context="com.elvis.android.blackcontacts.ManageBlockInfoActivity"
android:orientation="vertical">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/block_phone_view"/>
<View style="@style/view_divide_line_style" />
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/lv_block_phone">
</ListView>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/block_sms_view"/>
<View style="@style/view_divide_line_style" />
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/lv_block_message">
</ListView>
</LinearLayout>
这边有两个 ListView 一个用来显示来电记录, 一个用来显示短信记录.
四. 定义来电记录的集合物件 Calls.java,我们反复运用这个技巧
package com.elvis.android.blackcontacts;
/**
* Created by elvis on 11/2/15.
*/
public class Calls {
private String name = "";
private String phone = "";
private String time = "";
public void setName(String name) {
this.name = name;
}
public String getName() {
return this.name;
}
public void setPhone(String phone) {
this.phone = phone;
}
public String getPhone() {
return this.phone;
}
public void setTime(String time) {
this.time = time;
}
public String getTime() {
return this.time;
}
public Calls (String name, String ph, String time){
this.name = name;
this.phone = ph;
this.time = time;
}
public Calls (){
}
}
五. 定义 Adapater 用来连接显示来电记录的 ListView - CallAdapter.java
package com.elvis.android.blackcontacts;
/**
* Created by elvis on 11/2/15.
*/
import android.widget.BaseAdapter;
import java.util.ArrayList;
import android.view.LayoutInflater;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
public class CallAdapter extends BaseAdapter {
private static ArrayList<Calls> searchArrayList;
private LayoutInflater mInflater;
public CallAdapter(Context context, ArrayList<Calls> results) {
searchArrayList = results;
mInflater = LayoutInflater.from(context);
}
public int getCount() {
return searchArrayList.size();
}
public Object getItem(int position) {
return searchArrayList.get(position);
}
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View row;
row = mInflater.inflate(R.layout.caller, parent, false);
TextView tv_name, tv_phone, tv_time;
tv_name = (TextView) row.findViewById(R.id.tv_name);
tv_phone = (TextView) row.findViewById(R.id.tv_phone);
tv_time = (TextView) row.findViewById(R.id.tv_time);
tv_name.setText(searchArrayList.get(position).getName());
tv_phone.setText(searchArrayList.get(position).getPhone());
tv_time.setText(searchArrayList.get(position).getTime());
return (row);
}
public void updateResults(ArrayList<Calls> results) {
searchArrayList = results;
//Triggers the list update
notifyDataSetChanged();
}
}
ListView 中的每一个子项 caller.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal"
android:paddingBottom="1dip"
android:paddingLeft="10dip"
android:paddingTop="4dip" >
<TextView
android:id="@+id/tv_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#ff112b7d"
android:layout_marginLeft="5dp"
android:layout_marginTop="5dp"
android:textStyle="bold" />
<TextView
android:id="@+id/tv_phone"
android:layout_marginLeft="5dp"
android:layout_marginTop="5dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/tv_time"
android:layout_marginLeft="5dp"
android:layout_marginTop="5dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
六. 在 BlackInfoDBHelper.java 中定义取得 Calls 物件集合的方法 -
public ArrayList<Calls> select_calls() throws SQLException {
//Cursor c=null;
ArrayList<Calls> objects = new ArrayList<Calls>();
String cmd = "select * from block_phone";
Log.i(TAG, "SQL Command = " + cmd);
Cursor c=null;
try {
c = mDataBase.rawQuery(cmd, null);
//int id[]=new int[c.getCount()];
int i=0;
if (c.getCount() > 0)
{
c.moveToFirst();
do {
//id[i]=c.getInt(c.getColumnIndex("phone"));
//id_mapping[i] = c.getInt(0);
Log.i(TAG, "Name: " + c.getString(1));
//i++;
Calls call = new Calls("", c.getString(1), c.getString(2));
call.setName(queryName(call.getPhone()));
objects.add(call);
//cursor = c;
} while (c.moveToNext());
c.close();
}
} catch (Exception e) {
c.close();
} finally {
if(c!=null) {
c.close();
}
}
c.close();
return objects;
}
public String queryName(String number) {
String name = "";
String cmd = "select name from contact_info where phone1='" + number + "' or phone2='" +
number + "'";
Cursor c=null;
try {
c = mDataBase.rawQuery(cmd, null);
if (c.getCount() > 0)
{
c.moveToFirst();
do {
name = (c.getString(0));
//cursor = c;
} while (c.moveToNext());
c.close();
}
} catch (Exception e) {
c.close();
} finally {
if(c!=null) {
c.close();
}
}
c.close();
return name;
}
七. 在 ManageBlockInfoActivity 中获取 Calls 集合物件, 那么就要能在这个 Activity 中取得 dbhelper, 而这个方法如下描述
在多个 Activity 之间共享 dbhelper 的方法:
1. 在 MainActivity 宣告
private static BlackInfoDBHelper dbhelper = null;
// share dbhelper to multiple activities
public static BlackInfoDBHelper getAdapter(){
return dbhelper;
}
3. 那么在 ManageBlockInfoActivity 中就可以这样用
BlackInfoDBHelper dbhelper = MainActivity.getAdapter();
八. 在 ManageBlockInfoActivity 完成来电记录的显示.