-3

如何从三星 10.1 Android 平板电脑拨打电话?平板电脑支持 mini SIM 卡、2G 和 3G 网络。但是,我可以在平板电脑上接收短信,但无法拨打任何电话。当我想从我的应用程序中拨打电话时,我会被重定向到添加新联系人。(请注意,从电话设备拨打电话可以正常工作!)这是我拨打电话的代码:

public void onClick(View v) {
            String destination = mContactsAt.getText().toString();
            Log.d("CallActivity", "after getting the contact name");

            String phoneNo = getPhoneNumber(destination);               
            Log.d("CallActivity", "after showing number");
            if (phoneNo.startsWith("+")){
                phoneNo.replace("+", "00");                 
            }
            phoneNo.replaceAll("[^0-9]+", "");
            Log.d("CallActivity", "phoneNo to call =" + phoneNo + " destination " + destination);   
            phoneNumber = phoneNo;
            contactName = destination;

            Intent intent = new Intent(Intent.ACTION_CALL);
            intent.setData(Uri.parse("tel:"+phoneNo.trim()));
            startActivity(intent);
    }
4

1 回答 1

1

把它放在你的 oncreate() 中:

    // Register for Phone Calling
    PhoneCallListener phoneListener = new PhoneCallListener();
    TelephonyManager telephonyManager = (TelephonyManager) this
            .getSystemService(Context.TELEPHONY_SERVICE);
    telephonyManager.listen(phoneListener,
            PhoneStateListener.LISTEN_CALL_STATE);

这从你开始打电话的地方:

      Intent callIntent = new Intent(
                        Intent.ACTION_CALL);
      callIntent.setData(Uri.parse("tel:06641234567"));

      startActivity(callIntent);

至少:

   public 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);
        }

        if (TelephonyManager.CALL_STATE_OFFHOOK == state) {
            // active
            AudioManager audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
            audioManager.setMode(AudioManager.MODE_IN_CALL);
            audioManager.setSpeakerphoneOn(true);

            System.out.println("SPEAKER ON!!");

            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_PREVIOUS_IS_TOP);
          startActivity(i);

         isPhoneCalling = false; }

          }

    }
}
于 2013-08-28T12:53:04.303 回答