android拨打电话功能实现

在day01项目中实现拨打电话功能

1.首先在AndroidManifest.XML文件中设置添加拨打电话的权限与动作

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.day01"
    android:versionCode="1"
    android:versionName="1.0" >


    <!-- 拨号权限 -->
    <uses-permission android:name="android.permission.CALL_PHONE" />
    
    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="21" />


    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name" >


            <!-- 意图过滤器 -->
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <!-- 添加拨打电话的动作-->
        <activity
            android:name=".CallPhoneActivity"
            android:label="@string/title_activity_call_phone" >
        </activity>
    </application>
</manifest>


2.在res文件夹下的布局文件夹layout下创建CallPhoneActivity.xml文件,设置拨打电话界面的布局:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="${relativePackage}.${activityClass}" >
    <!-- TextView 控件-->
    <TextView
        android:id="@+id/tv_tip_phone"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/tv_tip_phone" />
    <!-- EditText 控件-->
    <EditText 
        android:id="@+id/et_phone"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:inputType="phone"
        android:layout_below="@+id/tv_tip_phone"
        />
    <!-- Button 控件-->
    <Button 
        android:id="@+id/call_phone"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/btn_call_phones"
        android:layout_below="@+id/et_phone"
        android:onClick="callPhone"
        />
</RelativeLayout>

3.在在res文件夹下的布局文件夹values下的strings.xml中自定义字符串资源

<?xml version="1.0" encoding="utf-8"?>
<resources>


    <string name="app_name">day01</string>
    <string name="hello_world">Hello world!</string>


    <!-- 定义字符串资源 -->
    
    <string name="title_activity_call_phone">拨号界面</string>
    <string name="tv_tip_phone">请输入电话号码</string>
    <string name="btn_call_phones">拨号</string>

</resources>

4.在src文件夹中的MainActivity.java中写入实现拨打电话功能的方法

package com.example.day01;


import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;


public class MainActivity extends Activity {


//声明控件对象
private EditText et_phone;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//设置显示的视图
setContentView(R.layout.activity_call_phone);

//查找视图中的控件
et_phone = (EditText)findViewById(R.id.et_phone);

}

/**
* 拨号的实现
* @param v
*/
public void callPhone(View v){
//获取输入的电话号码
String phone = et_phone.getText().toString();


if(!TextUtils.isEmpty(phone)){


  Intent intent = new Intent(Intent.ACTION_CALL,Uri.parse("tel:"+phone));


//执行意图
startActivity(intent);


}else{

Toast.makeText(this, "请输入手机号码", Toast.LENGTH_LONG).show();

}
}

}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值