Android Project to achieve blacklist interception effect _android

Source: Internet
Author: User
Tags object object reflection sqlite stub

This example describes the implementation of blacklist in Android programming. Share to everyone for your reference, specific as follows:

1, blacklist database creation

Three fields (_id field phone blacklist number mode intercept type)

To create a table's SQL statement

CREATE TABLE Blacknumber (_id integer primary key autoincrement, phone varchar (), mode varchar (5));

Combine projects to create databases, and corresponding tables

2.BlackNumberDao

Blacknumberdao Single case mode

Package Com.itheima.mobilesafe74.db.dao;
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;
Import Com.itheima.mobilesafe74.db.BlackNumberOpenHelper;

Import Com.itheima.mobilesafe74.db.domain.BlackNumberInfo;
  public class Blacknumberdao {private Blacknumberopenhelper blacknumberopenhelper; Blacknumberdao single case mode//1, privatization construction Method Private Blacknumberdao (context context) {//CREATE database already its table body Blacknumberopenhelper
  = new Blacknumberopenhelper (context);
  //2, declares an object of the current class private static Blacknumberdao Blacknumberdao = null; 3, provide a static method, if the object of the current class is empty, create a new public static Blacknumberdao getinstance {if (Blacknumberdao = null)
    {Blacknumberdao = new Blacknumberdao (context);
  return Blacknumberdao;
 /** Add an entry * @param phone intercept number * @param mode intercept type (1: SMS 2: Phone 3: Intercept all (SMS + phone)) * * public void Insert (String phone,string mode) {//1, open database, ready for write operation Sqlitedatabase db = Blacknumberopenhelper.getwrit

    Abledatabase ();
    Contentvalues values = new Contentvalues ();
    Values.put ("Phone", phone);
    Values.put ("mode", mode);
    
    Db.insert ("Blacknumber", null, values);
  Db.close (); /** Delete a phone number from the database * @param phone Delete phone number * * * public void Delete (String phone) {Sqlitedatabase db = Blackn

    Umberopenhelper.getwritabledatabase ();
    
    Db.delete ("Blacknumber", "phone =?", new String[]{phone});
  Db.close ();
  /** * According to the phone number, update the interception mode * @param phone number * Update blocking mode * @param mode (1: SMS 2: Phone 3: Intercept all (SMS + phone) * *

    public void Update (String phone,string mode) {Sqlitedatabase db = Blacknumberopenhelper.getwritabledatabase ();
    Contentvalues contentvalues = new Contentvalues ();
    
    Contentvalues.put ("mode", mode);
    
    Db.update ("Blacknumber", contentvalues, "phone =?", new String[]{phone});
Db.close ();  /** * @return Query to all numbers in the database and the collection of blocking types/public list<blacknumberinfo> FindAll () {Sqlitedatabas

    E db = Blacknumberopenhelper.getwritabledatabase ();
    Cursor Cursor = db.query ("Blacknumber", New string[]{"Phone", "mode"}, NULL, NULL, NULL, NULL, "_id desc");
    list<blacknumberinfo> blacknumberlist = new arraylist<blacknumberinfo> ();
      while (Cursor.movetonext ()) {Blacknumberinfo blacknumberinfo = new Blacknumberinfo ();
      Blacknumberinfo.phone = cursor.getstring (0);
      Blacknumberinfo.mode = cursor.getstring (1);
    Blacknumberlist.add (Blacknumberinfo);
    } cursor.close ();
    
    Db.close ();
  return blacknumberlist; /** * 20 data per query * @param index value/public list<blacknumberinfo> find (int index) {SQLite

    Database db = Blacknumberopenhelper.getwritabledatabase ();
Cursor Cursor = Db.rawquery ("Select Phone,mode from Blacknumber order by _id Desc?,", New Limit ""});    
    list<blacknumberinfo> blacknumberlist = new arraylist<blacknumberinfo> ();
      while (Cursor.movetonext ()) {Blacknumberinfo blacknumberinfo = new Blacknumberinfo ();
      Blacknumberinfo.phone = cursor.getstring (0);
      Blacknumberinfo.mode = cursor.getstring (1);
    Blacknumberlist.add (Blacknumberinfo);
    } cursor.close ();
    
    Db.close ();
  return blacknumberlist; /** * @return The total number of entries for the data in the database, and returns 0 representing no data or exception/public int GetCount () {Sqlitedatabase db = Blacknumberopenh
    Elper.getwritabledatabase ();
    int count = 0;
    Cursor Cursor = Db.rawquery ("SELECT count (*) from blacknumber;", null);
    if (Cursor.movetonext ()) {count = Cursor.getint (0);
    } cursor.close ();
    Db.close ();
  return count; /** * @param phone telephone number for query criteria * @return interception mode for incoming phone number 1: SMS 2: Tel 3: All 0: No such data/public int GetMode (String phone)
    {Sqlitedatabase db = Blacknumberopenhelper.getwritabledatabase (); Int mode = 0;
    Cursor Cursor = db.query ("Blacknumber", New string[]{"mode"}, "phone =?", new String[]{phone}, NULL, null,null);
    if (Cursor.movetonext ()) {mode = Cursor.getint (0);
    } cursor.close ();
    Db.close ();
  return mode;

 }
}

3, loading more trigger conditions

    1. Monitor state changes
    2. Scroll to the bottom and the last ListView entry is visible
    3. Scrolling state changes scrolling------> stops (Idle)

After loading the next page of data, you need to add to the end of the previous page of data

Listens for its scrolling state lv_blacknumber.setonscrolllistener (The new Onscrolllistener () {//scrolling process, the state changes the calling method () @Override public void on Scrollstatechanged (abslistview view, int scrollstate) {//onscrolllistener.scroll_state_fling fast scrolling//OnScrol Llistener.scroll_state_idle idle state//Onscrolllistener.scroll_state_touch_scroll touch to scroll state if (mblacknumber List!=null) {//Condition one: Scroll to stop state//Condition two: The last entry is visible (the last entry's index value >= the total number of entries in the data adapter-1) if (scrollstate = = Onscrolllis Tener. Scroll_state_idle && lv_blacknumber.getlastvisibleposition () >=mblacknumberlist.size ()-1 &am p;&!misload) {/*misload Prevent duplicate variables if the misload is currently being loaded, it will be true, after this load, the misload will be changed to False if the next load needs to be done 
        When the line, will judge the appeal Misload variable, false, if true, you need to wait for the last load complete, change its value to false and then load//////////////////////////////////// if (Mcount>mblacknumberlist.size ()) {//Load next page Data new Thread () {public void run () {
              //1, get the Operation blacklist database object Mdao = Blacknumberdao.getinstance (Getapplicationcontext ());
              2, the query part of the data list<blacknumberinfo> Moredata = Mdao.find (Mblacknumberlist.size ());
              3, the process of adding the next page of data Mblacknumberlist.addall (Moredata);
            4, notify the data adapter refresh mhandler.sendemptymessage (0);
        }}.start ();
      Call method @Override public void Onscroll (Abslistview view, int firstvisibleitem,
 int visibleitemcount, int totalitemcount) {}});

4. Intercept SMS

SMS in the reception, broadcast send, listen to broadcast recipient, intercept SMS (ordered)
Raise the priority level of the broadcast to the highest (1000)

5. Intercept Telephone

There is a call to dial, in the Bell state, the bell state through the code to hang up the phone (Aidl, reflection), intercept the phone
The way to hang up the phone number, placed in the Aidl file named Endcall

Here to see Telephonemanager source code, to find the way to get Itelephony objects

ServiceManager This type of Android is hidden from the developer, so it's not possible to call its method directly, so it needs reflection to call

ITelephony.Stub.asInterface (Servicemanager.getservice (Context.telephony_service));

<uses-permission android:name= "Android.permission.CALL_PHONE"/>

//1, get servicemanager byte code file
Class <?> clazz = Class.forName ("Android.os.ServiceManager");
2, Get methods method
= Clazz.getmethod ("GetService", string.class);
3, Reflection calls this method
IBinder IBinder = (ibinder) method.invoke (null, context.telephony_service);
4, call Get Aidl File object method
Itelephony itelephony = ITelephony.Stub.asInterface (ibinder);
5, call the Endcall method hidden in the Aidl

Blacknumberservice

Itelephony.endcall ();

Package com.itheima.mobilesafe74.service;
Import java.lang.reflect.InvocationTargetException;

Import Java.lang.reflect.Method;
Import Com.android.internal.telephony.ITelephony;
Import Com.itheima.mobilesafe74.db.dao.BlackNumberDao;
Import Com.itheima.mobilesafe74.service.AddressService.MyPhoneStateListener;
Import Android.app.Service;
Import Android.content.BroadcastReceiver;
Import Android.content.Context;
Import android.content.Intent;
Import Android.content.IntentFilter;
Import Android.os.IBinder;
Import Android.telephony.PhoneStateListener;
Import Android.telephony.SmsMessage;
Import Android.telephony.TelephonyManager;
Import Android.util.Log;

Import Android.view.WindowManager;
  public class Blacknumberservice extends Service {private Innersmsreceiver minnersmsreceiver;
  Private Blacknumberdao Mdao;
  Private Telephonymanager MTM;
  Private Myphonestatelistener Mphonestatelistener; @Override public void OnCreate () {Mdao = Blacknumberdao.getinstAnce (Getapplicationcontext ());
    Intercept SMS Intentfilter intentfilter = new Intentfilter ();
    Intentfilter.addaction ("Android.provider.Telephony.SMS_RECEIVED");
    
    Intentfilter.setpriority (1000);
    Minnersmsreceiver = new Innersmsreceiver ();
    
    Registerreceiver (Minnersmsreceiver, Intentfilter);
    Monitor the status of the phone//1, telephone Manager Object MTM = (Telephonymanager) getsystemservice (Context.telephony_service);
    2, monitor the status of the phone Mphonestatelistener = new Myphonestatelistener ();
    
    Mtm.listen (Mphonestatelistener, phonestatelistener.listen_call_state);
  Super.oncreate (); Class Myphonestatelistener extends phonestatelistener{//3, manual rewrite, the way the phone status changes will trigger @Override public void on
        callstatechanged (int state, String incomingnumber) {switch (state) {case Telephonymanager.call_state_idle:
      Break
      Case TelephonyManager.CALL_STATE_OFFHOOK:break;      Case telephonymanager.call_state_ringing://Hang up the phone aidl file to go//  Mtm.endcall ();
        Endcall (Incomingnumber);
      Break
    } super.oncallstatechanged (state, Incomingnumber); Class Innersmsreceiver extends broadcastreceiver{@Override public void onreceive (context context, Intent Intent) {//Get SMS content, get send SMS phone number, if this phone number is in blacklist, and interception mode is 1 (SMS) or 3 (all), intercept SMS//1, get SMS content object[] objects = (obje
      Ct[]) Intent.getextras (). Get ("PDUs"); 2, loop through the SMS process for (object object:objects) {//3, get SMS object smsmessage SMS = SMSMESSAGE.CREATEFROMPDU (b
        Yte[]) object;
        4, get the basic information of SMS object String originatingaddress = Sms.getoriginatingaddress ();
        
        String messagebody = Sms.getmessagebody ();
        
        int mode = Mdao.getmode (originatingaddress);
        if (mode = = 1 | | mode = = 3) {//Intercept SMS (Android 4.4 version Invalid SMS database, delete) abortbroadcast ();
  @Override public IBinder onbind (Intent arg0) {return null}}}} } public void Endcall(String phone)
    
    {int mode = Mdao.getmode (phone);
      if (mode = = 2 | | mode = = 3) {//ITelephony.Stub.asInterface (Servicemanager.getservice (Context.telephony_service)); ServiceManager This type of Android is hidden from the developer, so it's not possible to call its method directly, it needs reflection to call try {//1, get the ServiceManager bytecode file CLASS&LT;?&G T
        Clazz = Class.forName ("Android.os.ServiceManager");
        2, Get methods method = Clazz.getmethod ("GetService", String.class);
        3, Reflection calls this method IBinder IBinder = (ibinder) method.invoke (null, context.telephony_service);
        4, call Get Aidl File object method Itelephony Itelephony = ITelephony.Stub.asInterface (IBinder);
      5, call the Endcall method hidden in the Aidl itelephony.endcall ();
      catch (Exception e) {e.printstacktrace (); @Override public void OnDestroy () {if (minnersmsreceiver!=null) {Unregisterreceiver}}} (Minnersmsrec
    Eiver);
  } Super.ondestroy ();

 }
}

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.