在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();
}
}
}