Android APP BlackContact 信息转存


一. 首先建立两个资料表:


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




    
    

    
    

    
    

    
    



这边有两个 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 searchArrayList;
    private LayoutInflater mInflater;

    public CallAdapter(Context context, ArrayList 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 results) {
        searchArrayList = results;
        //Triggers the list update
        notifyDataSetChanged();
    }
}


ListView 中的每一个子项 caller.xml




    

    

    




六. 在 BlackInfoDBHelper.java 中定义取得 Calls 物件集合的方法 - 

public ArrayList select_calls() throws SQLException {

        //Cursor c=null;

        ArrayList objects = new ArrayList();
        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;


2. 在 MainActivity 定义 static 函数

// share dbhelper to multiple activities
    public static BlackInfoDBHelper getAdapter(){
        return dbhelper;
    }

3. 那么在 ManageBlockInfoActivity 中就可以这样用

BlackInfoDBHelper dbhelper = MainActivity.getAdapter();

八. 在 ManageBlockInfoActivity 完成来电记录的显示.


Android APP BlackContact 信息转存_第1张图片


你可能感兴趣的:(Android APP BlackContact 信息转存)