This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<uses-permission android:name="android.permission.READ_PHONE_STATE" /> | |
<uses-permission android:name="android.permission.CALL_PHONE" /> | |
<receiver android:name=".IncomingCallReceiver" android:enabled="true" android:exported="true"> | |
<intent-filter> | |
<action android:name="android.intent.action.PHONE_STATE" /> | |
</intent-filter> | |
</receiver> | |
-------------- package should be same as below----------- | |
package com.android.internal.telephony; | |
/** | |
* Created by Md. Amirul Islam on 22-Sep-19. | |
* Email : amirul.islam@uslbd.com | |
* Unisoft™ Systems Limited | |
*/ | |
public interface ITelephony { | |
boolean endCall(); | |
void answerRingingCall(); | |
void silenceRinger(); | |
} | |
---------------------------- | |
IncomingCallReceiver.java | |
public class IncomingCallReceiver extends BroadcastReceiver { | |
@Override | |
public void onReceive(Context context, Intent intent) { | |
ITelephony telephonyService; | |
try { | |
String state = intent.getStringExtra(TelephonyManager.EXTRA_STATE); | |
String number = intent.getExtras().getString(TelephonyManager.EXTRA_INCOMING_NUMBER); | |
if(state.equalsIgnoreCase(TelephonyManager.EXTRA_STATE_RINGING)){ | |
TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE); | |
try { | |
Method m = tm.getClass().getDeclaredMethod("getITelephony"); | |
m.setAccessible(true); | |
telephonyService = (ITelephony) m.invoke(tm); | |
if ((number != null)) { | |
telephonyService.endCall(); | |
Toast.makeText(context, "Ending the call from: " + number, Toast.LENGTH_SHORT).show(); | |
} | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} | |
Toast.makeText(context, "Ring " + number, Toast.LENGTH_SHORT).show(); | |
} | |
if(state.equalsIgnoreCase(TelephonyManager.EXTRA_STATE_OFFHOOK)){ | |
Toast.makeText(context, "Answered " + number, Toast.LENGTH_SHORT).show(); | |
} | |
if(state.equalsIgnoreCase(TelephonyManager.EXTRA_STATE_IDLE)){ | |
Toast.makeText(context, "Idle "+ number, Toast.LENGTH_SHORT).show(); | |
} | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} | |
} | |
} | |
================================= | |
MainActivity.java | |
----------------- | |
public class MainActivity extends AppCompatActivity { | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_main); | |
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { | |
checkPerms(); | |
} | |
} | |
@RequiresApi(api = Build.VERSION_CODES.M) | |
private void checkPerms() { | |
String[] perms = new String[]{ | |
Manifest.permission.CALL_PHONE, | |
Manifest.permission.READ_PHONE_STATE | |
}; | |
List<String> requestingPerms = new ArrayList<>(); | |
for (String perm : perms) { | |
if (checkSelfPermission(perm) != | |
PackageManager.PERMISSION_GRANTED) { | |
requestingPerms.add(perm); | |
} | |
} | |
if (requestingPerms.size() > 0) { | |
requestPermissions(requestingPerms.toArray(new String[requestingPerms.size()]), 0); | |
} | |
} | |
} | |
------------------------------- |
https://stackoverflow.com/questions/5990590/how-to-detect-phone-call-broadcast-receiver-in-android
https://gist.github.com/ftvs/e61ccb039f511eb288ee
http://droidcodesnippets.blogspot.com/2012/08/block-all-calls.html
http://www.nikola-breznjak.com/blog/android/make-native-android-app-can-block-phone-calls/
https://www.studytutorial.in/android-phonestatelistener-phone-call-broadcast-receiver-tutorial