调用系统自带的拨号功能打电话

来源:互联网 发布:淘宝买家秀福利木耳 编辑:程序博客网 时间:2024/04/30 07:56

效果图:



具体实施如下:

1.layout中 activity_main.xml

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical">    <TextView        android:id="@+id/text"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="请输入号码:"        android:textSize="20dp"/>    <EditText        android:id="@+id/ed"        android:layout_width="match_parent"        android:layout_height="wrap_content"/>    <Button        android:id="@+id/bt"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_gravity="center"        android:text="拨打"/></LinearLayout>

2.Activity中MainActivity

package com.liyulei.phonedeom;import android.content.Intent;import android.net.Uri;import android.os.Bundle;import android.support.v7.app.AppCompatActivity;import android.view.View;import android.widget.Button;import android.widget.EditText;public class MainActivity extends AppCompatActivity {    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        //获取布局中文件中定义的按钮        Button bt = (Button) findViewById(R.id.bt);        //设置一个侦听        bt.setOnClickListener(new myListener());    }    class myListener implements View.OnClickListener{        //按钮点击时调用        @Override        public void onClick(View v) {            //获取输入框的对象            EditText ed = (EditText) findViewById(R.id.ed);            // 获取用户输出的号码            String number = ed.getText().toString();            //告诉系统,我的动作是打电话            //1.创建意图对象            Intent intent = new Intent();            //2.把动作封装到意图中            intent.setAction(Intent.ACTION_CALL);            //3.打电话打给谁            intent.setData(Uri.parse("tel:"+ number));            //4.告诉系统我的动作            startActivity(intent);        }    }}


3.有了上面的代码,还不能够完全运行,还需要得到拨打电话的权限(在androidManifest.xml进行设置)

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


0 0
原创粉丝点击