打电话
16lz
2021-12-04
上篇写了用Android发短信的demo,这篇我们来了解下如何用Android拨打电话
直接看代码
修改main.xml
view plain- <?xmlversion="1.0"encoding="utf-8"?>
- <LinearLayoutxmlns: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
view plain- <?xmlversion="1.0"encoding="utf-8"?>
- <resources>
- <stringname="button">拨打电话</string>
- <stringname="app_name">actionCall</string>
- <stringname="text">13800138000</string>
- <stringname="hello">hello</string>
- </resources>
添加关键代码
view plain- packagecom.call;
- importjava.util.regex.Matcher;
- importjava.util.regex.Pattern;
- importandroid.app.Activity;
- importandroid.content.Intent;
- importandroid.net.Uri;
- importandroid.os.Bundle;
- importandroid.view.View;
- importandroid.widget.Button;
- importandroid.widget.EditText;
- importandroid.widget.Toast;
- publicclassActionCallextendsActivity{
- /**Calledwhentheactivityisfirstcreated.*/
- privateButtonbutton;
- privateEditTexttext;
- @Override
- publicvoidonCreate(BundlesavedInstanceState){
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- text=(EditText)findViewById(R.id.text);
- button=(Button)findViewById(R.id.button);
- button.setOnClickListener(newButton.OnClickListener(){
- @Override
- publicvoidonClick(Viewv){
- //TODOAuto-generatedmethodstub
- try{
- StringinputStr=text.getText().toString();
- if(isPhoneNumberValid(inputStr)==true){
- IntentmyIntentDial=newIntent(
- "Intent.ACTION_CALL",Uri.parse("tel:"+inputStr)
- );
- startActivity(myIntentDial);
- text.setText("");
- }else{
- text.setText("");
- Toast.makeText(ActionCall.this,"电话格式不对",Toast.LENGTH_LONG).show();
- }
- }catch(Exceptione){
- //TODO:handleexception
- System.out.println(e.getMessage());
- }
- }
- });
- }
- publicstaticbooleanisPhoneNumberValid(StringphoneNumber){
- booleanisValid=false;
- Stringexpression="^//(?(//d{3})//)?[-]?(//d{3})[-]?(//d{5})$";
- Stringexpression2="^//(?(//d{3})//)?[-]?(//d{4})[-]?(//d{4})$";
- CharSequenceinputStr=phoneNumber;
- Patternpattern=Pattern.compile(expression);
- Matchermatcher=pattern.matcher(inputStr);
- Patternpattern2=Pattern.compile(expression2);
- Matchermatcher2=pattern2.matcher(inputStr);
- if(matcher.matches()||matcher2.matches()){
- isValid=true;
- }
- returnisValid;
- }
- }
最后,别忘了在AndroidManifest.xml中添加权限
view plain- <?xmlversion="1.0"encoding="utf-8"?>
- <manifestxmlns:android="http://schemas.android.com/apk/res/android"
- package="com.call"
- android:versionCode="1"
- android:versionName="1.0">
- <applicationandroid:icon="@drawable/icon"android:label="@string/app_name">
- <activityandroid:name=".ActionCall"
- android:label="@string/app_name">
- <intent-filter>
- <actionandroid:name="android.intent.action.MAIN"/>
- <categoryandroid:name="android.intent.category.LAUNCHER"/>
- </intent-filter>
- </activity>
- </application>
- <uses-permissionandroid:name="android.permission.CALL_PHONE">
- </uses-permission>
- </manifest>
这样,整个demo就完成了,以下是运行结果
小结
1、如果想要直接拨打电话
view plain- Intentintent=newIntent(Intent.ACTION_CALL,uri);
如果想弹出拨号窗口,java代码应改为:
view plain- Intentintent=newIntent(Intent.ACTION_DIAL,uri);
最后别忘记添加拨号权限
view plain- <uses-permissionandroid:name="android.permission.CALL_PHONE">
更多相关文章
- 我开发的第一个Android软件
- Android(安卓)Activity界面切换添加动画特效
- android tab上显示数字
- Android(安卓)布局
- Android安装卸载程序具体操作方法解析
- Android代码混淆打包
- Flutter 环境第一次运行问题 ProcessException: Process "D:\fl
- android: 大量开源项目源代码分享
- 史上最全!最经典!最无私的Android资料(书籍+代码)分享-不要积分