Advertisement
Guest User

PhonecallStateBroadcastReceiver.java

a guest
Apr 11th, 2013
417
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.95 KB | None | 0 0
  1. package aze.android.autohangup;
  2.  
  3. import java.lang.reflect.Method;
  4.  
  5. import android.content.BroadcastReceiver;
  6. import android.content.Context;
  7. import android.content.Intent;
  8. import android.telephony.TelephonyManager;
  9. import android.util.Log;
  10. import android.widget.Toast;
  11.  
  12. import com.android.internal.telephony.ITelephony;
  13.  
  14. public class PhoneCallReceiver extends BroadcastReceiver {
  15.     Context context = null;
  16.     private ITelephony telephonyService;
  17.  
  18.     @Override
  19.     public void onReceive(final Context context, Intent intent) {
  20.  
  21.         if (intent.getAction().equals("android.intent.action.PHONE_STATE")) {
  22.             String state = intent.getStringExtra(TelephonyManager.EXTRA_STATE);
  23.             Log.d("PhoneStateReceiver**Call State=", state);
  24.  
  25.             if (state.equals(TelephonyManager.EXTRA_STATE_IDLE)) {
  26.                 ShowToast(context,"IDLE");
  27.             } else if (state.equals(TelephonyManager.EXTRA_STATE_RINGING)) {
  28.                 String incomingNumber = intent.getStringExtra(TelephonyManager.EXTRA_INCOMING_NUMBER);
  29.                 Log.d("PhoneStateReceiver**Incoming call " , incomingNumber);
  30.                 ShowToast(context," ringing");
  31.  
  32.             } else if (state.equals(TelephonyManager.EXTRA_STATE_OFFHOOK)) {
  33.  
  34.                 try {
  35.                     Thread.sleep(5000);
  36.                     if (killCall(context)) {
  37.                         ShowToast(context, "Blocked");
  38.                     } else {
  39.                         ShowToast(context, "Not Blocked");
  40.                     }
  41.                 } catch (Exception e) {
  42.                     e.printStackTrace();
  43.                 }
  44.             }else{
  45.                 ShowToast(context,"phone state else");
  46.             }
  47.         }
  48.         if (intent.getAction().equals(Intent.ACTION_NEW_OUTGOING_CALL)) {
  49.             // Outgoing call
  50.             String outgoingNumber = intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER);
  51.             Log.d("PhoneStateReceiver **Outgoing call ", outgoingNumber);
  52.             ShowToast(context, "Outgoing");
  53.         } else {
  54.             Log.d("PhoneStateReceiver **unexpected intent.action=", intent.getAction());
  55.         }
  56.     }
  57.  
  58.     public boolean killCall(Context context) {
  59.         try {
  60.             // Get the boring old TelephonyManager
  61.             TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
  62.  
  63.             // Get the getITelephony() method
  64.             Class classTelephony = Class.forName(telephonyManager.getClass().getName());
  65.             Method methodGetITelephony = classTelephony.getDeclaredMethod("getITelephony");
  66.             // Ignore that the method is supposed to be private
  67.             methodGetITelephony.setAccessible(true);
  68.             // Invoke getITelephony() to get the ITelephony interface
  69.             Object telephonyInterface = methodGetITelephony.invoke(telephonyManager);
  70.             // Get the endCall method from ITelephony
  71.             Class telephonyInterfaceClass = Class.forName(telephonyInterface.getClass().getName());
  72.             Method methodEndCall = telephonyInterfaceClass.getDeclaredMethod("endCall");
  73.             // Invoke endCall()
  74.             methodEndCall.invoke(telephonyInterface);
  75.             return true;
  76.         } catch (Exception e) {
  77.             e.printStackTrace();
  78.             return false;
  79.         }
  80.     }
  81.     public void ShowToast(final Context mContext,final String mstr){
  82.         Toast.makeText(mContext, mstr, Toast.LENGTH_LONG).show();
  83.     }
  84. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement