上篇写了用Android发短信的demo,这篇我们来了解下如何用Android拨打电话
直接看代码
修改main.xml
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="vertical"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- >
- <TextView
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="@string/hello"
- />
- <Button
- android:id="@+id/button"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="@string/button"
- />
- <EditText
- android:id="@+id/text"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="@string/text"
- />
- </LinearLayout>
修改资源文件strings.xml
- <?xml version="1.0" encoding="utf-8"?>
- <resources>
- <string name="button">拨打电话</string>
- <string name="app_name">actionCall</string>
- <string name="text">13800138000</string>
- <string name="hello">hello</string>
- </resources>
添加关键代码
- package com.call;
- 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 ActionCall extends Activity {
- /** Called when the activity is first created. */
- private Button button;
- private EditText text;
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- text = (EditText)findViewById(R.id.text);
- button = (Button)findViewById(R.id.button);
- button.setOnClickListener(new Button.OnClickListener(){
- @Override
- public void onClick(View v) {
- // TODO Auto-generated method stub
- try {
- String inputStr = text.getText().toString();
- if(isPhoneNumberValid(inputStr) == true){
- Intent myIntentDial = new Intent(
- "Intent.ACTION_CALL",Uri.parse("tel:"+inputStr)
- );
- startActivity(myIntentDial);
- text.setText("");
- }else{
- text.setText("");
- Toast.makeText(ActionCall.this, "电话格式不对", Toast.LENGTH_LONG).show();
- }
- } catch (Exception e) {
- // TODO: handle exception
- System.out.println(e.getMessage());
- }
- }
- });
- }
- public static boolean isPhoneNumberValid(String phoneNumber){
- boolean isValid = false;
- String expression = "^//(?(//d{3})//)?[- ]?(//d{3})[- ]?(//d{5})$";
- String expression2 = "^//(?(//d{3})//)?[- ]?(//d{4})[- ]?(//d{4})$";
- CharSequence inputStr = phoneNumber;
- Pattern pattern = Pattern.compile(expression);
- Matcher matcher = pattern.matcher(inputStr);
- Pattern pattern2 = Pattern.compile(expression2);
- Matcher matcher2 = pattern2.matcher(inputStr);
- if(matcher.matches()||matcher2.matches()){
- isValid = true;
- }
- return isValid;
- }
- }
最后,别忘了在AndroidManifest.xml中添加权限
- <?xml version="1.0" encoding="utf-8"?>
- <manifest xmlns:android="http://schemas.android.com/apk/res/android"
- package="com.call"
- android:versionCode="1"
- android:versionName="1.0">
- <application android:icon="@drawable/icon" android:label="@string/app_name">
- <activity android:name=".ActionCall"
- 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>
这样,整个demo就完成了,以下是运行结果
小结
1、如果想要直接拨打电话
- Intent intent = new Intent(Intent.ACTION_CALL, uri);
如果想弹出拨号窗口,java代码应改为:
- Intent intent = new Intent(Intent.ACTION_DIAL, uri);
最后别忘记添加拨号权限
- <uses-permission android:name="android.permission.CALL_PHONE">