2

我尝试拨打电话,然后单击“结束通话”然后返回上一个活动(在本例中是我的主菜单应用程序)。我使用了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();
                }
            }
      }

我尝试单击按钮进行调用,然后立即结束调用它。当我尝试再次单击按钮呼叫时,它会重复呼叫该号码。

4

0 回答 0