打电话

上篇写了用Android发短信的demo,这篇我们来了解下如何用Android拨打电话

 

直接看代码

 

修改main.xml

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:orientation="vertical"  
  4.     android:layout_width="fill_parent"  
  5.     android:layout_height="fill_parent"  
  6.     >  
  7. <TextView    
  8.     android:layout_width="fill_parent"   
  9.     android:layout_height="wrap_content"   
  10.     android:text="@string/hello"  
  11.     />  
  12.       
  13.     <Button    
  14.         android:id="@+id/button"  
  15.         android:layout_width="fill_parent"   
  16.         android:layout_height="wrap_content"   
  17.         android:text="@string/button"  
  18.     />  
  19.       
  20.     <EditText    
  21.         android:id="@+id/text"  
  22.         android:layout_width="fill_parent"   
  23.         android:layout_height="wrap_content"   
  24.         android:text="@string/text"  
  25.     />  
  26. </LinearLayout>  

 

修改资源文件strings.xml

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <resources>  
  3.     <string name="button">拨打电话</string>  
  4.     <string name="app_name">actionCall</string>  
  5.     <string name="text">13800138000</string>  
  6.     <string name="hello">hello</string>  
  7. </resources>  
 

 

添加关键代码

 

  1. package com.call;  
  2.   
  3. import java.util.regex.Matcher;  
  4. import java.util.regex.Pattern;  
  5.   
  6. import android.app.Activity;  
  7. import android.content.Intent;  
  8. import android.net.Uri;  
  9. import android.os.Bundle;  
  10. import android.view.View;  
  11. import android.widget.Button;  
  12. import android.widget.EditText;  
  13. import android.widget.Toast;  
  14.   
  15. public class ActionCall extends Activity {  
  16.     /** Called when the activity is first created. */  
  17.       
  18.     private Button button;  
  19.       
  20.     private EditText text;  
  21.       
  22.       
  23.       
  24.     @Override  
  25.     public void onCreate(Bundle savedInstanceState) {  
  26.         super.onCreate(savedInstanceState);  
  27.         setContentView(R.layout.main);  
  28.           
  29.         text = (EditText)findViewById(R.id.text);  
  30.         button = (Button)findViewById(R.id.button);  
  31.           
  32.         button.setOnClickListener(new Button.OnClickListener(){  
  33.   
  34.             @Override  
  35.             public void onClick(View v) {  
  36.                 // TODO Auto-generated method stub  
  37.                 try {  
  38.                     String inputStr = text.getText().toString();  
  39.                     if(isPhoneNumberValid(inputStr) == true){  
  40.                         Intent myIntentDial = new Intent(  
  41.                                 "Intent.ACTION_CALL",Uri.parse("tel:"+inputStr)  
  42.                         );  
  43.                           
  44.                         startActivity(myIntentDial);  
  45.                           
  46.                         text.setText("");  
  47.                     }else{  
  48.                         text.setText("");  
  49.                         Toast.makeText(ActionCall.this"电话格式不对", Toast.LENGTH_LONG).show();  
  50.                     }  
  51.                 } catch (Exception e) {  
  52.                     // TODO: handle exception  
  53.                     System.out.println(e.getMessage());  
  54.                 }  
  55.             }  
  56.               
  57.         });  
  58.     }  
  59.       
  60.     public static boolean isPhoneNumberValid(String phoneNumber){  
  61.           
  62.         boolean isValid = false;  
  63.         String expression = "^//(?(//d{3})//)?[- ]?(//d{3})[- ]?(//d{5})$";  
  64.         String expression2 = "^//(?(//d{3})//)?[- ]?(//d{4})[- ]?(//d{4})$";  
  65.         CharSequence inputStr = phoneNumber;  
  66.           
  67.         Pattern pattern = Pattern.compile(expression);  
  68.           
  69.         Matcher matcher = pattern.matcher(inputStr);  
  70.           
  71.         Pattern pattern2 = Pattern.compile(expression2);  
  72.           
  73.         Matcher matcher2 = pattern2.matcher(inputStr);  
  74.           
  75.         if(matcher.matches()||matcher2.matches()){  
  76.             isValid = true;  
  77.         }  
  78.           
  79.           
  80.         return isValid;  
  81.           
  82.     }  
  83.       
  84.       
  85. }  

 

最后,别忘了在AndroidManifest.xml中添加权限

 

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"  
  3.       package="com.call"  
  4.       android:versionCode="1"  
  5.       android:versionName="1.0">  
  6.     <application android:icon="@drawable/icon" android:label="@string/app_name">  
  7.         <activity android:name=".ActionCall"  
  8.                   android:label="@string/app_name">  
  9.             <intent-filter>  
  10.                 <action android:name="android.intent.action.MAIN" />  
  11.                 <category android:name="android.intent.category.LAUNCHER" />  
  12.             </intent-filter>  
  13.         </activity>  
  14.   
  15.     </application>  
  16.     <uses-permission android:name="android.permission.CALL_PHONE">  
  17.       
  18.     </uses-permission>  
  19.   
  20. </manifest>   

 

这样,整个demo就完成了,以下是运行结果

 

 

 

 

小结

 

1、如果想要直接拨打电话

 

  1. Intent intent = new Intent(Intent.ACTION_CALL, uri);  

 

如果想弹出拨号窗口,java代码应改为:

 

  1. Intent intent = new Intent(Intent.ACTION_DIAL, uri);  

 

 

最后别忘记添加拨号权限

 

  1. <uses-permission android:name="android.permission.CALL_PHONE">  

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值