Rejecting Incoming Calls Immediately Android - android

Can we block incoming phone calls in android before it reaches the phone application?
I have made an application to block incoming phone calls my problem is that even though the call is suspended immediately it pops up the calling screen for a second. Any suggestions how to avoid that inefficiency?
Also if there is a way to delete the phone call from the call history please let me know how to do it?
This is my main activity
import android.Manifest;
import android.content.pm.PackageManager;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
public class MainActivity extends AppCompatActivity {
private static final int PERMISSION_REQUEST_PHONE_STATE_GRANTED = 1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getPermission();
}
private void getPermission() {
if (ContextCompat.checkSelfPermission(this,
Manifest.permission.READ_PHONE_STATE) !=
PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this, new String[]
{Manifest.permission.READ_PHONE_STATE},
PERMISSION_REQUEST_PHONE_STATE_GRANTED);
}
}
}
This is my CallReceiver class which extends BroadcastReceiver
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.telephony.TelephonyManager;
import com.android.internal.telephony.ITelephony;
import java.lang.reflect.Method;
public class CallReceiver extends BroadcastReceiver {
private ITelephony telephonyService;
#Override
public void onReceive(Context context, Intent intent) {
TelephonyManager telephony = (TelephonyManager)
context.getSystemService(Context.TELEPHONY_SERVICE);
//make sure it's not an outgoing call
if (telephony.getCallState() == 1) {
try {
Class c = Class.forName(telephony.getClass().getName());
Method m = c.getDeclaredMethod("getITelephony");
m.setAccessible(true);
telephonyService = (ITelephony) m.invoke(telephony);
telephonyService.endCall();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
This is my manifest file
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.shulem.phonecallsblocker">
<uses-permission android:name="android.permission.CALL_PHONE" />
<uses-permission
android:name="android.permission.READ_PHONE_STATE"/>
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category
android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
<receiver
android:name=".CallReceiver"
android:enabled="true"
android:exported="true">
<intent-filter>
<action
android:name="android.intent.action.PHONE_STATE"/>
</intent-filter>
</receiver>
</application>
</manifest>
And this is a picture of the popup screen (which pops up after the call is already suspended on the caller’s side)enter image description here
Ps
I tried to add android priority but it didn’t help.

Relying on phone state to determine if there is an incoming call and then ending it via TelephonyManager is never going to be a reliable approach to implementing a call screening app of this nature. The broadcasts for phone state are sent asynchronously while the system is busy doing all sorts of other things related to call setup. So on some devices it might work, but on others there is a good chance it won't.

Related

BroadcastReceiver and ACTION_BOND_STATE_CHANGED working partialy in Android 9.0

I am trying to catch the event from the pairing process with the android via Broadcast receiver. As it seems, the BluetoothDevice.BOND_BONDING is works, but the BluetoothDevice.BOND_BONDED not.
In the old android versions this worked (tried with Android 6 and 7), however with the newer ones (tried Android 9, several devices) this does not work. In order to reproduce the problem I've made a simple program:
Java file:
package com.example.bluetoothtest;
import android.bluetooth.BluetoothDevice;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
public class MainActivity extends AppCompatActivity {
BroadcastReceiver receiver;
BluetoothDevice mDevice;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
receiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (BluetoothDevice.ACTION_BOND_STATE_CHANGED.equals(action)){
mDevice = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
if (mDevice.getBondState() == BluetoothDevice.BOND_BONDED) {
//means device paired
Log.d("bt", "bonded");
}
else if(mDevice.getBondState() == BluetoothDevice.BOND_BONDING) {
Log.d("bt", "bonding");
}
}
}
};
}
#Override
protected void onStart() {
super.onStart();
IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_BOND_STATE_CHANGED);
registerReceiver(receiver, filter);
}
#Override
protected void onStop() {
super.onStop();
unregisterReceiver(receiver);
}
}
Manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.bluetoothtest">
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
</manifest>
Did anybody else notice this problem? Am I missing a permission? Couldn't find anything relevant on the net.
You should try to retrieve the EXTRA_BOND_STATE like that :
val state = intent.extras?.get(BluetoothDevice.EXTRA_BOND_STATE) as Int
I have faced a similar problem. I have noticed one thing with devices such as HeadPhone, mouse or keyboard is that when you pair such devices, it is immediately connected to our Android device.
So android send us
android.bluetooth.device.action.ACL_CONNECTED
connected broadcast immediately. In case we receive this broadcast, it is safe to assume that Bluetooth device is already paired.
I would recommend adding this permission for Android 12 and above devices to listen to the connected broadcast.
android.permission.BLUETOOTH_CONNECT

Broadcast receiver is not calling on internet connectivity check

I am trying to make a simple app which will notify if there is internet connection available or not on internet connectivity change. i have found some solution on internet and tries to implement them but somehow its not working. my broadcast receiver which i have registered on my manifest file is not calling on network connectivity change.
Manifest
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver
android:name=".NetworkStateChangeReceiver">
<intent-filter >
<action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
</intent-filter>
</receiver>
</application>
Broadcast Receiver
package com.gdm.internetconnectivitycheck;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.support.v4.content.LocalBroadcastManager;
import android.util.Log;
import static android.content.Context.CONNECTIVITY_SERVICE;
public class NetworkStateChangeReceiver extends BroadcastReceiver {
public static final String NETWORK_AVAILABLE_ACTION = "com.gdm.retailalfageek.NetworkAvailable";
public static final String IS_NETWORK_AVAILABLE = "isNetworkAvailable";
#Override
public void onReceive(Context context, Intent intent) {
Intent networkStateIntent = new Intent(NETWORK_AVAILABLE_ACTION);
networkStateIntent.putExtra(IS_NETWORK_AVAILABLE, isConnectedToInternet(context));
LocalBroadcastManager.getInstance(context).sendBroadcast(networkStateIntent);
Log.e("Network Available ", "On receive called");
}
private boolean isConnectedToInternet(Context context) {
try {
if (context != null) {
ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
return networkInfo != null && networkInfo.isConnected();
}
return false;
} catch (Exception e) {
Log.e(NetworkStateChangeReceiver.class.getName(), e.getMessage());
return false;
}
}
}
Main Activity
package com.gdm.internetconnectivitycheck;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.support.v4.content.LocalBroadcastManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.design.widget.Snackbar;
import static com.gdm.internetconnectivitycheck.NetworkStateChangeReceiver.IS_NETWORK_AVAILABLE;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
IntentFilter intentFilter = new IntentFilter(NetworkStateChangeReceiver.NETWORK_AVAILABLE_ACTION);
LocalBroadcastManager.getInstance(this).registerReceiver(new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
boolean isNetworkAvailable = intent.getBooleanExtra(IS_NETWORK_AVAILABLE, false);
String networkStatus = isNetworkAvailable ? "connected" : "disconnected";
Snackbar.make(findViewById(R.id.main_activity), "Network Status: " + networkStatus, Snackbar.LENGTH_LONG).show();
}
}, intentFilter);
}
}
Apps targeting Android 7.0 (API level 24) and higher must register the
following broadcasts with
registerReceiver(BroadcastReceiver,IntentFilter)
Declaring a receiver in the manifest does not work.
CONNECTIVITY_ACTION
Beginning with Android 8.0 (API level 26), the
system imposes additional restrictions on manifest-declared receivers.
If your app targets API level 26 or higher, you cannot use the
manifest to declare a receiver for most implicit broadcasts
(broadcasts that do not target your app specifically). You can still
use a context-registered reciever when the user is actively using your
app.
directly from official doc.
you need to register for CONNECTIVITY_CHANGE action at runtime from activity.
using registerReceiver.
IntentFilter filter = new IntentFilter();
filter.addAction("android.net.conn.CONNECTIVITY_CHANGE");
registerReceiver(new NetworkStateChangeReceiver(), filter);
And don't forget to unregister.

activity does not open on event detection

I am following this answer to implement app un-install detector. I am basically trying to open Main Activity when un-installation event is detected, but when I un-istall my app nothing happens (Main Activity does not open before un-install).
Please help, what is wrong with my code?
MainAcivity:
package com.example.appdeveloper.unistalltest;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
Broadcast Receiver:
package com.example.appdeveloper.unistalltest;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;
public class UninstallIntentReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
// fetching package names from extras
String[] packageNames = intent.getStringArrayExtra("android.intent.extra.PACKAGES");
if(packageNames!=null){
for(String packageName: packageNames){
if(packageName!=null && packageName.equals("com.example.appdeveloper.unistalltest")){
// User has selected our application under the Manage Apps settings
// now initiating background thread to watch for activity
new ListenActivities(context).start();
}
}
}
else {
Toast.makeText(context, "No package found in Broadcast Receiver", Toast.LENGTH_LONG).show();
}
}
}
ListenActivities.java:
package com.example.appdeveloper.unistalltest;
import android.app.ActivityManager;
import android.content.Context;
import android.content.Intent;
import android.os.Looper;
import android.util.Log;
import android.widget.Toast;
import java.util.List;
public class ListenActivities extends Thread {
boolean exit = false;
ActivityManager am = null;
Context context = null;
public ListenActivities(Context con){
context = con;
am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
}
public void run(){
Looper.prepare();
while(!exit){
// get the info from the currently running task
List< ActivityManager.RunningTaskInfo > taskInfo = am.getRunningTasks(MAX_PRIORITY);
String activityName = taskInfo.get(0).topActivity.getClassName();
Log.d("topActivity", "CURRENT Activity ::"
+ activityName);
if (activityName.equals("com.android.packageinstaller.UninstallerActivity")) {
// User has clicked on the Uninstall button under the Manage Apps settings
//do whatever pre-uninstallation task you want to perform here
// show dialogue or start another activity or database operations etc..etc..
context.startActivity(new Intent(context, MainActivity.class).setFlags(Intent.FLAG_ACTIVITY_NEW_TASK));
exit = true;
Toast.makeText(context, "Done with preuninstallation tasks... Exiting Now", Toast.LENGTH_LONG).show();
}
else if(activityName.equals("com.android.settings.ManageApplications")) {
// back button was pressed and the user has been taken back to Manage Applications window
// we should close the activity monitoring now
exit=true;
}
}
Looper.loop();
}
}
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.appdeveloper.unistalltest">
<uses-permission android:name="android.permission.GET_TASKS"/>
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name=".UninstallIntentReceiver">
<intent-filter android:priority="0">
<action android:name="android.intent.action.QUERY_PACKAGE_RESTART" />
<data android:scheme="package" />
</intent-filter>
</receiver>
</application>
</manifest>
Your app can't know when the user uninstall it since Android 4.0+. I had a similar problem, but didn't found a way to do it. The only things I found today (i already gave up with my app, but maybe it can help you) is this: https://developer.android.com/guide/topics/admin/device-admin.html
I'm not sure this will help you, but it is the only thing i found.

Receiving the Broadcast

I want to make a application that gets the incoming calling number if the call is from a specific number then do some stuff . I want to get the incoming calling number even when the application is not running .. I am using the BraodcastReceiver to get the incoming number .
I have two java class one which extends he activity and the other extends the BraodcastReceiver for getting the incoming calling number .
Main class which extends activity :
package digicare.ringmanager;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
public class Main_Activity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_layout);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main_layout, menu);
return true;
}
}
it is pretty simple and the Number checker class which extends the BroadcastReceiver :
package digicare.ringmanager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.media.AudioManager;
import android.os.Bundle;
import android.telephony.TelephonyManager;
import android.util.Log;
public class number_checker extends BroadcastReceiver {
private int ringer_mode ;
private String AM_str;
#Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
AudioManager AM =(AudioManager)context.getSystemService(Context.AUDIO_SERVICE);
Log.w("start", "starting the Main_Activity");
AM_str=String.valueOf(ringer_mode);
Log.w("Ringer_mode at start", AM_str);
//setting the ringer mode on number match
try {
Bundle extras=intent.getExtras();
if (extras !=null){
String state = extras.getString(TelephonyManager.EXTRA_STATE);
Log.w("state at start",state);
if (state.equals(TelephonyManager.EXTRA_STATE_RINGING)){
String phonenumber = extras.getString(TelephonyManager.EXTRA_INCOMING_NUMBER);
//AM.setRingerMode(1);
ringer_mode =AM.getRingerMode();
AM_str=String.valueOf(ringer_mode);
Log.w("Ringer_mode at ringing", AM_str);
Log.w("Number", phonenumber);
if (phonenumber.equals("1234")){
Log.w("yahoo", "Number matched");
if (ringer_mode==AudioManager.RINGER_MODE_SILENT || ringer_mode==AudioManager.RINGER_MODE_VIBRATE ){
AM.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
Log.w("Phone number is matched .!", "Now , ringer mode is normal");
int now_nor =AM.getRingerMode();
String now_nor_str=String.valueOf(now_nor);
Log.w("ring_mode at num matched",now_nor_str);
}
}
}
if (state.equals(TelephonyManager.EXTRA_STATE_OFFHOOK ) || state.equals(TelephonyManager.EXTRA_STATE_IDLE)){
int now_nor =AM.getRingerMode();
String now_nor_str=String.valueOf(now_nor);
Log.w("ring_mode at offHock",now_nor_str);
if (ringer_mode==AudioManager.RINGER_MODE_NORMAL){
AM.setRingerMode(AudioManager.RINGER_MODE_NORMAL );
Log.w("Normal", "");
}else if (ringer_mode==AudioManager.RINGER_MODE_SILENT ){
AM.setRingerMode(AudioManager.RINGER_MODE_SILENT );
Log.w("silent", "");
}else if (ringer_mode==AudioManager.RINGER_MODE_VIBRATE ){
AM.setRingerMode(AudioManager.RINGER_MODE_VIBRATE);
Log.w("vibrat", "");
}
// Log.w("Again", "Now the Ringer mode is get back ");
int now =AM.getRingerMode();
String now_str=String.valueOf(now);
Log.w("ring_mode at end ",now_str);
}
}
} catch (Exception e) {
// TODO: handle exception
Log.w("MY_DEBUG_TAG", e);
}
}
}
And the AndroidManifist.xml is this :
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="digicare.ringmanager"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="5"
android:targetSdkVersion="17" />
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
<uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS"/>
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="digicare.ringmanager.Main_Activity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name="number_checker" >
<intent-filter>
<action android:name="android.intent.action.PHONE_STATE"/>
</intent-filter>
</receiver>
</application>
</manifest>
but this not working this cannot get the incoming calling number .
wot could i do ???? have to call the number_checker class for register the Braodcast ???
please help i am a new android developer
You can either dynamically register an instance of this class with Context.registerReceiver() or statically publish an implementation through the tag in your AndroidManifest.xml.
Note: If registering a receiver in your Activity.onResume() implementation, you should unregister it in Activity.onPause(). (You won't receive intents when paused, and this will cut down on unnecessary system overhead). Do not unregister in Activity.onSaveInstanceState(), because this won't be called if the user moves back in the history stack.
also check this link :
http://www.vogella.com/articles/AndroidBroadcastReceiver/article.html
NEW Update:
check this link http://androidexample.com/Incomming_Phone_Call_Broadcast_Receiver__-_Android_Example/index.php?view=article_discription&aid=61&aaid=86
Update 2
put . before the receiver name. like this <receiver android:name=".number_checker" >

Is there a way on Android to intercept incoming calls/SMSes to either block/unblock it?

Is there a way to intercept incomming calls/SMSes (to block or unblock it) on the basis of mobile numbers which are added to screening list?
----> For your question, I think the following will be helpful.
package android_programmers_guide.PhoneCallReceiver;
import java.lang.reflect.Method;
import com.android.internal.telephony.ITelephony;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.telephony.TelephonyManager;
import android.util.Log;
import android.widget.Toast;
public class PhoneCallReceiver extends BroadcastReceiver
{
Context context = null;
private static final String TAG = "THIRI THE WUT YEE";
private ITelephony telephonyService;
#Override
public void onReceive(Context context, Intent intent)
{
Log.v(TAG, "Receving....");
TelephonyManager telephony = (TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE);
try
{
Class c = Class.forName(telephony.getClass().getName());
Method m = c.getDeclaredMethod("getITelephony");
m.setAccessible(true);
telephonyService = (ITelephony) m.invoke(telephony);
Toast tag = Toast.makeText(context, "Call is not allowed in the meeting!!!", Toast.LENGTH_LONG);
tag.setDuration(25);
tag.show();
telephonyService.silenceRinger();
telephonyService.endCall();
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
-----> Here is interface class
package com.android.internal.telephony;
interface ITelephony
{
boolean endCall();
void answerRingingCall();
void silenceRinger();
}
----> And the Manifest is as follows
<application android:icon="#drawable/icon" android:label="#string/app_name">
<activity android:name=".PhoneCall2"
android:label="#string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name=".PhoneCallReceiver">
<intent-filter android:priority="100" >
<action android:name="android.intent.action.PHONE_STATE" />
</intent-filter>
</receiver>
</application>
<uses-sdk android:minSdkVersion="3" />
<uses-permission android:name="android.permission.MODIFY_PHONE_STATE" />
<uses-permission android:name="android.permission.CALL_PHONE" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
</manifest>
---> Honestly, I've found this code on the internet
Cheers!
As far as I know the application for receiving calls cannot be modified.
However, see How to block calls in android for some creative suggestions.
There seems to be a walkaround for deleting SMS see
How to delete an SMS from the inbox in Android programmatically?.
But it is highly questionable for an application to do this, as the sideeffects could be severe. Make sure your users understand this!
The best solution IMHO would be to have a separate build of android which supported these child-safety features (I assume that is what you want to use it for).
Well, if you are looking for an app, there's certainly a number of call blocking apps in the Android market. I'd recommend the following free app that does this and a lot more things:
https://play.google.com/store/apps/details?id=cloud4apps.cBlocker

Categories

Resources