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

直接看代码

修改main.xml

view plain
  1. <?xmlversion="1.0"encoding="utf-8"?>
  2. <LinearLayoutxmlns: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. <Button
  13. android:id="@+id/button"
  14. android:layout_width="fill_parent"
  15. android:layout_height="wrap_content"
  16. android:text="@string/button"
  17. />
  18. <EditText
  19. android:id="@+id/text"
  20. android:layout_width="fill_parent"
  21. android:layout_height="wrap_content"
  22. android:text="@string/text"
  23. />
  24. </LinearLayout>

修改资源文件strings.xml

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

添加关键代码

view plain
  1. packagecom.call;
  2. importjava.util.regex.Matcher;
  3. importjava.util.regex.Pattern;
  4. importandroid.app.Activity;
  5. importandroid.content.Intent;
  6. importandroid.net.Uri;
  7. importandroid.os.Bundle;
  8. importandroid.view.View;
  9. importandroid.widget.Button;
  10. importandroid.widget.EditText;
  11. importandroid.widget.Toast;
  12. publicclassActionCallextendsActivity{
  13. /**Calledwhentheactivityisfirstcreated.*/
  14. privateButtonbutton;
  15. privateEditTexttext;
  16. @Override
  17. publicvoidonCreate(BundlesavedInstanceState){
  18. super.onCreate(savedInstanceState);
  19. setContentView(R.layout.main);
  20. text=(EditText)findViewById(R.id.text);
  21. button=(Button)findViewById(R.id.button);
  22. button.setOnClickListener(newButton.OnClickListener(){
  23. @Override
  24. publicvoidonClick(Viewv){
  25. //TODOAuto-generatedmethodstub
  26. try{
  27. StringinputStr=text.getText().toString();
  28. if(isPhoneNumberValid(inputStr)==true){
  29. IntentmyIntentDial=newIntent(
  30. "Intent.ACTION_CALL",Uri.parse("tel:"+inputStr)
  31. );
  32. startActivity(myIntentDial);
  33. text.setText("");
  34. }else{
  35. text.setText("");
  36. Toast.makeText(ActionCall.this,"电话格式不对",Toast.LENGTH_LONG).show();
  37. }
  38. }catch(Exceptione){
  39. //TODO:handleexception
  40. System.out.println(e.getMessage());
  41. }
  42. }
  43. });
  44. }
  45. publicstaticbooleanisPhoneNumberValid(StringphoneNumber){
  46. booleanisValid=false;
  47. Stringexpression="^//(?(//d{3})//)?[-]?(//d{3})[-]?(//d{5})$";
  48. Stringexpression2="^//(?(//d{3})//)?[-]?(//d{4})[-]?(//d{4})$";
  49. CharSequenceinputStr=phoneNumber;
  50. Patternpattern=Pattern.compile(expression);
  51. Matchermatcher=pattern.matcher(inputStr);
  52. Patternpattern2=Pattern.compile(expression2);
  53. Matchermatcher2=pattern2.matcher(inputStr);
  54. if(matcher.matches()||matcher2.matches()){
  55. isValid=true;
  56. }
  57. returnisValid;
  58. }
  59. }

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

view plain
  1. <?xmlversion="1.0"encoding="utf-8"?>
  2. <manifestxmlns:android="http://schemas.android.com/apk/res/android"
  3. package="com.call"
  4. android:versionCode="1"
  5. android:versionName="1.0">
  6. <applicationandroid:icon="@drawable/icon"android:label="@string/app_name">
  7. <activityandroid:name=".ActionCall"
  8. android:label="@string/app_name">
  9. <intent-filter>
  10. <actionandroid:name="android.intent.action.MAIN"/>
  11. <categoryandroid:name="android.intent.category.LAUNCHER"/>
  12. </intent-filter>
  13. </activity>
  14. </application>
  15. <uses-permissionandroid:name="android.permission.CALL_PHONE">
  16. </uses-permission>
  17. </manifest>

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

小结

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

view plain
  1. Intentintent=newIntent(Intent.ACTION_CALL,uri);

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

view plain
  1. Intentintent=newIntent(Intent.ACTION_DIAL,uri);

最后别忘记添加拨号权限

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

更多相关文章

  1. 我开发的第一个Android软件
  2. Android(安卓)Activity界面切换添加动画特效
  3. android tab上显示数字
  4. Android(安卓)布局
  5. Android安装卸载程序具体操作方法解析
  6. Android代码混淆打包
  7. Flutter 环境第一次运行问题 ProcessException: Process "D:\fl
  8. android: 大量开源项目源代码分享
  9. 史上最全!最经典!最无私的Android资料(书籍+代码)分享-不要积分

随机推荐

  1. textView 属性总结
  2. Android中的Binder机制一(实名Binder)
  3. 谷歌Android被Linux内核除名(转)
  4. Tabhost中Activity绑定Service
  5. Android音乐播放器开发小记——项目简介
  6. android EditText 的键盘弹出(不弹出)坑爹
  7. Android - 修改最小SDK版本(minSdkVersio
  8. apk重签名之re-sign.jar重签名
  9. Android 8.1 中Systemui中的常见修改(八)Sy
  10. Android平台上面输入法遮挡问题-android: