打电话

本文详细介绍了使用Android拨打电话的全过程,包括修改布局文件、配置资源文件、添加关键代码及在AndroidManifest.xml中添加拨号权限。通过示例代码演示了如何实现拨打电话功能,并解释了相关代码逻辑。

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

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

直接看代码

修改main.xml

  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

  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>

添加关键代码

  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中添加权限

  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、如果想要直接拨打电话

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

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

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

最后别忘记添加拨号权限

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值