2013-03-03 145 views
0

我想将多个按钮添加到我的应用程序中,所以如果您单击一个按钮,您会自动拨打某个人,但现在我卡在通话操作中,我该如何拨打电话正在点击按钮?我为我的活动命名为telefoonnummers.java下面的代码:拨打电话按钮

package com.example.rome; 

import android.os.Bundle; 
import android.app.Activity; 
import android.view.Menu; 
import android.view.MenuItem; 
import android.support.v4.app.NavUtils; 
import android.widget.EditText; 
import android.widget.Button; 
import android.view.View; 
import android.widget.Toast; 


public class Telefoonnummers extends Activity { 

Button mHALbellen; 
Button mWITbellen; 
Button mWDGbellen; 
Button mVlierbellen; 
Button mHotelbellen; 


@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_telefoonnummers); 
    mHALbellen = (Button) findViewById(R.id.button1); 
    mWITbellen = (Button) findViewById(R.id.button3); 
    mWDGbellen = (Button) findViewById(R.id.button4); 
    mVlierbellen = (Button) findViewById(R.id.button5); 
    mHotelbellen = (Button) findViewById(R.id.button6); 
    // Show the Up button in the action bar. 
    getActionBar().setDisplayHomeAsUpEnabled(true); 
} 

@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    // Inflate the menu; this adds items to the action bar if it is present. 
    getMenuInflater().inflate(R.menu.activity_telefoonnummers, menu); 
    return true; 
} 

@Override 
public boolean onOptionsItemSelected(MenuItem item) { 
    switch (item.getItemId()) { 
    case android.R.id.home: 
     // This ID represents the Home or Up button. In the case of this 
     // activity, the Up button is shown. Use NavUtils to allow users 
     // to navigate up one level in the application structure. For 
     // more details, see the Navigation pattern on Android Design: 
     // 
     // http://developer.android.com/design/patterns/navigation.html#up-vs-back 
     // 
     NavUtils.navigateUpFromSameTask(this); 
     return true; 
    } 
    return super.onOptionsItemSelected(item); 
} 

public void vanHalbellen(View view){ 
    if (view == mHALbellen){ 
     //WHICH CODE SHOULD BE HERE TO MAKE A PHONECALL WHEN THE BUTTON mHALBELLEN IS PRESSED?? 
      } 
     } 
} 

你们请帮忙吗?

回答

4

,添加以下权限到你的清单:

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

,然后单击按钮时执行此意图:

String uri = "tel: phone_number_here"; 
Intent intent = new Intent(Intent.ACTION_CALL); 
intent.setData(Uri.parse(uri)); 
startActivity(intent); 

您还可以使用Intent.ACTION_DIAL而不是Intent.ACTION_CALL。这显示了已经输入号码的拨号器,但是允许用户决定是否实际进行呼叫。 ACTION_DIAL不需要CALL_PHONE权限。