How to block a mobile number call and message receiving in android application development?
create PhoneCallReceiver .java
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
import android.util.Log;
import android.widget.Toast;
public class PhoneCallReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
TelephonyManager telephony = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
PhoneCallStateListener customPhoneListener = new PhoneCallStateListener(context);
telephony.listen(customPhoneListener, PhoneStateListener.LISTEN_CALL_STATE);
}}
now create PhoneCallStateListener .java
import java.lang.reflect.Method;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.media.AudioManager;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
import android.widget.Toast;
import com.android.internal.telephony.ITelephony;
public class PhoneCallStateListener extends PhoneStateListener {
private Context context;
public PhoneCallStateListener(Context context){
this.context = context;
}
@Override
public void onCallStateChanged(int state, String incomingNumber) {
SharedPreferences prefs=PreferenceManager.getDefaultSharedPreferences(context);
switch (state) {
case TelephonyManager.CALL_STATE_RINGING:
String block_number = prefs.getString("block_number", null);
AudioManager audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
//Turn ON the mute
audioManager.setStreamMute(AudioManager.STREAM_RING, true);
TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
try {
Toast.makeText(context, "in"+block_number, Toast.LENGTH_LONG).show();
Class clazz = Class.forName(telephonyManager.getClass().getName());
Method method = clazz.getDeclaredMethod("getITelephony");
method.setAccessible(true);
ITelephony telephonyService = (ITelephony) method.invoke(telephonyManager);
//Checking incoming call number
System.out.println("Call "+block_number);
if (incomingNumber.equalsIgnoreCase("+91"+block_number)) {
//telephonyService.silenceRinger();//Security exception problem
telephonyService = (ITelephony) method.invoke(telephonyManager);
telephonyService.silenceRinger();
System.out.println(" in "+block_number);
telephonyService.endCall();
}
} catch (Exception e) {
Toast.makeText(context, e.toString(), Toast.LENGTH_LONG).show();
}
//Turn OFF the mute
audioManager.setStreamMute(AudioManager.STREAM_RING, false);
break;
case PhoneStateListener.LISTEN_CALL_STATE:
}
super.onCallStateChanged(state, incomingNumber);
}}
Now in src create this package com.android.internal.telephony
now in this package Right Click -> New -> File now give name ITelephony.aidl
and paste this code
package com.android.internal.telephony;
interface ITelephony {
boolean endCall();
void answerRingingCall();
void silenceRinger();
}
NOTE: Code is tested in Android 2.2 (Froyo),2.3 (GingerBread)
How to Block outgoing calls and Text SMS
http://code.google.com/p/krvarma-android-samples/source/browse/trunk/DetectCalls/src/com/varma/samples/detectcalls/receivers/OutgoingCallReceiver.java
How to block/unblock all incoming/outgoing calls/text messages in android?
After 8 Hours R&D I found my Own Solution for my above question:
Manifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.deactivateimservicedemo"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="19" />
<uses-feature android:name="android.hardware.telephony" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS" />
<uses-permission android:name="android.permission.CALL_PHONE" />
<!-- For Incoming Text messages -->
<uses-permission android:name="android.permission.RECEIVE_SMS" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.deactivateimservicedemo.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
tools:context="com.example.deactivateimservicedemo.MainActivity$PlaceholderFragment" >
<Button
android:id="@+id/btnActivate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Activate" />
<Button
android:id="@+id/btnDeactivate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Deactivate" />
</LinearLayout>
MainActivity.java
package com.example.deactivateimservicedemo;
import java.lang.reflect.Method;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.telephony.TelephonyManager;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import com.android.internal.telephony.ITelephony;
public class MainActivity extends ActionBarActivity {
private Button btnActivate, btnDeactivate;
private BroadcastReceiver receiver;
private IntentFilter filter;
private boolean isReceiverRegistered;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnActivate = (Button) findViewById(R.id.btnActivate);
btnActivate.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
/*Register Receiver*/
registerReceiver(receiver, filter);
isReceiverRegistered = true;
}
});
btnDeactivate = (Button) findViewById(R.id.btnDeactivate);
btnDeactivate.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
/*UnRegister Receiver*/
if(isReceiverRegistered){
Log.i("Receiver is", "Register");
unregisterReceiver(receiver);
}else {
Log.i("Receiver is", "Not Register");
}
}
});
filter = new IntentFilter();
filter.addAction("android.intent.action.PHONE_STATE");
filter.addAction("android.provider.Telephony.SMS_RECEIVED");
receiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
Log.i("Intent Action", action);
if(intent!=null){
if(action.equals("android.intent.action.PHONE_STATE")){
try {
TelephonyManager tm = (TelephonyManager) context
.getSystemService(Context.TELEPHONY_SERVICE);
Class<?> c = Class.forName(tm.getClass().getName());
Method m = c.getDeclaredMethod("getITelephony");
m.setAccessible(true);
com.android.internal.telephony.ITelephony telephonyService = (ITelephony) m
.invoke(tm);
telephonyService.endCall();
} catch (Exception e) {
Log.e("Exception", e.toString());
}
}else if(action.equals("android.provider.Telephony.SMS_RECEIVED")){
Bundle extras = intent.getExtras();
if (extras != null) {
abortBroadcast();
}
}
}
}
};
}
@Override
protected void onDestroy() {
btnDeactivate.performClick();
super.onDestroy();
}
}
That's IT.
To block the out going call from my android app.
You can block the outgoing call using the setResultData(null) function in the onReceive method of the Broaadcast receiver.
public class BlockOutgoing extends BroadcastReceiver
{
String number;
@Override
public void onReceive(Context context, Intent intent)
{
Log.d("12280", "asdasNumber is-->> " + number);
number = intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER);
setResultData(null);
Toast.makeText(context, "Outgoing Call Blocked" , 5000).show();
}
}
In the manifest file, you need to register the receiver like this,
<receiver
android:name=".BlockOutgoing"
android:label="@string/app_name" >
<intent-filter android:priority="1">
<action android:name="android.intent.action.NEW_OUTGOING_CALL" />
</intent-filter>
</receiver>
Also define the permission to intercept the outgoing call,
<uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS" />
Edit-
To unregister a broadcast receiver, follow this link
Related Topics
How to Detect When a User Plugs Headset on Android Device? (Opposite of Action_Audio_Becoming_Noisy)
Android: Cursoradapter, Listview and Checkbox
Android Datepicker Change to Only Month and Year
In Android 7 (API Level 24) My App Is Not Allowed to Mute Phone (Set Ringer Mode to Silent)
Phonegap Camera Restarts the Application
How to Force Action Bar Overflow Icon to Show
Android Keeps Caching My Intents Extras, How to Declare a Pending Intent That Keeps Fresh Extras
Safely Fixing: Javax.Net.Ssl.Sslpeerunverifiedexception: No Peer Certificate
Android: Determine Security Type of Wifi Networks in Range (Without Connecting to Them)
Android Youtube Upload Video with Static Username and Password
How to Stop Asynctask Thread in Android
How to Add "Go Back" Function in Webview Inside Fragment
How to Tell If the Sdcard Is Mounted in Android
Android Studio 3.1 "Run" Not Compiling Code