📅  最后修改于: 2020-10-11 03:45:20             🧑  作者: Mango
我们能够通过intent在android中拨打电话。您只需编写三行代码即可拨打电话。
Intent callIntent = new Intent(Intent.ACTION_CALL);
callIntent.setData(Uri.parse("tel:"+8802177690));//change the number
startActivity(callIntent);
从面板上拖动EditText和Button,现在activity_main.xml文件将如下所示:
您需要编写CALL_PHONE权限,如下所示:
让我们编写代码以通过意图拨打电话。
package com.example.phonecall;
import android.net.Uri;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends Activity {
EditText edittext1;
Button button1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Getting the edittext and button instance
edittext1=(EditText)findViewById(R.id.editText1);
button1=(Button)findViewById(R.id.button1);
//Performing action on button click
button1.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View arg0) {
String number=edittext1.getText().toString();
Intent callIntent = new Intent(Intent.ACTION_CALL);
callIntent.setData(Uri.parse("tel:"+number));
startActivity(callIntent);
}
});
}
@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_main, menu);
return true;
}
}