android应用程序因中断而关闭

android应用程序因中断而关闭,android,interrupt-handling,Android,Interrupt Handling,当我的android应用程序运行时,我接到一个电话,要么我参加通话,要么取消通话。我的应用程序在后台关闭 我希望我的应用程序在后台运行 当我用On-Stop方法接听电话时,我可以看到我添加的toast消息 @Override protected void onStop() { super.onStop(); Toast.makeText(getApplicationContext(), "onStop()", Toast.LENGTH_LONG).show

当我的android应用程序运行时,我接到一个电话,要么我参加通话,要么取消通话。我的应用程序在后台关闭

我希望我的应用程序在后台运行

当我用On-Stop方法接听电话时,我可以看到我添加的toast消息

@Override
    protected void onStop() {
        super.onStop();
        Toast.makeText(getApplicationContext(), "onStop()", Toast.LENGTH_LONG).show();
}
当电话通话结束时,我需要打开我的应用程序,我该怎么办

这是我的onResume代码片段

@Override
    protected void onResume() {
        super.onResume();
        Toast.makeText(getApplicationContext(), "on Resume being called", Toast.LENGTH_LONG).show();
}
这将在活动开始时调用

@Override
    protected void onPause() {
        super.onPause();
}

我应该在暂停和恢复中添加什么才能返回到我的应用程序使用电话管理器跟踪电话呼叫状态:

 public class Main_Activity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    TelephonyManager tele = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
    PhoneStateListener call = new PhoneStateListener(){
        public void onCallStateChanged (int state , String incomingNumber){
        if(state==TelephonyManager.CALL_STATE_RINGING)
        {
            Log.e ("Phone State" , "_________________Phone is Ringing");
            Toast.makeText(getBaseContext(), "Ringing", Toast.LENGTH_SHORT).show();
        }
        else if (state==TelephonyManager.CALL_STATE_OFFHOOK)
        {
            Log.e ("Call" , "_________________Phone Call");
            Toast.makeText(getBaseContext(), "On Call", Toast.LENGTH_SHORT).show();
        }

        else if (state==TelephonyManager.CALL_STATE_IDLE)
        {
            Log.e ("Call" , "_________________No Call");
            Toast.makeText(getBaseContext(), "Ideal", Toast.LENGTH_SHORT).show();
        }
        }
    };
    tele.listen(call,PhoneStateListener.LISTEN_CALL_STATE);
}

对我来说,它工作得很好。

你可以使用广播接收器来收听手机状态。并在通话结束后恢复应用程序。 因此,您应该添加一个PhoneStateBroadcastReceiver

添加到清单文件

<receiver android:name=".PhoneStateBroadcastReceiver">
<intent-filter>
     <action android:name="android.intent.action.PHONE_STATE"/>     
</intent-filter>
</receiver>

添加onPause&onResume方法的代码片段。谢谢,当通话结束时,我可以让toast空闲,如何将我的应用程序回调到我已提前停止的活动中。当通话断开时,尝试尝试一下。
public class PhoneStateBroadcastReceiver extends BroadcastReceiver{
    @Override
    public void onReceive(Context context, Intent intent) {
         TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
         telephonyManager.listen(new CustomPhoneStateListener(context), PhoneStateListener.LISTEN_CALL_STATE);
    }
}