我一直在想用什么能把安卓所有知识点,复习完,它,就是它,手机安全卫士,几乎包括所有知识点,我最近也想高仿一个款外国的工具箱玩玩,So,顺便写一写笔记吧。
最后的高仿的工具箱我也会开源在github.....不扯淡了~~~开搞。
我的思路永远是从布局开始,因为你要想一般用户会怎么使用,你或者你也可以参考别人已经做好的东西。
别怀疑 上面的标题,因为这是我的一个测试Demo。
主界面是一个标题栏 添加能输入号码 和选择模式 电话拦截和短信拦截 最近被别人鄙视,说我的代码风格差,所以这一篇文章会用到注解......
BlackNumSearchDao.java帮助类
package com.example.Darkbutton.BlackNumSerach; import android.content.Context; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteOpenHelper; public class BlackNumSearchDao extends SQLiteOpenHelper { public BlackNumSearchDao(Context context) { // 上下文 数据库名 版本 super(context, "blacknumber.db", null, 1); } // 初始化数据库的表结构 @Override public void onCreate(SQLiteDatabase db) { // 建立 表 名 blacknumber id 自增长 20个字节的号码 模式 2字节 db.execSQL("create table blacknumber(_id integer primary key autoincrement,number varchar(20),mode varchar(2))"); } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { } }
数据库类 主要存放一些数据库语句,比如查询,添加,删除,查询
package com.example.Darkbutton.BlackNumSerach; import java.util.ArrayList; import java.util.List; import android.content.ContentValues; import android.content.Context; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; public class BlackNumberDao { private BlackNumSearchDao helper; public BlackNumberDao(Context context) { helper=new BlackNumSearchDao(context); } /** * 查询黑名单号码是是否存在 * @param number * @return */ public boolean find(String number){ boolean result = false; SQLiteDatabase db = helper.getReadableDatabase(); // where 之后的语句是用来做筛选的条件 查询表明 blacknumber 的某一个号码 Cursor cursor=db.rawQuery("select * from blacknumber where number=?", new String []{number}); if (cursor.moveToNext()) { result=true; } cursor.close(); db.close(); return result; } /** * 查询黑名单号码的拦截模式 * @param number * @return 返回号码的拦截模式,不是黑名单号码返回null */ public String findMode(String number){ String result=null; SQLiteDatabase db = helper.getReadableDatabase(); //查询?号码的mode Cursor cursor = db.rawQuery("select mode from blacknumber where number=?", new String []{number}); if (cursor.moveToNext()) { result=cursor.getString(0); } cursor.close(); db.close(); return result; } /** * 查询全部黑名单号码 * @return */ public List<BlackNumberInfo> findAll(){ List<BlackNumberInfo> result = new ArrayList<BlackNumberInfo>(); SQLiteDatabase db = helper.getReadableDatabase(); Cursor cursor = db.rawQuery("select number,mode from blacknumber order by _id desc", null); while(cursor.moveToNext()){ BlackNumberInfo info = new BlackNumberInfo(); String number = cursor.getString(0); String mode = cursor.getString(1); info.setMode(mode); info.setNumber(number); result.add(info); } cursor.close(); db.close(); return result; } /** * 添加黑名单号码 * @param number 黑名单号码 * @param mode 拦截模式 1.电话拦截 2.短信拦截 3.全部拦截 */ public void add(String number,String mode){ SQLiteDatabase db = helper.getWritableDatabase(); ContentValues values=new ContentValues(); values.put("number", number); values.put("mode", mode); db.insert("blacknumber", null, values); db.close(); } /** * 修改黑名单号码的拦截模式 * @param number 要修改的黑名单号码 * @param newmode 新的拦截模式 */ public void update(String number,String newmode){ SQLiteDatabase db = helper.getWritableDatabase(); ContentValues values=new ContentValues(); values.put("mode", newmode); //表名为blacknumber db.update("blacknumber", values, "number=?", new String []{number}); db.close(); } /** * 删除黑名单号码 * @param number 要删除的黑名单号码 */ public void delete(String number){ SQLiteDatabase db = helper.getWritableDatabase(); db.delete("blacknumber", "number=?", new String[]{number}); db.close(); } }
package com.example.Darkbutton.BlackNumSerach; /** * 黑名单号码的业务bean * @author Administrator * */ public class BlackNumberInfo { private String number; private String mode; public String getNumber() { return number; } public void setNumber(String number) { this.number = number; } public String getMode() { return mode; } public void setMode(String mode) { this.mode = mode; } @Override public String toString() { return "BlackNumberInfo [number=" + number + ", mode=" + mode + "]"; } }
主类,我要对listview 进行初始化,还有button点击出现的AlertDialog 进行处理
package com.example.Darkbutton.BlackNumSerach; import java.util.List; import com.example.Darkbutton.R; import com.lidroid.xutils.ViewUtils; import com.lidroid.xutils.view.annotation.ViewInject; import android.app.Activity; import android.app.AlertDialog; import android.app.AlertDialog.Builder; import android.content.DialogInterface; import android.os.Bundle; import android.text.TextUtils; import android.view.View; import android.view.ViewGroup; import android.view.View.OnClickListener; import android.widget.BaseAdapter; import android.widget.Button; import android.widget.CheckBox; import android.widget.EditText; import android.widget.ImageView; import android.widget.ListView; import android.widget.TextView; import android.widget.Toast; public class BlackNumSerach extends Activity { private BlackNumberDao dao; private List<BlackNumberInfo> infos; private CallSmsSafeAdapter adapter; @ViewInject(R.id.textView1) TextView textView; @ViewInject(R.id.lv_callsms_safe) ListView listView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_call_sms_safe); ViewUtils.inject(BlackNumSerach.this); dao=new BlackNumberDao(this); infos = dao.findAll(); adapter = new CallSmsSafeAdapter(); listView.setAdapter(adapter); } private class CallSmsSafeAdapter extends BaseAdapter{ @Override public int getCount() { return infos.size(); } @Override public Object getItem(int position) { return null; } @Override public long getItemId(int position) { return 0; } @Override public View getView(final int position, View convertView, ViewGroup parent) { View view; ViewHolder holder; //1.减少内存中view对象创建的个数 if(convertView==null){ //把一个布局文件转化成 view对象。 view = View.inflate(getApplicationContext(), R.layout.list_item_callsms, null); //2.减少子孩子查询的次数 内存中对象的地址。 holder = new ViewHolder(); holder.tv_number = (TextView) view.findViewById(R.id.tv_black_number); holder.tv_mode = (TextView) view.findViewById(R.id.tv_block_mode); holder.iv_delete = (ImageView) view.findViewById(R.id.iv_delete); //当孩子生出来的时候找到他们的引用,存放在记事本,放在父亲的口袋 view.setTag(holder); }else{ // Log.i(TAG,"厨房有历史的view对象,复用历史缓存的view对象:"+position); view = convertView; holder = (ViewHolder) view.getTag();//5% } holder.tv_number.setText(infos.get(position).getNumber()); String mode = infos.get(position).getMode(); if("1".equals(mode)){ holder.tv_mode.setText("电话拦截"); }else if("2".equals(mode)){ holder.tv_mode.setText("短信拦截"); }else{ holder.tv_mode.setText("全部拦截"); } holder.iv_delete.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { AlertDialog.Builder builder = new Builder(BlackNumSerach.this); builder.setTitle("警告"); builder.setMessage("确定要删除这条记录么?"); builder.setPositiveButton("确定", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { //删除数据库的内容 dao.delete(infos.get(position).getNumber()); //更新界面。 infos.remove(position); //通知listview数据适配器更新 adapter.notifyDataSetChanged(); } }); builder.setNegativeButton("取消", null); builder.show(); } }); return view; } /** * view对象的容器 *记录孩子的内存地址。 *相当于一个记事本 */ } static class ViewHolder{ TextView tv_number; TextView tv_mode; ImageView iv_delete; } // -----------初始化弹出窗口---------------- private EditText et_blacknumber; private CheckBox cb_phone; private CheckBox cb_sms; private Button bt_ok; private Button bt_cancel; public void addBlackNumber(View view){ AlertDialog.Builder builder = new Builder(this); final AlertDialog dialog = builder.create(); View contentView = View.inflate(this, R.layout.dialog_add_blacknumber, null); et_blacknumber = (EditText) contentView.findViewById(R.id.et_blacknumber); cb_phone = (CheckBox) contentView.findViewById(R.id.cb_phone); cb_sms = (CheckBox) contentView.findViewById(R.id.cb_sms); bt_cancel = (Button) contentView.findViewById(R.id.cancel); bt_ok = (Button) contentView.findViewById(R.id.ok); dialog.setView(contentView, 0, 0, 0, 0); dialog.show(); bt_cancel.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { dialog.dismiss(); } }); bt_ok.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { String blacknumber = et_blacknumber.getText().toString().trim(); if(TextUtils.isEmpty(blacknumber)){ Toast.makeText(getApplicationContext(), "黑名单号码不能为空", 0).show(); return; } String mode ; if(cb_phone.isChecked()&&cb_sms.isChecked()){ //全部拦截 mode = "3"; }else if(cb_phone.isChecked()){ //电话拦截 mode = "1"; }else if(cb_sms.isChecked()){ //短信拦截 mode = "2"; }else{ Toast.makeText(getApplicationContext(), "请选择拦截模式", 0).show(); return; } //数据被加到数据库 dao.add(blacknumber, mode); //更新listview集合里面的内容。 BlackNumberInfo info = new BlackNumberInfo(); info.setMode(mode); info.setNumber(blacknumber); infos.add(0, info); //通知listview数据适配器数据更新了。 adapter.notifyDataSetChanged(); dialog.dismiss(); } }); } }
下一篇 练手小项目(3)安全卫士——黑名单拦截,广播和服务的加入进行拦截