本文打算实现具有一个一键拨号功能的 APP
1.布局文件activity_main.xml
xmlns:tools="/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="cn.sehzh.intenttester.MainActivity" >
android:id="@+id/mCallButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="26dp"
android:text="Call" />
2.MainActivity
package cn.sehzh.intenttester;
import android.app.Activity;
import android.content.Intent;
import .Uri;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends Activity {
private Button mBtn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mBtn = (Button) findViewById(R.id.mCallButton);
mBtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
dialPhoneNumber("110");
}
});
}
private void dialPhoneNumber(String phoneNumber) {
Intent intent = new Intent(Intent.ACTION_CALL);
intent.setData(Uri.parse("tel:" + phoneNumber));
if (intent.resolveActivity(getPackageManager()) != null) {
startActivity(intent);
}
}
}3.运行效果
4.注意
Intent intent = new Intent(Intent.ACTION_CALL);中的ACTION_CALL如果写为ACTION_DIAL则会跳出带键盘拨号界面,而不是直接拨打电话;
需要以下权限