|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
本帖最后由 青玄 于 2015-1-6 14:15 编辑
通讯卫士模块 本模块主要实现的是,对你要设置的电话号码进行短信拦截与电话拦截! 原理的赘述: 在我们对号码进行设置的时候,它其实是把号码保存到android的数据库中的! 当我们再次进入的时候它是从数据库里面取数据的; 在创建数据库的时候我们需要继承SQLiteOpenHelper这个类: package cn.cbd.mobilesafe.activity.db; import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper; public class BlackNumberOpenHelper extends SQLiteOpenHelper { public BlackNumberOpenHelper(Context context)
{
/**
* 1、context 上下文
* 2、"blackNumber.db" 需要创建的数据库
* 3、null -->CursorFactory
* 4、版本号
*/
super(context, "blackNumber.db", null, 1);
} @Override
public void onCreate(SQLiteDatabase db)
{
db.execSQL("create table blackNumber (id integer primary key autoincrement," +
"number varchar(20),mode varchar(20))");
} @Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
} }
数据库增删改查的实现类:package cn.cbd.mobilesafe.activity.db.dao;
import java.util.ArrayList;
import java.util.List;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import cn.cbd.mobilesafe.activity.bean.BlackNumber;
import cn.cbd.mobilesafe.activity.db.BlackNumberOpenHelper;
public class BlackNumberDao {
private BlackNumberOpenHelper blackNumberOpenHelper;
public BlackNumberDao(Context context)
{
this.blackNumberOpenHelper=new BlackNumberOpenHelper(context);
}
/**
* 往黑名单中添加数据
* @param number 电话号码
* @param mode all表示电话+短信,phone电话,sms表示短信
*/
public void save(String number,String mode)
{
SQLiteDatabase db=blackNumberOpenHelper.getWritableDatabase();
db.execSQL("insert into blackNumber (number,mode) values(?,?)",new Object[]{number,mode});
db.close();
}
/**
* 根据电话号码从黑名单中找到对应的记录
* @param number 手机号码
* @return 找到该记录,返回true,否则返回false
*/
public boolean find(String number)
{
boolean result=false;
SQLiteDatabase db=blackNumberOpenHelper.getReadableDatabase();
Cursor cursor=db.rawQuery("select * from blackNumber where number=?", new String[]{number});
if(cursor.moveToFirst())
{
result=true;
}
cursor.close();
db.close();
return result;
}
/**
* 查找一个某个号是否在数据库中
* @param number 电话号码
* @return -1 表示不在数据库中,0 表示电话拦截,1 短信拦截,2表示全部拦截
*/
public int findByMode(String number)
{
int code=-1;
SQLiteDatabase db=blackNumberOpenHelper.getReadableDatabase();
Cursor cursor=db.rawQuery("select mode from blackNumber where number=?", new String[]{number});
if(cursor.moveToFirst())
{
String mode=cursor.getString(0);
if("phone".equals(mode))
{
code=0;
}else if("sms".equals(mode)){
code=1;
}else if("all".equals(mode)){
code=2;
}
}
cursor.close();
db.close();
return code;
}
/**
* 根据电话号码更新黑名单中指定记录的mode
* @param number 电话号码
* @param mode all表示电话+短信,phone电话,sms表示短信
*/
public void update(String number,String mode)
{
SQLiteDatabase db=blackNumberOpenHelper.getWritableDatabase();
db.execSQL("update blackNumber set mode=? where number=?", new Object[]{mode,number});
db.close();
}
/**
* 根据电话号码删除黑名单中对应的记录
* @param number 电话号码
*/
public void delete(String number)
{
SQLiteDatabase db=blackNumberOpenHelper.getWritableDatabase();
db.execSQL("delete from blackNumber where number=?", new Object[]{number});
db.close();
}
/**
* 找出黑名单中所有的记录
* @return List集合
*/
public List<BlackNumber> findAll()
{
List<BlackNumber> blackNumberList=new ArrayList<BlackNumber>();
BlackNumber blackNumber=null;
SQLiteDatabase db=blackNumberOpenHelper.getWritableDatabase();
Cursor cursor=db.rawQuery("select * from blackNumber", null);
while(cursor.moveToNext())
{
blackNumber=new BlackNumber();
blackNumber.setId(cursor.getInt(cursor.getColumnIndex("id")));
blackNumber.setNumber(cursor.getString(cursor.getColumnIndex("number")));
blackNumber.setMode(cursor.getString(cursor.getColumnIndex("mode")));
blackNumberList.add(blackNumber);
}
db.close();
return blackNumberList;
}
/**
* 分页找出黑名单中所有的记录
* @return List集合
*/
public List<BlackNumber> findByPage(int page)
{
int pageIndex=page*30;
List<BlackNumber> blackNumberList=new ArrayList<BlackNumber>();
BlackNumber blackNumber=null;
SQLiteDatabase db=blackNumberOpenHelper.getWritableDatabase();
Cursor cursor=db.rawQuery("select * from blackNumber limit 30 offset ?", new String[]{pageIndex+""});
while(cursor.moveToNext())
{
blackNumber=new BlackNumber();
blackNumber.setId(cursor.getInt(cursor.getColumnIndex("id")));
blackNumber.setNumber(cursor.getString(cursor.getColumnI
ndex("number")));
blackNumber.setMode(cursor.getString(cursor.getColumnIndex("mode")));
blackNumberList.add(blackNumber);
}
db.close();
return blackNumberList;
}
}
然后写一个广播接受者进行拦截短信:
package cn.cbd.mobilesafe.activity.receiver;
import cn.cbd.mobilesafe.activity.db.dao.BlackNumberDao;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.telephony.SmsMessage;
public class BlackNumberSmsReceiver extends BroadcastReceiver {
private static final String TAG="BlackNumberSmsReceiver";
@Override
public void onReceive(Context context, Intent intent) {
BlackNumberDao blackNumberDao=new BlackNumberDao(context);
Object[] obj=(Object[]) intent.getExtras().get("pdus");
for(Object o : obj)
{
SmsMessage smsMessage=SmsMessage.createFromPdu((byte[])o);
//发信
的后机号,应该在此判断发信人的手机号是否与安全号码匹配
String number=smsMessage.getOriginatingAddress();
int code=blackNumberDao.findByMode(number);
if(code==1 || code==2)
{
abortBroadcast();
}
}
}
}
然后就是主程序模块的类:
package cn.cbd.mobilesafe.activity;
import java.util.List;
import cn.cbd.mobilesafe.activity.bean.BlackNumber;
import cn.cbd.mobilesafe.activity.db.dao.BlackNumberDao;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.os.AsyncTask;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.AdapterView.AdapterContextMenuInfo;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
public class BlackNumberActivity extends Activity implements OnClickListener {
private ListView lv_blacknumber;
private BlackNumberDao blackNumberDao;
private List<BlackNumber> blackNumberList;
private LinearLayout ll_loading;
private BlackNumberAdapter adapter;
private int pageNumber;
private boolean loading;
private AlertDialog dialog;
private EditText et_add;
private CheckBox cb_phone;
private CheckBox cb_sms;
private int position;
private static final String TAG="BlackNumberActivity";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add_blacknumber);
ll_loading = (LinearLayout) findViewById(R.id.ll_loading);
lv_blacknumber=(ListView) findViewById(R.id.lv_blacknumber);
//第一步: 给ListView注册一个上下文菜单
registerForContextMenu(lv_blacknumber);
//为listView添加一个滚动监听事件
lv_blacknumber.setOnScrollListener(new OnScrollListener()
{
@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
switch(scrollState)
{
//静止状态
case OnScrollListener.SCROLL_STATE_IDLE :
int position=lv_blacknumber.getLastVisiblePosition(); //从零开始计算
int size=blackNumberList.size();
if(loading)
{
//如果数据正在加载,则返回
Log.i(TAG,"数据正在加载,请稍后......");
return;
}
if((position+1)==size)
{
Log.i(TAG,"数据到达列表尾部,请加载数据.");
pageNumber=pageNumber+1;
fillData();
}
break;
//滑行状态
case OnScrollListener.SCROLL_STATE_FLING :
break;
//手指触模滑动
case OnScrollListener.SCROLL_STATE_TOUCH_SCROLL :
break;
}
}
@Override
public void onScroll(AbsListView view, int firstVisibleItem,
int visibleItemCount, int totalItemCount) {
// TODO Auto-generated method stub
}
});
blackNumberDao=new BlackNumberDao(this);
fillData();
}
private void fillData() {
new AsyncTask<Void, Void, Void>() {
@Override
protected Void doInBackground(Void... params) {
if(blackNumberList==null)
{
blackNumberList=blackNumberDao.findByPage(pageNumber);
}
else
{
List<BlackNumber> blackNumberList2=blackNumberDao.findByPage(pageNumber);
for(BlackNumber bn : blackNumberList2)
{
blackNumberList.add(bn);
}
}
return null;
}
@Override
protected void onPreExecute() {
loading=true;
ll_loading.setVisibility(View.VISIBLE);
super.onPreExecute();
}
@Override
protected void onPostExecute(Void result) {
ll_loading.setVisibility(View.INVISIBLE);
if(adapter==null)
{
adapter=new BlackNumberAdapter();
lv_blacknumber.setAdapter(adapter);
}
else
{
adapter.notifyDataSetChanged();//通知界面数据的适配器的数据更新了
}
loading=false;
super.onPostExecute(result);
}
}.execute();
}
private final class BlackNumberAdapter extends BaseAdapter
{
@Override
public int getCount() {
return blackNumberList.size();
}
@Override
public Object getItem(int position) {
return blackNumberList.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view=null;
ViewHolder holder;
if(convertView==null)
{
view=View.inflate(getApplicationContext(), R.layout.blacknumber_item, null);
holder=new ViewHolder();
holder.tv_number=(TextView) view.findViewById(R.id.blacknumber_item_number);
holder.tv_mode=(TextView) view.findViewById(R.id.blacknumber_item_mode);
view.setTag(holder);
}
else
{
view=convertView;
holder=(ViewHolder) view.getTag();
}
BlackNumber blackNumber=blackNumberList.get(position);
holder.tv_number.setText(blackNumber.getNumber());
String mode=blackNumber.getMode();
if("all".equals(mode))
{
holder.tv_mode.setText("电话+短信拦截");
}else if("phone".equals(mode)){
holder.tv_mode.setText("电话拦截");
}else if("sms".equals(mode)){
holder.tv_mode.setText("短信拦截");
}
return view;
}
}
static class ViewHolder
{
TextView tv_mode;
TextView tv_number;
}
public void addBlackNumber(View v)
{
showBlackNumberDialog(1,-1);
}
/**
* 根据type值判断是增加黑名单号码还是更新黑名单号码
* @param type 1 表示增加,2表示更新
* @param index 表示要修改的对象在集合中的位置,如果不需要请设置为-1
*/
private void showBlackNumberDialog(int type,int index) {
AlertDialog.Builder builder=new Builder(this);
dialog=builder.create();
View view=View.inflate(this,R.layout.layout_addblacknumber, null);
if(type==2)
{
TextView tv_title=(TextView) view.findViewById(R.id.tv_dialog_title);
tv_title.setText("修改黑名单号码的模式");
}
et_add=(EditText) view.findViewById(R.id.et_add_black_number);
if(index>=0) //表示更新
{
position=index;
BlackNumber blackNumber =(BlackNumber) lv_blacknumber.getItemAtPosition(index);
et_add.setText(blackNumber.getNumber());
et_add.setEnabled(false);
}
cb_phone= (CheckBox) view.findViewById(R.id.cb_block_phone);
cb_sms= (CheckBox) view.findViewById(R.id.cb_block_sms);
Button bt_add_ok=(Button) view.findViewById(R.id.bt_add_ok);
bt_add_ok.setTag(type);//往button中添加一个值,用于判断是添加还是更新
Button bt_add_cancle=(Button) view.findViewById(R.id.bt_add_cancle);
bt_add_ok.setOnClickListener(this);
bt_add_cancle.setOnClickListener(this);
dialog.setView(view, 0, 0, 0, 0);
dialog.show();
}
@Override
public void onClick(View v) {
switch(v.getId())
{
case R.id.bt_add_cancle :
dialog.dismiss();
break;
case R.id.bt_add_ok :
String number=et_add.getText().toString().trim();
if(TextUtils.isEmpty(number))
{
Toast.makeText(this, "电话号码不为能为空", Toast.LENGTH_SHORT).show();
return;
}
int type= (Integer) v.getTag();
if(type==1)
{
if(cb_phone.isChecked() && cb_sms.isChecked())
{
blackNumberDao.save(number, "all");
}else if(cb_phone.isChecked()){
blackNumberDao.save(number, "phone");
}else if(cb_sms.isChecked()){
blackNumberDao.save(number, "sms");
}else{
Toast.makeText(this, "拦截模式不能为空,请选择拦截模式", Toast.LENGTH_SHORT).show();
return;
}
}
else
{
BlackNumber blackNumber=blackNumberList.get(position);
if(cb_phone.isChecked() && cb_sms.isChecked())
{
blackNumberDao.update(number, "all");
blackNumber.setMode("all");
blackNumberList.set(position, blackNumber);
}else if(cb_phone.isChecked()){
blackNumberDao.update(number, "phone");
blackNumber.setMode("phone");
blackNumberList.set(position, blackNumber);
}else if(cb_sms.isChecked()){
blackNumberDao.update(number, "sms");
blackNumber.setMode("sms");
blackNumberList.set(position, blackNumber);
}else{
Toast.makeText(this, "拦截模式不能为空,请选择拦截模式", Toast.LENGTH_SHORT).show();
return;
}
adapter.notifyDataSetChanged();
}
dialog.dismiss();
break;
}
}
//第二步: 上下文菜单弹出时,调用该方法
@Override
public void onCreateContextMenu(ContextMenu menu, View v,ContextMenuInfo menuInfo) {
MenuInflater inflater=getMenuInflater();
inflater.inflate(R.menu.blacknumber_set_menu, menu);
super.onCreateContextMenu(menu, v, menuInfo);
}
//第三步: 上下文菜单被选择时执行该方法
@Override
public boolean onContextItemSelected(MenuItem item) {
AdapterContextMenuInfo info=(AdapterContextMenuInfo) item.getMenuInfo();
int position=(int) info.id;//此position为ListView中选择条目的position的位置
switch(item.getItemId())
{
case R.id.item_blacknumber_delete:
delete(position);
return true;
case R.id.item_blacknumber_update:
update(position);
return true;
default :
return true;
}
}
/**
* 更新ListView中的position位置的条目内容
* @param position 位置
*/
private void update(int position) {
showBlackNumberDialog(2,position);
}
/**
* 删除ListView中的position位置的条目内容
* @param position 位置
*/
private void delete(int position) {
//根据位置从ListView中获取该对象
BlackNumber blackNumber=(BlackNumber) lv_blacknumber.getItemAtPosition(position);
//从集合中删除该数据
blackNumberList.remove(position);
//从数据库中删除该记录
blackNumberDao.delete(blackNumber.getNumber());
//通知界面更新
adapter.notifyDataSetChanged();
}
}
效果:
|
|