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

心不动则不痛 提交于 2019-12-07 08:45:48

问题


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.


回答1:


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) {

       }
      }
     };  

}




回答2:


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.



来源:https://stackoverflow.com/questions/8074391/android-how-can-i-launch-phone-call-in-my-activity-and-then-back-to-it

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!