How to get more information on incoming call - java

I have set up my virtual phone numbers to forward calls on my cell phone. When the call is forwarded, I am receiving the incoming number, but not what number they dialed us to reach.
Is it possible to get the number they dialed us to reach?
public class MyPhoneStateListener extends PhoneStateListener {
private String[] projection = new String[] {
People._ID, People.NAME, People.NUMBER
};
public void onCallStateChanged(int state,String incomingNumber){
switch(state)
{
case TelephonyManager.CALL_STATE_IDLE:
Log.d("DEBUG", "IDLE");
break;
case TelephonyManager.CALL_STATE_OFFHOOK:
if(!incomingNumber.equals("")){
handleCall(incomingCall);
}
break;
case TelephonyManager.CALL_STATE_RINGING:
Log.d("DEBUG", "RINGING");
break;
}
}

From what I have learnt, it is only possible to get the incoming number.

Related

Possible to use TelephonyManager without permission READ_PHONE_STATE

I made a music player app and for pausing the song when the phone gets called i use this code.
But this needs the permission READ_PHONE_STATE which maybe scares off some users, so i would like to know if it's possible to achieve the same thing another way without asking for this permission?
My code
private void callStateListener(){
//incomingCallPause: checkbox value if user wants to pause when there is an incoming call.
incomingCallPause = storageUtil.loadSwitchOnCall();
mTelephonyManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
mPhoneStateListener = new PhoneStateListener(){
#Override
public void onCallStateChanged(int state, String incomingNumber) {
switch (state) {
case TelephonyManager.CALL_STATE_OFFHOOK:
if (mediaPlayer != null){
pauseSong();
}
break;
case TelephonyManager.CALL_STATE_RINGING:
if (incomingCallPause && mediaPlayer != null) {
pauseSong();
NotificationBuilder(PlaybackStatus.PAUSED);
incomingCall = true;
}
break;
case TelephonyManager.CALL_STATE_IDLE:
if (mediaPlayer != null) {
if (incomingCall) {
incomingCall = false;
NotificationBuilder(PlaybackStatus.PLAYING);
if (!mediaPlayer.isPlaying()){
mediaPlayer.start();
}
}
}
break;
}
super.onCallStateChanged(state, incomingNumber);
}
};
As per Android official document you need to define manifest permission to use TelephonyManager.
Requires Manifest.permission.READ_PHONE_STATE
Referral link:
https://developer.android.com/reference/android/telephony/TelephonyManager

How count missed call or reject call in android studio

Here I make a call from my app and I want to count missed or reject call
and I use a counter (counterNoiseCalling) to count missed or reject call but in node in Firebase counter still always equal 0 and I don't know why.
private class MyPhoneListener extends PhoneStateListener {
private boolean onCall = false;
String userNameCalling = UserDetails.username;
int counterNoisingCall = 0;
#Override
public void onCallStateChanged(int state, String incomingNumber) {
switch (state) {
case TelephonyManager.CALL_STATE_RINGING:
// phone ringing...
Toast.makeText(Call.this, incomingNumber + " calls you", Toast.LENGTH_LONG).show();
break;
case TelephonyManager.CALL_STATE_OFFHOOK:
// one call exists that is dialing, active, or on hold
Toast.makeText(Call.this, "on call...", Toast.LENGTH_LONG).show();
if(!onCall) {
//because user answer the incoming call
Toast.makeText(Call.this, "The call is being answered", Toast.LENGTH_SHORT).show();
}else {
// reject call or missed call
Toast.makeText(Call.this, "Number Busy Or No Reply", Toast.LENGTH_SHORT).show();
counterNoisingCall++;
}
break;
case TelephonyManager.CALL_STATE_IDLE:
// in initialization of the class and at the end of phone call
// detect flag from CALL_STATE_OFFHOOK
if (onCall == true) {
Toast.makeText(Call.this, "restart app after call to Users List", Toast.LENGTH_LONG).show();
// restart our application to return to Our Contact
Intent restart = new Intent(Call.this , Users.class);
restart.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(restart);
onCall = false;
}
break;
default:
break;
}
Firebase childRef = mRootRef.child(userNameCalling);
childRef.setValue(counterNoisingCall);
}
}
If you can debug and see that counterNoisingCall variable is not 0 then the problem seem to be related to Firebase.
I guess that may be is related to security rules. Maybe the user does not have permission write on the Firebase node.
I would put a complete listener on setValue to see that the command completed with success.
Try this.
Firebase childRef = mRootRef.child(userNameCalling);
childRef.setValue(counterNoisingCall).addOnCompleteListener(new OnCompleteListener<Void>() {
#Override
public void onComplete(#NonNull Task<Void> task) {
if (!task.isSuccessful()) {
Log.d("",task.getException().getMessage());
}
}
});

How do I switch layout when connected to a Bluetooth device in a Handler?

So I would like to change the layout once i'm successfully connected to a Bluetooth device. I would like to do this in my Handler, but I don't know how. As you can see in my Handler code below , I've tried this in my case BTHandler.STATE_CONNECTED: (this is just copied from BluetoothChat) but I dont know how.
private Handler mHandler = new Handler() {
#Override
public void handleMessage(Message msg) {
switch (msg.what) {
case Constants.MESSAGE_STATE_CHANGE:
switch (msg.arg1) {
case BTHandler.STATE_CONNECTED:
setStatus(getString(R.string.title_connected_to, mConnectedDeviceName));
mConversationArrayAdapter.clear();
break;
case BTHandler.STATE_CONNECTING:
Toast.makeText(getApplicationContext(), "Connecting…", Toast.LENGTH_SHORT).show();
Log.v("Log", "connecting");
break;
case BTHandler.STATE_LISTEN:
case BTHandler.STATE_NONE:
Toast.makeText(getApplicationContext(), "Connected", Toast.LENGTH_LONG).show();
Log.v("Log", "connected");
break;
}
}
}
};
Solved the problem by myself.
guiHandler(Constants.CONNECTION_STATUS, Constants.STATE_CONNECTED, "");
This is written in my run method that's inside a ConnectThread.

Do a command when State Call is ringing

I have a Little app to make in Android.
So I have few Button and each button call a specific intermediate number first.
After this first call I have to wait ~15seconds and the app have to call the true number.
for example, I click on a button named "toto" and is telephone number is 001 so the app first make a call to the intermediate number which is for example 000 and there will be a voice saying that he has to wait few seconds before reach toto.
For now I have this :
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TelephonyManager telephoneM = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
PhoneStateListener listner = new PhoneStateListener() {
public void onCallStateChanged(int state, String incomingnumber) {
String etat = "N/A";
switch (state) {
case TelephonyManager.CALL_STATE_IDLE:
etat = "pas de reponse";
Toast.makeText(MainActivity.this, "" + etat,
Toast.LENGTH_SHORT).show();
break;
case TelephonyManager.CALL_STATE_RINGING:
etat = "sonne";
Toast.makeText(MainActivity.this, "" + etat,
Toast.LENGTH_SHORT).show();
break;
case TelephonyManager.CALL_STATE_OFFHOOK:
etat = "en ligne";
Toast.makeText(MainActivity.this, "" + etat,
Toast.LENGTH_SHORT).show();
break;
}
}
};
telephoneM.listen(listner, PhoneStateListener.LISTEN_CALL_STATE);
Btoto = (Button) findViewById(R.id.num1);
Btoto.setOnClickListener(this);
public void onClick(View v) {
switch (v.getId()) {
case R.id.num1:
Intent localIntent = new Intent("android.intent.action.CALL");
localIntent.setData(Uri.parse("tel:0153204255")); // the intermediate number
startActivity(localIntent);
//here I have to make a delay after the first ringing
// And here i have to make the real call to toto
break;
Can someone give me some advice please ?
Thank you

Android application getting killed because of incoming call

In my application I am copying all the Call Logs into a Database in an Async Task. But If a call comes in between my application can get terminated leading to incomplete data. So first of all I want to stop my application from getting terminated in such scenario, so what should I do? Also I want to keep track of cursor such that when application is opened again it will continue from the exact same place where it stopped and complete the job.
This is the code that helped me save my game state during incoming call.. Hope it helps you too...
TelephonyManager tm;
private PhoneStateListener mPhoneListener = new PhoneStateListener() {
public void onCallStateChanged(int state, String incomingNumber) {
try {
switch (state) {
case TelephonyManager.CALL_STATE_RINGING:
Toast.makeText(GameActivity.this, "CALL_STATE_RINGING", Toast.LENGTH_SHORT).show();
//Your function to save state right here...
stopTimer();
break;
case TelephonyManager.CALL_STATE_OFFHOOK:
Toast.makeText(GameActivity.this, "CALL_STATE_OFFHOOK", Toast.LENGTH_SHORT).show();
break;
case TelephonyManager.CALL_STATE_IDLE:
Toast.makeText(GameActivity.this, "CALL_STATE_IDLE", Toast.LENGTH_SHORT).show();
break;
default:
Toast.makeText(GameActivity.this, "default", Toast.LENGTH_SHORT).show();
Log.i("Default", "Unknown phone state=" + state);
}
} catch (Exception e) {
Log.i("Exception", "PhoneStateListener() e = " + e);
}
}
};

Categories