机器人。如何结束通话并返回之前的活动

时间:2013-06-07 13:38:26

标签: android telephonymanager

我尝试拨打电话,然后点击“结束通话”,然后返回上一个活动(在本例中为我的主菜单应用程序)。我用过phonestatelistener。问题是,当我点击结束通话时,它会转到我的主菜单,但会一次又一次地开始拨打电话。有解决方案吗?

这是我的代码:

public void onClick(View v)
                {
                    String telepon2 = telepon.getText().toString();                 
                    try {

                        Intent intent = new Intent(Intent.ACTION_CALL);
                        intent.setData(Uri.parse("tel:"+telepon2));
                        v.getContext().startActivity(intent);
                    } catch (ActivityNotFoundException activityException) {
                        Log.e("dialing-example", "Call failed", activityException);
                    }                   
                }               
            });

这是我的PhoneStateListener

TelephonyManager tManager = (TelephonyManager) 
                getSystemService(Context.TELEPHONY_SERVICE);
              listener = new ListenToPhoneState();
              tManager.listen(listener, PhoneStateListener.LISTEN_CALL_STATE);

这是我的监听电话

 private class ListenToPhoneState extends PhoneStateListener {

           String LOG_TAG = null;

        @Override
            public void onCallStateChanged(int state, String incomingNumber) {
                if(TelephonyManager.CALL_STATE_RINGING == state) {
                    Log.i(LOG_TAG, "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.
                    Log.i(LOG_TAG, "OFFHOOK");
                }
                if(TelephonyManager.CALL_STATE_IDLE == state) {
                    //when this state occurs, and your flag is set, restart your app
                    Log.i(LOG_TAG, "IDLE");
                    Intent homeIntent = new Intent(Intent.ACTION_MAIN);
                    homeIntent.addCategory(Intent.CATEGORY_HOME);
                    homeIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    finish();
                }
            }
      }

我尝试点击按钮进行调用,然后立即调用它。当我再次尝试点击按钮呼叫时,它会反复拨打该号码。

0 个答案:

没有答案