Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package aze.android.autohangup;
- import java.lang.reflect.Method;
- import android.content.BroadcastReceiver;
- import android.content.Context;
- import android.content.Intent;
- import android.telephony.TelephonyManager;
- import android.util.Log;
- import android.widget.Toast;
- import com.android.internal.telephony.ITelephony;
- public class PhoneCallReceiver extends BroadcastReceiver {
- Context context = null;
- private ITelephony telephonyService;
- @Override
- public void onReceive(final Context context, Intent intent) {
- if (intent.getAction().equals("android.intent.action.PHONE_STATE")) {
- String state = intent.getStringExtra(TelephonyManager.EXTRA_STATE);
- Log.d("PhoneStateReceiver**Call State=", state);
- if (state.equals(TelephonyManager.EXTRA_STATE_IDLE)) {
- ShowToast(context,"IDLE");
- } else if (state.equals(TelephonyManager.EXTRA_STATE_RINGING)) {
- String incomingNumber = intent.getStringExtra(TelephonyManager.EXTRA_INCOMING_NUMBER);
- Log.d("PhoneStateReceiver**Incoming call " , incomingNumber);
- ShowToast(context," ringing");
- } else if (state.equals(TelephonyManager.EXTRA_STATE_OFFHOOK)) {
- try {
- Thread.sleep(5000);
- if (killCall(context)) {
- ShowToast(context, "Blocked");
- } else {
- ShowToast(context, "Not Blocked");
- }
- } catch (Exception e) {
- e.printStackTrace();
- }
- }else{
- ShowToast(context,"phone state else");
- }
- }
- if (intent.getAction().equals(Intent.ACTION_NEW_OUTGOING_CALL)) {
- // Outgoing call
- String outgoingNumber = intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER);
- Log.d("PhoneStateReceiver **Outgoing call ", outgoingNumber);
- ShowToast(context, "Outgoing");
- } else {
- Log.d("PhoneStateReceiver **unexpected intent.action=", intent.getAction());
- }
- }
- public boolean killCall(Context context) {
- try {
- // Get the boring old TelephonyManager
- TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
- // Get the getITelephony() method
- Class classTelephony = Class.forName(telephonyManager.getClass().getName());
- Method methodGetITelephony = classTelephony.getDeclaredMethod("getITelephony");
- // Ignore that the method is supposed to be private
- methodGetITelephony.setAccessible(true);
- // Invoke getITelephony() to get the ITelephony interface
- Object telephonyInterface = methodGetITelephony.invoke(telephonyManager);
- // Get the endCall method from ITelephony
- Class telephonyInterfaceClass = Class.forName(telephonyInterface.getClass().getName());
- Method methodEndCall = telephonyInterfaceClass.getDeclaredMethod("endCall");
- // Invoke endCall()
- methodEndCall.invoke(telephonyInterface);
- return true;
- } catch (Exception e) {
- e.printStackTrace();
- return false;
- }
- }
- public void ShowToast(final Context mContext,final String mstr){
- Toast.makeText(mContext, mstr, Toast.LENGTH_LONG).show();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement