简体   繁体   English

处理来电时的Android应用暂停,并在通话结束后恢复

[英]Handling Android application pause on incoming call and resume after call end

I want to pause my android application when the phone receives an incoming call. 我希望在手机收到来电时暂停我的Android应用程序。 After the call ends, I want my applications to resume automatically. 通话结束后,我希望我的应用程序自动恢复。

How would this be implemented in an Android application? 如何在Android应用程序中实现?

you have to implement a Listener for the PhoneState. 你必须为PhoneState实现一个Listener。 I did this in a private Class: 我是在私人班级做到的:

private class PhoneCallListener extends PhoneStateListener {

    private boolean isPhoneCalling = false;

    // needed for logging
    String TAG = "PhoneCallListener";

    @Override
    public void onCallStateChanged(int state, String incomingNumber) {

        if (TelephonyManager.CALL_STATE_RINGING == state) {
            // phone ringing
            Log.i(TAG, "RINGING, number: " + incomingNumber);
        }

        if (TelephonyManager.CALL_STATE_OFFHOOK == state) {
            // active
            Log.i(TAG, "OFFHOOK");

            isPhoneCalling = true;
        }

        if (TelephonyManager.CALL_STATE_IDLE == state) {
            // run when class initial and phone call ended,
            // need detect flag from CALL_STATE_OFFHOOK
            Log.i(TAG, "IDLE");

            if (isPhoneCalling) {

                Log.i(TAG, "restart app");

                // restart call application
                Intent i = getBaseContext().getPackageManager()
                        .getLaunchIntentForPackage(
                                getBaseContext().getPackageName());
                i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK
                        | Intent.FLAG_ACTIVITY_CLEAR_TOP
                        | Intent.FLAG_ACTIVITY_SINGLE_TOP);
                startActivity(i);

                isPhoneCalling = false;
            }

        }


}
}

and you need to add the permission to the Manifest-File 并且您需要将权限添加到Manifest-File

<uses-permission android:name="android.permission.READ_PHONE_STATE" />
private class EndCallListener extends PhoneStateListener {
  private boolean active = false;
  @Override
  public void onCallStateChanged(int state, String incomingNumber) {
    if(TelephonyManager.CALL_STATE_RINGING == state) {
      Log.i("EndCallListener", "RINGING, number: " + incomingNumber);
    }
    if(TelephonyManager.CALL_STATE_OFFHOOK == state) {
      //wait for phone to go offhook (probably set a boolean flag) so you know your app initiated the call.
      active = true;
      Log.i("EndCallListener", "OFFHOOK");
    }
    if(TelephonyManager.CALL_STATE_IDLE == state) {
      //when this state occurs, and your flag is set, restart your app
      Log.i("EndCallListener", "IDLE");
      if (active) {
        active = false;
        // stop listening                   
        TelephonyManager mTM = (TelephonyManager) m_activity.getSystemService( Context.TELEPHONY_SERVICE );
        mTM.listen(this, PhoneStateListener.LISTEN_NONE);
        // restart the inbox activity
        //Intent intent = new Intent(m_activity, MDInboxActivity.class);
        //m_activity.startActivity(intent);
      }
    }
  }
}

And you can initialize the above class by calling the below lines: 您可以通过调用以下行来初始化上述类:

try {
  EndCallListener callListener = new EndCallListener();
  TelephonyManager mTM = (TelephonyManager) m_activity.getSystemService(Context.TELEPHONY_SERVICE);
  mTM.listen(callListener, PhoneStateListener.LISTEN_CALL_STATE);
} catch(Exception e) {
  Log.e("callMonitor", "Exception: "+e.toString());
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM