Pausing a service in background in android - 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

Related

Which AudioFocus do I need to set for an alarm application

From this blog I learned that, for all media applications, I should request AudioFocus and start the playback only if the AudioFocus is granted.
am.requestAudioFocus(audioFocusChangeListener,AudioManager.STREAM_ALARM,AudioManager.AUDIOFOCUS_GAIN);
I am making an alarm application.
As an alarm, it should always ring until dismissed - except during phone calls (it is logical).
I can track if a call is ringing or idle by using the TelephonyManager
TelephonyManager telephonyManager =
(TelephonyManager) this.getSystemService(Context.TELEPHONY_SERVICE);
telephonyManager.listen(phoneStateListener, PhoneStateListener.LISTEN_CALL_STATE);
switch (telephonyManager.getCallState()) {
case TelephonyManager.CALL_STATE_IDLE:
outsidePause = false;
break;
default:
outsidePause = true;
break;
}
PhoneStateListener phoneStateListener = new PhoneStateListener() {
#Override public void onCallStateChanged(int state, String incomingNumber) {
switch (state) {
case TelephonyManager.CALL_STATE_IDLE:
outsidePause = false;
Settings.System.putInt(getContentResolver(), Settings.System.SCREEN_BRIGHTNESS_MODE,
Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL);
android.provider.Settings.System.putInt(getContentResolver(),
android.provider.Settings.System.SCREEN_BRIGHTNESS, (0));
am.setStreamVolume(AudioManager.STREAM_ALARM, 0, 0);
setInnerData();
startAlarm();
break;
default:
outsidePause = true;
vibrator.cancel();
pauseAlarm();
break;
}
super.onCallStateChanged(state, incomingNumber);
}
};
Taking in account the TelephonyManager, is it useful and nessesary to use the AudioFocus (if it is, which mode is preferrable)?
Can another application take it from mine, making silencing the alarm?
Seems, that using Audio focus if ok. Othet application does not take it, untill I abandone it.
Hey guys you can use below code in your onAudioFocusChange() method to Manage your Audio Focus when Alarm or Music Player or WhatsApp video call or Phone call comes in all these scenario it will work
// (Below code) Mandatory method for onAudioFocusChange
#Override
public void onAudioFocusChange(int focusChange) {
switch(focusChange)
{
case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT:
// Whenever Alarm is started command comes to this place
notificationView.setImageViewResource(R.id.remoteViewImageView, playbutton);
notificationManager.notify(1,notification);
playButtonImage.setImageResource(playbutton);
say = false;
mediaPlayer.pause();
break;
case AudioManager.AUDIOFOCUS_GAIN :
if(mediaPlayer.isPlaying())
{
notificationView.setImageViewResource(R.id.remoteViewImageView, pause);
notificationManager.notify(1,notification);
playButtonImage.setImageResource(pause);
say = false;
mediaPlayer.start();
}
break;
case AudioManager.AUDIOFOCUS_LOSS:
if(mediaPlayer.isPlaying())
{
notificationView.setImageViewResource(R.id.remoteViewImageView, playbutton);
notificationManager.notify(1,notification);
playButtonImage.setImageResource(playbutton);
mediaPlayer.pause();
say = false;
audioManager.abandonAudioFocus(this);
}
break;
}
}
}

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!

track the incoming call action

I know this is a asked question. By using broadcast receiver and using
android.intent.action.PHONE_STATE
in receiver tag, you can know the actions of a phone. But how can I identify whether it is an incoming call or a outgoing call?
here is my code
#Override
public void onReceive(Context context, Intent intent)
{
this.context = context ;
System.out.println(":::called onReceiver:::");
TelephonyManager telephony = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
telephony.listen(phoneCallListener, PhoneStateListener.LISTEN_CALL_STATE);
}
private final PhoneStateListener phoneCallListener = new PhoneStateListener()
{
#Override
public void onCallStateChanged(int state, String incomingNumber)
{
switch(state)
{
case TelephonyManager.CALL_STATE_RINGING:
isRinging = true;
break;
case TelephonyManager.CALL_STATE_OFFHOOK:
if (isRinging)
{
hasAttended = true ;
isRinging = false;
}
break;
case TelephonyManager.CALL_STATE_IDLE:
if (hasAttended)
{
isCallEnded = true ;
hasAttended = false;
}
break;
}
if(isCallEnded)
{
isCallEnded=false;
Intent callIntent = new Intent(context.getApplicationContext(),MyActivity.class);
callIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(callIntent);
}
super.onCallStateChanged(state, incomingNumber);
}
};
here each time a phoneCallListener object is created and increases the calling rate of onCallStateChanged by one each time..
This is a handy tutorial that uses both broadcast receiver and using android.intent.action.PHONE_STATE that creates different toasts to appear depending on the calls state. When you get this working you will be able to manipulate the code to do what you want when a call is either incoming or outgoing
First your class must extend BroadcastReceiver
Next you have to create a method that will listen to the phones state... PhoneStateListener which will listen to when the Phone state changes
Then do a simple switch case to catch the call depending on if it is an incoming call or an outgoing call
EDIT
All you need to do is write some code to check if the previous state was 'ringing'. If the current state is idle and the previous state was ringing, they cancelled/missed the call. If the current state is offhook and the previous state was ringing, they answered the call.
switch(state){
case TelephonyManager.CALL_STATE_RINGING:
Log.i(LOG_TAG, "RINGING");
wasRinging = true;
break;
case TelephonyManager.CALL_STATE_OFFHOOK:
Log.i(LOG_TAG, "OFFHOOK");
if (!wasRinging) {
// Start your new activity
} else {
// Cancel your old activity
}
// this should be the last piece of code before the break
wasRinging = true;
break;
case TelephonyManager.CALL_STATE_IDLE:
Log.i(LOG_TAG, "IDLE");
// this should be the last piece of code before the break
wasRinging = true;
break;
}

Android: how can I launch phone call in my activity, and then back to it?

I can launch the phone call by Intent:
String url = "tel:3334444";
Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse(url));
But it will stay in the phone call screen. What I want is staying at my app activity. Is it possible that launching the phone call in background? Or return to the previous activity immediately.
You need to implement Phonecall state in side of your activity
// //Handling phone states
private PhoneStateListener phoneListener = new PhoneStateListener() {
public void onCallStateChanged(int state, String incomingNumber) {
try {
switch (state) {
case TelephonyManager.CALL_STATE_RINGING:
Toast.makeText(activity.this, "CALL_STATE_RINGING", Toast.LENGTH_SHORT).show();
mediaPlayer.pause();
break;
case TelephonyManager.CALL_STATE_OFFHOOK:
Toast.makeText(activity.this, "CALL_STATE_OFFHOOK", Toast.LENGTH_SHORT).show();
mediaPlayer.pause();
break;
case TelephonyManager.CALL_STATE_IDLE:
Toast.makeText(activity.this, "CALL_STATE_IDLE", Toast.LENGTH_SHORT).show();
mediaPlayer.start();
break;
default:
Toast.makeText(activity.this, "default", Toast.LENGTH_SHORT).show();
}
} catch (Exception e) {
}
}
};
}
In a background thread (probably needs to be in a foreground service) or on a regular (quickly) repeating alarm poll ActivityManager.getRunningTasks(). The first task is the top-most. Check the topActivity on this task to see if it is the InCallScreen (note on some Ericsson phones this is replaced with a custom class). If it is, bring your activity to the front.
You'll need to use TelephonyManager to watch for on-hook to stop your background thread or alarm if the call is abandoned.

how to get only active calls from PhoneStateListener

I would like to get only active calls from PhoneStateListener.
I have created the listener with method onCallStateChanged.
Once I dial (and the call is picked up) the state is already off-hook.
Well, it doesn't really surprise me, because of the text in the API:
Device call state: Off-hook. At least one call exists that is dialing, active, or on hold, and no calls are ringing or waiting.
But is there a possibility to get only active calls?
From what I tried on my nexus one, active call wake CALL_STATE_OFFHOOK,
try use log file to watch the steps.
Try this ..
private PhoneStateListener mPhoneListener = new PhoneStateListener() {
public void onCallStateChanged(int state, String incomingNumber) {
switch (state) {
case TelephonyManager.CALL_STATE_RINGING:
Log.d(TAG, "ringing goes here");
break;
case TelephonyManager.CALL_STATE_OFFHOOK:
Log.d(TAG, "Active call goes here");
break;
case TelephonyManager.CALL_STATE_IDLE:
Log.d(TAG, "CALL STATE IDLE goes here");
break;
default:
Log.d(TAG, "Unknown state=" + state);
}
}
and then call it :
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TelephonyManager tm = (TelephonyManager)getSystemService(TELEPHONY_SERVICE);
tm.listen(mPhoneListener, PhoneStateListener.LISTEN_CALL_STATE);
}
Don't forget add permission to your Mainifest file:
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>

Categories

Resources