首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 移动开发 > 移动开发 >

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

2012-08-30 
第三章:交互式通信服务与手机控制(自定义拨打电话)效果:main.xml?xml version1.0 encodingutf-8?

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








main.xml

<?xml version="1.0" encoding="utf-8"?><AbsoluteLayoutandroid:id="@+id/widget0"android:layout_width="fill_parent"android:layout_height="fill_parent"xmlns:android="http://schemas.android.com/apk/res/android"><EditTextandroid:id="@+id/edit"android:layout_width="232px"android:layout_height="wrap_content"android:textSize="18sp"android:layout_x="30px"android:layout_y="27px"></EditText><Buttonandroid:id="@+id/call"android:layout_width="157px"android:layout_height="wrap_content"android:text="&#25320;&#25171;&#30005;&#35805;"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;    }}


热点排行