2017-06-19 50 views
0

請幫我修理我的代碼,以便當我按下通話按鈕時,代碼會調用我提供的手機號碼。當我按通話按鈕時,我的代碼不會撥打電話

//monitor phone call activities 
private class PhoneCallListener extends PhoneStateListener { 

    private boolean isPhoneCalling = false; 

    String LOG_TAG = "LOGGING 123"; 

    @Override 
    public void onCallStateChanged(int state, String incomingNumber) { 
     if (TelephonyManager.CALL_STATE_RINGING == state) { 
      // phone ringing 
      Log.i(LOG_TAG, "RINGING, number: " + incomingNumber); 
      isPhoneCalling = true; 
     } 

     if (TelephonyManager.CALL_STATE_OFFHOOK == state) { 
      // active 
      Log.i(LOG_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(LOG_TAG, "IDLE"); 

      if (isPhoneCalling) { 
       Log.i(LOG_TAG, "restart app"); 

       // restart app 
       Intent i = getBaseContext().getPackageManager() 
         .getLaunchIntentForPackage(
           getBaseContext().getPackageName()); 
       i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
       startActivity(i); 

       isPhoneCalling = true; 
      } 

     } 
    } 
} 

回答

0

爲了使你的代碼應該寫的簡單,小碼的而不是爲了使給定數量的呼叫準確的代碼如下:

// add button listener 
     button.setOnClickListener(new OnClickListener() { 

      @Override 
      public void onClick(View arg0) { 

       Intent callIntent = new Intent(Intent.ACTION_CALL); 
       callIntent.setData(Uri.parse("tel:"+999999999));//change the number 
       startActivity(callIntent); 

      } 

     }); 
相關問題