Is it possible to retrieve the call forwarding state on Android?
Using this code only shows a popup with the current state but I was wondering if it's possible to get the actual number that is set if any
Intent intentCallForward = new Intent(Intent.ACTION_CALL);
Uri uri = Uri.fromParts("tel", "*#21#", "#");
intentCallForward.setData(uri);
startActivity(intentCallForward);
It's simple. All you need is a BroadCast to be tracking your phone state. Try something like this:
public class CallBroadcastReceiver extends BroadcastReceiver
{
public static String numberToCall;
public void onReceive(Context context, Intent intent) {
Log.d("CallForwardingBroadCast", "CallBroadcastReceiver::onReceive got Intent: " + intent.toString());
if (intent.getAction().equals(Intent.ACTION_NEW_OUTGOING_CALL)) {
numberToCall = intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER);
Log.d("CallRecorder", "CallBroadcastReceiver intent has EXTRA_PHONE_NUMBER: " + numberToCall);
}
}
}
Related
I am getting Incoming call Details(Number,Name,Date). But How to get outgoing call details. I have written a code for outgoing calls details but it throws NullPointerException. Below is my MyCallReceiver.java file and manifest file
public void onReceive(Context context, Intent intent) {
this.context = context;
if (intent.getStringExtra(TelephonyManager.EXTRA_STATE).equals(TelephonyManager.EXTRA_STATE_RINGING)) {
String incomingNumber = intent.getStringExtra(TelephonyManager.EXTRA_INCOMING_NUMBER);
Toast.makeText(context, "Call From : " + incomingNumber, Toast.LENGTH_LONG).show();
doToast(getContactName(context, incomingNumber) + " " + incomingNumber);
String currentDateTimeString = DateFormat.getDateTimeInstance().format(new Date());
doToast(currentDateTimeString +" "+incomingNumber);
} else if (intent.getStringExtra(TelephonyManager.EXTRA_STATE).equals(TelephonyManager.EXTRA_STATE_IDLE) || intent.getStringExtra(TelephonyManager.EXTRA_STATE).equals(TelephonyManager.EXTRA_STATE_OFFHOOK)) {
Toast.makeText(context, "DETECTED CALL HANG UP EVENT", Toast.LENGTH_LONG).show();
String outgoingNumber=intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER);
Toast.makeText(context,"Calling To :"+outgoingNumber,Toast.LENGTH_LONG).show();
}
}
public void onReceive(Context context, Intent intent)
{
String state=intent.getStringExtra(TelephonyManager.EXTRA_STATE);
if(state==null)
{
//Outgoing call
String number=intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER);
Log.i("tag","Outgoing number : "+number);
}
else if (state.equals(TelephonyManager.EXTRA_STATE_RINGING))
{
//Incoming call
String number = intent.getStringExtra(TelephonyManager.EXTRA_INCOMING_NUMBER);
Log.i("tag","Incoming number : "+number);
}
}
First of all intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER) gives you outgoing number while phone state is idle and turns to "null" while phone status changes to OFF_HOOK.
The easiest way was to save the number before another onRecive happens.
Creating separate listeners for different events (ie. incoming vs outgoing calls) might make your life easier. For outgoing calls, instead checking for state of TelephonyManager, you might want to create an IntentFilter.
IntentFilter filterCalls = new IntentFilter();
filterCalls.addAction(Intent.ACTION_NEW_OUTGOING_CALL);
MyCallReceiver myCallReceiver = new MyCallReceiver();
registerReceiver(myCallReceiver, filterCalls);
Then in your onReceive you can simply have:
String outgoingNumber = intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER)
and be sure that it will work.
I am starting the service , and I put a message in extra,
Intent mServiceIntent = new Intent(this, TextToSpeechService.class);
mServiceIntent.putExtra(Intent.EXTRA_TEXT, "a message");
mServiceIntent.setType(HTTP.PLAIN_TEXT_TYPE);
this.startService(mServiceIntent);
but running , the service starts and the log shows a message = null...
public class MyService extends IntentService {
static final String TAG = "MyService";
public MyService() {
super("My Service");
}
#Override
protected void onHandleIntent(Intent intent) {
String message = intent.getStringExtra("message");
Log.d(TAG, "received message, should say: " + message);
}
could it be related to the MIME TYPE, when I state mServiceIntent.setType(HTTP.PLAIN_TEXT_TYPE);
( using import org.apache.http.protocol.HTTP;)
As #Squonk already mentioned your code doesn't really go together. To start your Service you have to use an Intent like this:
// The class you set here determines where the Intent will go.
// You want it to start MyService so we write MyService.class here.
Intent intent = new Intent(this, MyService.class);
intent.putExtra(Intent.EXTRA_TEXT, "a message");
startService(intent);
You can very well use the Intent.EXTRA_TEXT constant as key for your extra, but you have to use the same key to retrieve the message in your Service:
#Override
protected void onHandleIntent(Intent intent) {
String message = intent.getStringExtra(Intent.EXTRA_TEXT);
Log.d(TAG, "received message, should say: " + message);
}
The code in your MyService doesn't really show if you actually use the mime type in your MyService so I removed it from my example above.
I want to read the phone setting to see if forwarding is enabled or not. And if it is
enabled then I want to get the number that calls are forwarded to. I DO NOT want to use
"PhoneStateListener" because the forwarding can be activated before my app installation.
What is the solution?
You can make a broadcast call and register it and try to get the outgoing call as shown below :
public class CallBroadcastReceiver extends BroadcastReceiver
{
public static String numberToCall;
public void onReceive(Context context, Intent intent) {
Log.d("CallRecorder", "CallBroadcastReceiver::onReceive got Intent: " + intent.toString());
if (intent.getAction().equals(Intent.ACTION_NEW_OUTGOING_CALL)) {
numberToCall = intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER);
Log.d("CallRecorder", "CallBroadcastReceiver intent has EXTRA_PHONE_NUMBER: " + numberToCall);
}
}
}
How to pass a integer value from Service class using a Broadcast intent to a Activity. Below is my code but getting some errors,
Sending side
private void DisplayLoggingInfo() {
Log.d(TAG, "entered DisplayLoggingInfo");
int dataJNI = BioLIb.intFromJNI();
intent.putExtra("mykey", dataJNI);
sendBroadcast(intent);
}
Receiving side
private Intent intent;
the above is a global variable. In my onCreate I will call BroadcastReceiver class like below.
intent = new Intent(this, BroadCastService.class);
onReceive of BroadcastReceiver i will call
public void onReceive(Context context, Intent intent) {
updateUI(intent);
}
int time = intent.getINtExtra("mykey", 1);
But I am not getting my exact value of dataJNI in a receiving side. I am always getting value is 1, How to get resolve this.
When you're sending you're putting an integer into the Intent Bundle. When receiving you0re trying to extract a long. Did you try using the same type in both sides?
use the intent that you received as a parameter of onReceive method
int time;
public void onReceive(Context context, Intent intent) {
updateUI(intent);
time = intent.getLongExtra("mykey", 1);
}
Im developing an application,which blocks all outgoing calls and then after blocking that call ,another new call is initiated to a predefined number...
My problem is that,when i block the call using a broadcastreceiver,the second call which i programmatically initiate is also getting blocked...
Is any method to unregister the broadcast after blocking the first call,or any other method or technique???
This is my broadcastreceiver which i implemented for my app...
public class CallListenerActivity extends BroadcastReceiver {
Uri uri;
#Override
public void onReceive(Context context, Intent intent) {
Bundle bundle = intent.getExtras();
if(bundle == null)
return;
String phonenumber = intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER);
Log.i("###OutgoingCallReceiver",phonenumber);
Log.i("###OutgoingCallReceiver",bundle.toString());
String info = "Detect Calls sample application\nOutgoing number: " + phonenumber;
Toast.makeText(context, info, Toast.LENGTH_LONG).show();
String phoneNumber = "5556";
Uri uri = Uri.fromParts("tel", phoneNumber, null);
Intent callIntent = new Intent(Intent.ACTION_CALL, uri);
callIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(callIntent);
}
You could try starting your original (CallListenerActivity? The one which registered the broadcast receiver) activity again using a flag stored as extra in the intent.
Evaluate the intent in your activity and unregister the broadcast receiver if you see the flag in the extras. Then start the the call activity as shown in your example code.
You can use store a check when you are initiating a call and after the completion of the call mark it. For this this check you can use SharedPreference.