Android: how can I start a phone call in my activity and then return to it?

I can start a phone call with the intention of:

String url = "tel:3334444"; Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse(url)); 

But it will remain on the phone call screen. What I want depends on the activity of my application. Is it possible to start a background call? Or immediately return to the previous action.

Thanks.

+4
source share
2 answers

You need to implement Phonecall state aside from your activity.

//// Processing 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) { } } }; 

}

+1
source

In the background thread (perhaps it should be in the foreground) or in a regular (fast) repeating ActivityManager.getRunningTasks () alarm. The first task is the highest. Check topActivity for this task to see if it is InCallScreen (note that on some Ericsson phones this is replaced with a special class). If so, bring your activity to the forefront.

You will need to use TelephonyManager to look at the hook to stop the background thread or alarm if the call is canceled.

0
source

Source: https://habr.com/ru/post/1380505/


All Articles