Security guard-blacklist interception and security guard
I have been thinking about what I can use to review all the knowledge points of Android. It is like it, mobile security guard, and almost all the knowledge points. Recently I also want to simulate a foreign toolbox to play with it, so, take notes by the way.
The final high-imitation toolbox will also be open-source on github ~~~ Start.
My idea always begins with layout, because if you want to know how to use it for average users, you or you can refer to what others have already done.
Do not doubt the title above, because this is a test Demo of mine.
The main interface is a title bar that allows you to enter numbers and select the mode of phone interception and text message interception. Recently, it was despised by others, saying that my code style is poor, so this article will use annotations ......
First, we need to save data. I also use databases to practice it. However, most security guards come with a data query, such as advertising text messages, fraud calls will go to the database to traverse and get the query results.
BlackNumSearchDao. java help class
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) {// version of the context database name super (Context, "blacknumber. db ", null, 1);} // initialize the database table structure @ Overridepublic void onCreate (SQLiteDatabase db) {// create the number mode with the table name blacknumber id increasing by 20 bytes 2-node db.exe cSQL ("create table blacknumber (_ id integer primary key autoincrement, number varchar (20 ), mode varchar (2) ") ;}@ Overridepublic void onUpgrade (SQLiteDatabase db, int oldVersion, int newVersion ){}}
The database class mainly stores some database statements, such as query, add, delete, and query.
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);}/*** query whether the blacklist number exists * @ Param number * @ return */public boolean find (String number) {boolean result = false; SQLiteDatabase db = helper. getReadableDatabase (); // The statement after where is used for filtering. The condition query indicates that a certain number of blacknumber Cursor cursor is db. rawQuery ("select * from blacknumber where number =? ", New String [] {number}); if (cursor. moveToNext () {result = true;} cursor. close (); db. close (); return result;}/*** query the interception mode of the blacklist number * @ param number * @ return the interception mode of the number, return null */public String findMode (String number) {String result = null; SQLiteDatabase db = helper. getReadableDatabase (); // query? ModeCursor 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;}/*** query all blacklist numbers * @ 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 (cur Sor. 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;}/*** Add a blacklist number * @ param number blacklist number * @ param mode interception mode 1. phone interception 2. text message interception 3. block all */public void add (String number, String mode) {SQLiteDatabase db = helper. getWritableDatabas E (); ContentValues values = new ContentValues (); values. put ("number", number); values. put ("mode", mode); db. insert ("blacknumber", null, values); db. close ();} /*** modify the interception mode of the blacklist number * @ param number the blacklist number to be modified * @ param newmode the new interception mode */public void update (String number, String newmode) {SQLiteDatabase db = helper. getWritableDatabase (); ContentValues values = new ContentValues (); values. put ("mode", newmode); // The table name is blacknu. Mber db. update ("blacknumber", values, "number =? ", New String [] {number}); db. close ();}/*** delete blacklist number * @ param number the blacklist number to be deleted */public void delete (String number) {SQLiteDatabase db = helper. getWritableDatabase (); db. delete ("blacknumber", "number =? ", New String [] {number}); db. close ();}}
As shown above, we have created an entity class for storage, which is convenient to use.
Package com. example. darkbutton. blackNumSerach;/*** business bean of the blacklist number * @ 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 ;}@ Overridepublic String toString () {return "BlackNumberInfo [number =" + number + ", mode =" + mode + "]" ;}}
Main class. I want to initialize the listview and process the AlertDialog that appears when a button is clicked.
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; priv Ate List <BlackNumberInfo> infos; private CallSmsSafeAdapter adapter; @ ViewInject (R. id. textView1) TextView textView; @ ViewInject (R. id. lv_callsms_safe) ListView; @ Overrideprotected 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 (); adap Ter = new CallSmsSafeAdapter (); listView. setAdapter (adapter);} private class CallSmsSafeAdapter extends BaseAdapter {@ Overridepublic int getCount () {return infos. size () ;}@ Overridepublic Object getItem (int position) {return null ;}@ Overridepublic long getItemId (int position) {return 0 ;}@ Overridepublic View getView (final int position, view convertView, ViewGroup parent) {View view; ViewHolder holder; // 1. Reduce the number of view objects created in the memory if (convertView = null) {// convert a layout file to a view object. View = View. inflate (getApplicationContext (), R. layout. list_item_callsms, null); // 2. Reduce the number of times the child queries the address of the objects in the memory. 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); // when the child is born, find their reference, store it in notepad, and put it in his father's pocket view. setTag (holder);} else {// Log. I (TAG, "view objects with historical history in the kitchen, reuse view objects cached in history:" + position); view = convertView; holder = (ViewHolder) view. getTag (); // 5%} hol Der. TV _number.setText (infos. get (position ). getNumber (); String mode = infos. get (position ). getMode (); if ("1 ". equals (mode) {holder. TV _mode.setText ("phone interception");} else if ("2 ". equals (mode) {holder. TV _mode.setText ("SMS interception");} else {holder. TV _mode.setText ("All interceptions");} holder. iv_delete.setOnClickListener (new OnClickListener () {@ Overridepublic void onClick (View v) {AlertDialog. builder builder = new Builder (BlackNumSerach. This); builder. setTitle ("warning"); builder. setMessage ("are you sure you want to delete this record? "); Builder. setPositiveButton ("OK", new DialogInterface. onClickListener () {@ Overridepublic void onClick (DialogInterface dialog, int which) {// Delete the database content dao. delete (infos. get (position ). getNumber (); // update the interface. Infos. remove (position); // notifies the listview data adapter to update the adapter. yydatasetchanged () ;}}); builder. setNegativeButton ("cancel", null); builder. show () ;}}); return view ;}/ *** view object container * records the child's memory address. * Equivalent to a notepad */} static class ViewHolder {TextView TV _number; TextView TV _mode; ImageView iv_delete;} // ----------- initialization pop-up window -------------- 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 content View = 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 () {@ Overridepublic void onClick (View v) {dialog. dismiss () ;}}); bt_ OK .setOnClickListener (new OnClickListener () {@ Overridepublic void onClick (View v) {String blacknumber = et_blacknumber.getText (). toString (). trim (); if (TextUtils. isEmpty (blacknumber) {Toast. makeText (getApplicationContext (), "The Blacklist number cannot be blank", 0 ). show (); return;} String mod E; if (cb_phone.isChecked () & cb_sms.isChecked () {// All interception mode = "3";} else if (cb_phone.isChecked ()) {// telephone interception mode = "1";} else if (cb_sms.isChecked () {// SMS interception mode = "2";} else {Toast. makeText (getApplicationContext (), "select interception mode", 0 ). show (); return;} // The data is added to the database dao. add (blacknumber, mode); // update the content in the listview set. BlackNumberInfo info = new BlackNumberInfo (); info. setMode (mode); info. setNumber (blacknumber); infos. add (0, info); // notifies the listview data adapter that the data has been updated. Adapter. notifyDataSetChanged (); dialog. dismiss ();}});}}
In the next section, add services and broadcasts to intercept text messages and calls.
Next expert Program (3) security guard-blacklist interception, broadcast and service addition Interception