第三章:交互式通信服务与手机控制(自定义拨打电话)

本文介绍如何在Android应用中实现拨打电话功能,包括必要的XML布局文件配置、AndroidManifest.xml权限设置及Java代码实现。文中详细解释了如何验证电话号码格式,并通过Intent发起拨号。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

效果:

[img]http://dl.iteye.com/upload/attachment/389937/db995e17-1e47-3cb0-8fe4-a5d1eb83f73e.jpg[/img]


[img]http://dl.iteye.com/upload/attachment/389939/86cf6075-b24f-3d9e-b2ff-3eab55e545bc.jpg[/img]


main.xml

<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout
android:id="@+id/widget0"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
>

<EditText
android:id="@+id/edit"
android:layout_width="232px"
android:layout_height="wrap_content"
android:textSize="18sp"
android:layout_x="30px"
android:layout_y="27px"
>
</EditText>
<Button
android:id="@+id/call"
android:layout_width="157px"
android:layout_height="wrap_content"
android:text="拨打电话"
android:layout_x="54px"
android:layout_y="117px"
>
</Button>
</AbsoluteLayout>




AndroidManifest.xml需要添加权限Android.permission.CALL_PHONE否则点击按钮会报错

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="call.action.text"
android:versionCode="1"
android:versionName="1.0">
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".CallActionTest"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>

</application>
<!-- 添加拨出电话的权限 -->
<uses-permission android:name="android.permission.CALL_PHONE"></uses-permission>
</manifest>



package call.action.text;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

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

public class CallActionTest extends Activity {
private EditText ed;
private Button bt;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
/**载入main.xml*/
setContentView(R.layout.main);
/**通过id找到EditText组件化*/
ed=(EditText)findViewById(R.id.edit);
/**通过id找到Button组件化*/
bt=(Button)findViewById(R.id.call);
/**设置Button按钮点击事件*/
bt.setOnClickListener(new Button.OnClickListener(){
public void onClick(View v){
try {
/**获取输入文本框内容*/
String str = ed.getText().toString();
/**匹配号码格式是否正确*/
if (isPhoneNumberValid(str) == true) {
/**构建一个新的Intent运行action.CALL的常数通过URI将字符串带入*/

/**
* 也可以调用虚拟电话拨打电话
* Intent myIntentDial = new Intent(
* "android.intent.action.DIAL", Uri.parse("tel:"+ ""));
* */
Intent myIntentDial = new Intent(
"android.intent.action.CALL", Uri.parse("tel:"
+ str));
/**带入自定义的Intent对象以运行拨打电话的工作*/
startActivity(myIntentDial);
ed.setText("");
} else {
ed.setText("");
/**提示信息*/
Toast.makeText(CallActionTest.this, "输入的电话格式不正确!",
Toast.LENGTH_LONG).show();
}
} catch (Exception e) {
e.printStackTrace();
}
}
});
}

public boolean isPhoneNumberValid(String str){
boolean flg=false;
/**
* ^\\(?:可以使用"("作为开头
* (\\d{3}):紧接着三个数字
* \\)?:可以使用")"继续
* [- ]?:接着三个数字
* (\\d{4}$:以四个数字的结束
* 可以比较下列数字格式:
* (123)456-78900,123-4560-7890,12345679800,(123)-4560-7890*/
String str1="^\\(?(\\d{3})\\)?[- ]?(\\d{3})[- ]?(\\d{5})$";
String str2="^\\(?(\\d{3})\\)?[- ]?(\\d{4})[- ]?(\\d{4})$";
CharSequence input=str;
/**创建Pattern带入表达式*/
Pattern pattern=Pattern.compile(str1);
/**将Pattern以参数的形式传入Matcher*/
Matcher matcher=pattern.matcher(input);
Pattern pattern2=Pattern.compile(str2);
Matcher matcher2=pattern2.matcher(input);
if(matcher.matches()||matcher2.matches()){
flg=true;
}
return flg;
}
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值