Advertisement
Guest User

Untitled

a guest
Oct 28th, 2016
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.55 KB | None | 0 0
  1. package com.broad.sowmy.blockingcall;
  2.  
  3. import android.content.Context;
  4. import android.content.SharedPreferences;
  5. import android.media.AudioManager;
  6. import android.preference.PreferenceManager;
  7. import android.telephony.PhoneStateListener;
  8. import android.telephony.TelephonyManager;
  9. import android.widget.Toast;
  10.  
  11. import java.lang.reflect.Method;
  12.  
  13. import com.android.internal.telephony.ITelephony;
  14. /**
  15. * Created by sowmy on 24-10-2016.
  16. */
  17.  
  18. public class PhoneCallStateListener extends PhoneStateListener {
  19.  
  20. private Context context;
  21. String block_num;
  22. SharedPreferences shared;
  23.  
  24. public PhoneCallStateListener(Context context){
  25. this.context = context;
  26. }
  27.  
  28. @Override
  29. public void onCallStateChanged(int state, String incomingNumber) {
  30.  
  31.  
  32. switch (state) {
  33. case TelephonyManager.CALL_STATE_RINGING:
  34. shared=context.getSharedPreferences("get",Context.MODE_PRIVATE);
  35. block_num=shared.getString("number",null);
  36. AudioManager audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
  37. audioManager.setStreamMute(AudioManager.STREAM_RING, true);
  38. TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
  39. try {
  40. Toast.makeText(context, block_num, Toast.LENGTH_SHORT).show();
  41. Class clazz = Class.forName(tm.getClass().getName());
  42. Method method = clazz.getDeclaredMethod("getITelephony");
  43. method.setAccessible(true);
  44. ITelephony telephonyService = (ITelephony) method.invoke(tm);
  45.  
  46. System.out.println("Call " + block_num);
  47.  
  48. if (incomingNumber.equalsIgnoreCase("+91" + block_num)) {
  49.  
  50. telephonyService = (ITelephony) method.invoke(tm);
  51. telephonyService.silenceRinger();
  52. System.out.println(" in " + block_number);
  53. telephonyService.endCall();
  54. }
  55. } catch (Exception e) {
  56. Toast.makeText(context, e.toString(), Toast.LENGTH_LONG).show();
  57. }
  58. //Turn OFF the mute
  59. audioManager.setStreamMute(AudioManager.STREAM_RING, false);
  60. break;
  61. case PhoneStateListener.LISTEN_CALL_STATE:
  62.  
  63. }
  64. super.onCallStateChanged(state, incomingNumber);
  65. }
  66.  
  67. }
  68.  
  69. package com.broad.sowmy.blockingcall;
  70.  
  71. import android.content.BroadcastReceiver;
  72. import android.content.Context;
  73. import android.content.Intent;
  74. import android.telephony.PhoneStateListener;
  75. import android.telephony.TelephonyManager;
  76. import android.util.Log;
  77. import android.widget.Toast;
  78.  
  79. /**
  80. * Created by sowmy on 24-10-2016.
  81. */
  82.  
  83. public class PhoneCallReceiver extends BroadcastReceiver {
  84. @Override
  85. public void onReceive(Context context, Intent intent) {
  86. TelephonyManager tm=(TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE);
  87. PhoneCallStateListener phoneCallStateListener=new PhoneCallStateListener(context);
  88. tm.listen(phoneCallStateListener, PhoneStateListener.LISTEN_CALL_STATE);
  89.  
  90. }
  91. }
  92.  
  93. <?xml version="1.0" encoding="utf-8"?>
  94. <manifest xmlns:android="http://schemas.android.com/apk/res/android"
  95. package="com.broad.sowmy.blockingcall">
  96.  
  97.  
  98. <uses-permission android:name="android.permission.CALL_PHONE"></uses-permission>
  99. <uses-permission android:name="android.permission.READ_PHONE_STATE">
  100. </uses-permission>
  101. <uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS"></uses-permission>
  102. <uses-permission android:name="android.permission.PROCESS_INCOMING_CALLS"></uses-permission>
  103. <application
  104. android:allowBackup="true"
  105. android:icon="@mipmap/ic_launcher"
  106. android:label="@string/app_name"
  107. android:supportsRtl="true"
  108. android:theme="@style/AppTheme">
  109. <activity android:name=".MainActivity">
  110. <intent-filter>
  111. <action android:name="android.intent.action.MAIN" />
  112.  
  113. <category android:name="android.intent.category.LAUNCHER" />
  114. </intent-filter>
  115. </activity>
  116. <receiver android:name=".PhoneCallReceiver">
  117. <intent-filter>
  118. <action android:name="android.intent.action.PHONE_STATE"></action>
  119. </intent-filter>
  120. </receiver>
  121. </application>
  122.  
  123. </manifest>
  124.  
  125. package com.android.internal.telephony;
  126.  
  127. public interface ITelephony {
  128.  
  129. boolean endCall();
  130.  
  131. void answerRingingCall();
  132.  
  133. void silenceRinger();
  134.  
  135. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement