2012-09-24 80 views
2

我有一个ImageButton触发呼叫意图。我想知道是否可以传递2个数字作为数据,以便当您点击该按钮时,它会自动打开一个弹出窗口,让您选择要拨打的号码。这应该是您的联系人的默认Android行为,如果您有多个与您要拨打的联系人关联的号码,它会为您提供选项。Android呼叫意图多号码选择

我的代码是这样的:

Intent callIntent = new Intent(Intent.ACTION_CALL); 
callIntent.setData(Uri.parse("1234567890")); 
startActivity(callIntent); 

我试图把由分号separeted多号,但不工作:

Intent callIntent = new Intent(Intent.ACTION_CALL); 
callIntent.setData(Uri.parse("1234567890;0987654321")); 
startActivity(callIntent); 

有没有人有一个解决方案?

感谢 曼努埃尔

回答

0

我不认为有退出编程任何这样的事情。

但你可以尝试这样的事情,在图像按钮点击:

1)如果只有1号,使用你所编写的代码。

2)如果有多个号码,打开对话框(您需要创建一个全部由自己),并让用户选择号码,然后重定向数代码

+0

注意,在Android的存在是不同的对话框'AlertDialog可能是你想要做的。 –

+0

谢谢,我认为这是可能的,但我想你是对的。 – manuel

0
import android.Manifest; 
    import android.content.Context; 
    import android.content.Intent; 
    import android.content.pm.PackageManager; 
    import android.net.Uri; 
    import android.support.v4.app.ActivityCompat; 
    import android.support.v7.app.AppCompatActivity; 
    import android.os.Bundle; 
    import android.telephony.PhoneStateListener; 
    import android.telephony.TelephonyManager; 
    import android.text.Html; 
    import android.text.method.LinkMovementMethod; 
    import android.util.Log; 
    import android.view.View; 
    import android.widget.TextView; 

    import java.util.ArrayList; 
    import java.util.regex.Matcher; 
    import java.util.regex.Pattern; 

    public class MainActivity extends AppCompatActivity { 

     TextView tv; 

     @Override 
     protected void onCreate(Bundle savedInstanceState) { 
      super.onCreate(savedInstanceState); 
      setContentView(R.layout.activity_main); 
      attachUi(); 
      //getPattern(); 
     } 

     private void attachUi() { 
      tv = (TextView) findViewById(R.id.textview); 
      tv.setOnClickListener(clk); 
     } 

     private View.OnClickListener clk = new View.OnClickListener() { 
      @Override 
      public void onClick(View view) { 
       gotoCall(); 
      } 
     }; 

     private void gotoCall() { 
      ArrayList<String> list = new ArrayList<String>(); 
      list.add("08698511503"); 
      list.add("07666175151"); 
      list.add("0548554552"); 



      TelephonyManager TelephonyMgr = (TelephonyManager) this.getSystemService(Context.TELEPHONY_SERVICE); 
      TelephonyMgr.listen(new PhoneCallListener(this, list), PhoneStateListener.LISTEN_CALL_STATE); 

      if (ActivityCompat.checkSelfPermission(this, Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) { 
       // TODO: Consider calling 
       // ActivityCompat#requestPermissions 
       // here to request the missing permissions, and then overriding 
       // public void onRequestPermissionsResult(int requestCode, String[] permissions, 
       //           int[] grantResults) 
       // to handle the case where the user grants the permission. See the documentation 
       // for ActivityCompat#requestPermissions for more details. 
       return; 
      } 
      //startActivity(callIntent); 
     } 
    } 



    // create new class 

    class PhoneCallListener extends PhoneStateListener { 
     int count = 0; 
     Context context; 
     ArrayList<String> list; 

     public PhoneCallListener(MainActivity mainActivity, ArrayList<String> list) { 
      this.context = mainActivity; 
      this.list = list; 
     } 

     @Override 
     public void onCallStateChanged(int state, String incomingNumber) { 
      super.onCallStateChanged(state, incomingNumber); 
      Log.v(this.getClass().getSimpleName(), "List Size ::" + list); 
      switch (state) { 
       case TelephonyManager.CALL_STATE_RINGING: 
        Log.v(this.getClass().getSimpleName(), "Inside Ringing State::"); 
        break; 
       case TelephonyManager.CALL_STATE_IDLE: 
        String phone_number = null; 
        Log.v(this.getClass().getSimpleName(), "Inside Idle State::"); 
        if (list.size() > count) { 
         phone_number = list.get(count); 
         count++; 
        } 
        if (phone_number != null) { 
         nextCalling(phone_number); 
        } 
        break; 
       case TelephonyManager.CALL_STATE_OFFHOOK: 
        Log.v(this.getClass().getSimpleName(), "Inside OFFHOOK State::"); 
        break; 
       default: 
        break; 
      } 
     } 

     private void nextCalling(String phone_number) { 
      Intent callIntent1 = new Intent(Intent.ACTION_CALL); 
      callIntent1.setData(Uri.parse("tel:" + phone_number)); 
      if (ActivityCompat.checkSelfPermission(context, Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) { 
       // TODO: Consider calling 
       // ActivityCompat#requestPermissions 
       // here to request the missing permissions, and then overriding 
       // public void onRequestPermissionsResult(int requestCode, String[] permissions, 
       //           int[] grantResults) 
       // to handle the case where the user grants the permission. See the documentation 
       // for ActivityCompat#requestPermissions for more details. 
       return; 
      } 
      context.startActivity(callIntent1); 
     } 
    } 

// in manifest add permission.. 

<uses-permission android:name="android.permission.CALL_PHONE" /> 
    <uses-permission android:name="android.permission.READ_PHONE_STATE" />