Android项目实战--手机卫士16--电话黑名单的增删改查



最新实战教程,让你了解Android自动化刷量、作弊与防作弊的那些事,案例:刷友盟统计、批量注册苹果帐号





好,今天我们来完成一下我们高级工具的黑名单功能,其实这个黑名单也很简单的,就是一个表记录用户标记的名单,当然有黑当然也有白啦,但是我们就不演示白名单的做法啦,都是和黑名单差不多的,你们有兴趣的可以自己完善一下。

好啦,让我们来看一下我们今天要做的效果,


我们做的是黑名单的增删改查,那么肯定就要建一个表啦

com.xiaobin.security.utils.DBHelper

package com.xiaobin.security.utils;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class DBHelper extends SQLiteOpenHelper
{

	public DBHelper(Context context)
	{
		super(context, "security.db", null, 1);
	}

	@Override
	public void onCreate(SQLiteDatabase db)
	{
		db.execSQL("create table blacknumber (_id integer primary key autoincrement, number varchar(20))");
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
	{
		//数据库版本发生不同的时候调用
	}

}

既然我们把数据库创建好啦,那么剩下来的就是增删改查啦

com.xiaobin.security.dao.BlackNumberDao

package com.xiaobin.security.dao;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.xiaobin.security.utils.DBHelper;

public class BlackNumberDao
{
	private DBHelper dbHelper;
	
	public BlackNumberDao(Context context)
	{
		dbHelper = new DBHelper(context);
	}
	
	public boolean find(String number)
	{
		boolean result = false;
		SQLiteDatabase db = dbHelper.getReadableDatabase();
		if(db.isOpen())
		{
			Cursor cursor = db.rawQuery("select number from blacknumber where number = ? ", new String[] {number});
			if(cursor.moveToNext())
			{
				result = true;
			}
			cursor.close();
			db.close();
		}
		return result;
	}
	
	public void add(String number)
	{
		find(number);
		SQLiteDatabase db = dbHelper.getWritableDatabase();
		if(db.isOpen())
		{
			db.execSQL("insert into blacknumber (number) values(?)", new Object[] {number});
			db.close();
		}
	}
	
	public void delete(String number)
	{
		SQLiteDatabase db = dbHelper.getWritableDatabase();
		if(db.isOpen())
		{
			db.execSQL("delete from blacknumber where number = ? ", new Object[] {number});
			db.close();
		}
	}
	
	public void update(String oldNumber, String newNumber)
	{
		SQLiteDatabase db = dbHelper.getWritableDatabase();
		if(db.isOpen())
		{
			db.execSQL("update blacknumber set number = ? where number = ? ", new Object[] {newNumber, oldNumber});
			db.close();
		}
	}
	
	public List<String> findAll()
	{
		List<String> numbers = new ArrayList<String>();
		SQLiteDatabase db = dbHelper.getReadableDatabase();
		if(db.isOpen())
		{
			Cursor cursor = db.rawQuery("select number from blacknumber", null);
			while(cursor.moveToNext())
			{
				numbers.add(cursor.getString(0));
			}
			cursor.close();
			db.close();
		}
		return numbers;
	}

}

都是数据库的基本操作,我就不写注释啦,现在方法就写好啦,那么,就要写逻辑啦

com.xiaobin.security.ui.NumberSecurityActivity

package com.xiaobin.security.ui;

import java.util.List;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.text.InputType;
import android.text.TextUtils;
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.view.Window;
import android.widget.AdapterView.AdapterContextMenuInfo;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.xiaobin.security.R;
import com.xiaobin.security.dao.BlackNumberDao;

public class NumberSecurityActivity extends Activity
{
	private ListView lv_number;
	private Button bt_number_add;
	private BlackNumberDao dao;
	private List<String> numbers;
	private NumberAdapter adapter;
	
	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.number_security);
		
		dao = new BlackNumberDao(this);
		numbers = dao.findAll();
		adapter = new NumberAdapter();
		lv_number = (ListView) findViewById(R.id.lv_number);
		lv_number.setAdapter(adapter);
		//给listview注册一个上下文菜单
		registerForContextMenu(lv_number);
		
		bt_number_add = (Button) findViewById(R.id.bt_number_add);
		bt_number_add.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				AlertDialog.Builder builder = new AlertDialog.Builder(NumberSecurityActivity.this);
				builder.setTitle("添加黑名单");
				final EditText et_number = new EditText(NumberSecurityActivity.this);
				et_number.setInputType(InputType.TYPE_CLASS_PHONE);
				et_number.setHint("请输入黑名单号码");
				builder.setView(et_number);
				builder.setPositiveButton("添加", new DialogInterface.OnClickListener()
				{
					@Override
					public void onClick(DialogInterface dialog, int which)
					{
						String number = et_number.getText().toString().trim();
						if(TextUtils.isEmpty(number))
						{
							Toast.makeText(NumberSecurityActivity.this, "添加号码不能为空", Toast.LENGTH_SHORT).show();
						}
						else
						{
							dao.add(number);
							numbers = dao.findAll();
							adapter.notifyDataSetChanged();
						}
					}
				});
				builder.setNegativeButton("取消", new DialogInterface.OnClickListener()
				{
					@Override
					public void onClick(DialogInterface dialog, int which)
					{
						
					}
				});
				builder.create().show();
			}
		});
	}
	
	@Override
	public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo)
	{
		super.onCreateContextMenu(menu, v, menuInfo);
		
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.number, menu);
	}
	
	@Override
	public boolean onContextItemSelected(MenuItem item)
	{
		//拿到点击的菜单的信息
		AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
		switch(item.getItemId())
		{
			case R.id.update_number : 
				String oldNumber = numbers.get((int) info.id);
				updateNumber(oldNumber);
				break;
				
			case R.id.delete_number : 
				int id = (int) info.id;
				String number = numbers.get(id);
				dao.delete(number);
				numbers = dao.findAll();
				adapter.notifyDataSetChanged();
				break;
				
			default : 
				break;
		}
		return super.onContextItemSelected(item);
	}
	
	private void updateNumber(final String oldNumber)
	{
		AlertDialog.Builder builder = new AlertDialog.Builder(NumberSecurityActivity.this);
		builder.setTitle("更新黑名单");
		final EditText et_number = new EditText(NumberSecurityActivity.this);
		et_number.setInputType(InputType.TYPE_CLASS_PHONE);
		et_number.setHint("请输入新的号码");
		builder.setView(et_number);
		builder.setPositiveButton("修改", new DialogInterface.OnClickListener()
		{
			@Override
			public void onClick(DialogInterface dialog, int which)
			{
				String number = et_number.getText().toString().trim();
				if(TextUtils.isEmpty(number))
				{
					Toast.makeText(NumberSecurityActivity.this, "添加号码不能为空", Toast.LENGTH_SHORT).show();
				}
				else
				{
					dao.update(oldNumber, number);
					numbers = dao.findAll();
					adapter.notifyDataSetChanged();
				}
			}
		});
		builder.setNegativeButton("取消", new DialogInterface.OnClickListener()
		{
			@Override
			public void onClick(DialogInterface dialog, int which)
			{
				
			}
		});
		builder.create().show();
	}
	
	//==================================================================
	
	private class NumberAdapter extends BaseAdapter
	{
		@Override
		public int getCount()
		{
			return numbers.size();
		}

		@Override
		public Object getItem(int position)
		{
			return numbers.get(position);
		}

		@Override
		public long getItemId(int position)
		{
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent)
		{
			if(convertView == null)
			{
				View view = View.inflate(NumberSecurityActivity.this, R.layout.number_security_item, null);
				TextView tv_item = (TextView) view.findViewById(R.id.tv_number_item);
				tv_item.setText(numbers.get(position));
				return view;
			}
			else
			{
				TextView tv_item = (TextView) convertView.findViewById(R.id.tv_number_item);
				tv_item.setText(numbers.get(position));
				return convertView;
			}
		}
	}

}

好啦,今天我们的黑名单的增删改查就到这里的啦,明天我们继续会完善这个黑名单的功能的

今天源码下载

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值