Android Handle phone call - android

I have audio recording, when a phone call come I need to stop the recording, how can I do this?

You have to use the PhoneStateListener:
TelephonyManager tm = (TelephonyManager)getSystemService(TELEPHONY_SERVICE);
tm.listen(mPhoneListener, PhoneStateListener.LISTEN_CALL_STATE);
// somewhere else
private PhoneStateListener mPhoneListener = new PhoneStateListener() {
public void onCallStateChanged(int state, String incomingNumber) {
try {
switch (state) {
case TelephonyManager.CALL_STATE_RINGING:
// do something...
break;
case TelephonyManager.CALL_STATE_OFFHOOK:
// do something...
break;
case TelephonyManager.CALL_STATE_IDLE:
// do something...
break;
default:
Log.d(TAG, "Unknown phone state=" + state);
}
} catch (RemoteException e) {}
}
};
Make sure to include this permission in your Manifest:
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>

I've never tried to access the phone stuff on Android but check here -
http://developer.android.com/reference/android/telephony/package-summary.html
and here -
http://developer.android.com/reference/android/telephony/PhoneStateListener.html
and here -
http://developer.android.com/reference/android/telephony/TelephonyManager.html

Related

Can I get the number that is calling by native dialer?

Can I get the number that is calling by native dialer ?
I want to show the "toast message" with information about cost per minute according to a dialing number. But I do not know how to get this number.
It could be good to have sample which show "toast message" with dialed number during of call.
yes you can do it.You need to implement PhoneStateListener
public class CustomPhoneStateListener extends PhoneStateListener {
public static Boolean phoneRinging = false;
public void onCallStateChanged(int state, String incomingNumber) {
switch (state) {
case TelephonyManager.CALL_STATE_IDLE:
Log.d("DEBUG", "IDLE");
phoneRinging = false;
break;
case TelephonyManager.CALL_STATE_OFFHOOK:
Log.d("DEBUG", "OFFHOOK");
phoneRinging = false;
break;
case TelephonyManager.CALL_STATE_RINGING:
Log.d("DEBUG", "RINGING");
phoneRinging = true;
break;
}
}
}
To register the listener do this:
CustomPhoneStateListener phoneListener = new CustomPhoneStateListener();
telephony = (TelephonyManager) context
.getSystemService(Context.TELEPHONY_SERVICE);
telephony.listen(phoneListener, PhoneStateListener.LISTEN_CALL_STATE);
Note that access to some telephony information is permission-protected. Your application won't receive updates for protected information unless it has the appropriate permissions declared in its manifest file. Where permissions apply, they are noted in the appropriate LISTEN_ flags.

Android automatic call and cut after connect

In my android application, I wanna call a person from the application and drop the call if the call get connected successful ( after 2 rings or so ). I can call to the number via this but is there any way to drop the call and get the event after 2 rings or a single ring ?
My calling code looks like
String posted_by = "1234567890";
String uri = "tel:" + posted_by.trim() ;
Intent intent = new Intent(Intent.ACTION_CALL);
intent.setData(Uri.parse(uri));
startActivity(intent);
I am new to android
Please help, Thanks in advance
You can make use of the following in a receiver:
/** The listener. */
private final PhoneStateListener listener = new PhoneStateListener() {
#Override
public void onCallStateChanged(int state, String incomingNumber) {
switch (state) {
case TelephonyManager.CALL_STATE_IDLE:
Log.d("TAG", "IDLE");
if (incomingNumber.equalsIgnoreCase("phoneNumber")) {
if (audiomanager.getRingerMode() == AudioManager.RINGER_MODE_NORMAL) {
audiomanager.setRingerMode(AudioManager.RINGER_MODE_VIBRATE);
} else if (audiomanager.getRingerMode() == AudioManager.RINGER_MODE_VIBRATE) {
audiomanager.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
}
}
break;
case TelephonyManager.CALL_STATE_OFFHOOK:
Log.d("TAG", "OFFHOOK");
audiomanager.setRingerMode(AudioManager.RINGER_MODE_VIBRATE);
break;
case TelephonyManager.CALL_STATE_RINGING:
Log.d("TAG", "RINGING====>" + incomingNumber);
if (incomingNumber.equalsIgnoreCase("phoneNumber")) {
switch (audiomanager.getRingerMode()) {
case AudioManager.RINGER_MODE_VIBRATE:
audiomanager.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
audiomanager.setStreamVolume(AudioManager.STREAM_RING,volRing,0);
break;
case AudioManager.RINGER_MODE_SILENT:
audiomanager.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
audiomanager.setStreamVolume(AudioManager.STREAM_RING,volRing,0);
break;
}
}
break;
}
};
There's no way of doing this in Android. However you could use TelephonyManager and PhoneStateListener to check the phone states. When you got this information you should be able to implement the functionality you need.This post should help you: Android READ PHONE STATE?.
Good luck!

Pausing a service in background in android

I am working on an app in which I am playing media player using service. The app is working fine but when i receive a call or a message, the music keeps on playing. My requirement is when I get a call, the music should be paused and on call disconnection, it should resume from where it has stopped. How can I achieve this?
I am not sure how you coded your app but this should help you.
Check out this How to know the moment when the called person picks up his phone You would just need to pause the music in the switch statement for the specified event
TelephonyManager tm = (TelephonyManager)getSystemService(TELEPHONY_SERVICE);
tm.listen(mPhoneListener, PhoneStateListener.LISTEN_CALL_STATE);
private PhoneStateListener mPhoneListener = new PhoneStateListener() {
public void onCallStateChanged(int state, String incomingNumber) {
try {
switch (state) {
case TelephonyManager.CALL_STATE_RINGING:
// do something...
break;
case TelephonyManager.CALL_STATE_OFFHOOK:
// this state is the one you're looking for
break;
case TelephonyManager.CALL_STATE_IDLE:
// do something...
break;
default:
Log.d(TAG, "Unknown phone state=" + state);
}
} catch (RemoteException e) {}
}
};
EDIT: here is another link Detect if an outgoing call has been answered

Mode of ringing changes from vibration to ringing when Incoming call is from particular number

I want to create an android application that changes the mode to ringing from vibration when incoming call from particular number
For Changing ringing from vibration on incoming call use Use TelephonyManager,AudioManager and PhoneStateListener as:
TelephonyManager mTelephonyMgr = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
mTelephonyMgr.listen(new TeleListener(), PhoneStateListener.LISTEN_CALL_STATE);
class TeleListener extends PhoneStateListener
{
public void onCallStateChanged(int state, String incomingNumber)
{
super.onCallStateChanged(state, incomingNumber);
switch (state)
{
case TelephonyManager.CALL_STATE_IDLE:
//CALL_STATE_IDLE;
break;
case TelephonyManager.CALL_STATE_OFFHOOK:
//CALL_STATE_OFFHOOK;
break;
case TelephonyManager.CALL_STATE_RINGING:
//CALL_STATE_RINGING
//CHECK YOUR PARTICULAR NUMBER HERE
if(incomingNumber=="1234567890")
{
// USE AudioManager for Settingringing from vibration
AudioManager am = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
switch (am.getRingerMode()) {
case AudioManager.RINGER_MODE_NORMAL:
Log.i("MyApp","NORMAL mode");
am.setRingerMode(AudioManager.RINGER_MODE_VIBRATE);
break;
}
}
else
{
//DO SOMETHING HERE
}
break;
default:
break;
}
}
}
add <uses-permission android:name="android.permission.READ_PHONE_STATE"> permission in manifest.xml
or How we Get Phone State using BroadcastReceiver see this tutorial:
Get Phone State When Someone is calling using BroadcastReceiver Example

Understand when user is speaking at the phone

(Android) Some days ago I saw a way to register a broadcast receiver to understand when the screen goes black or the inverse.
Now i'd like to find a way to understand when the users is speaking at the phone but i don't find a broadcast for this case, so how can i understand if the user is at the phone? (Possibly without always running a service that would discharge the battery).
Tnk's
Valerio
I'm not sure about "speaking" at the phone, since there is no API to detect that, but you can listen to the phone state, like:
TelephonyManager telephonyManager = (TelephonyManager)
getSystemService(Context.TELEPHONY_SERVICE);
PhoneStateListener listener = new PhoneStateListener() {
#Override
public void onCallStateChanged(int state, String incomingNumber) {
String stateString = "N/A";
switch (state) {
case TelephonyManager.CALL_STATE_IDLE:
stateString = "Idle";
break;
case TelephonyManager.CALL_STATE_OFFHOOK:
stateString = "Off Hook";
break;
case TelephonyManager.CALL_STATE_RINGING:
stateString = "Ringing";
break;
}
}
};
telephonyManager.listen(listener, PhoneStateListener.LISTEN_CALL_STATE);
Don't forget the following permission in the manifest:
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>

Categories

Resources