我是靠谱客的博主 现代钢笔,这篇文章主要介绍Android studio案例之实现电话拨号,现在分享给大家,希望可以做个参考。

 一、代码配置

1、创建项目

流程看图

2、增添代码

更改布局

布局完整代码

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context=".MainActivity"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="电话拨号" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toTopOf="parent" /> </LinearLayout>

插入图片

activity_main_xml文件布局

完整代码

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context=".MainActivity"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="电话拨号" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toTopOf="parent" /> <EditText android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="输入电话号码" android:inputType="number" android:id="@+id/phoneNum"/> <RelativeLayout android:layout_width="match_parent" android:layout_height="match_parent"> <ImageButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/call" android:layout_centerInParent="true" android:id="@+id/call_btn"/> </RelativeLayout> </LinearLayout>

效果展示

mainactivity.java文件

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
EditText phoneNum; ImageButton call_btn; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); phoneNum=(EditText) findViewById(R.id.phoneNum); call_btn=(ImageButton) findViewById(R.id.call_btn); call_btn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent=new Intent(); intent.setAction(Intent.ACTION_CALL); intent.setData(Uri.parse("tel:"+phoneNum.getText())); startActivity(intent);

图片

运行代码

拨号尝试,出现报错,需要设置对应权限

3、权限请求

Androidmainfest.xml文件中

复制代码
1
<uses-permission android:name="android.permission.CALL_PHONE"/>

Android 6.0以上需要自己手动赋予权限。

应用程序权限请求
版本号判断方法

复制代码
1
2
3
protected boolean shouldAskPermissions(){ return (Build.VERSION.SDK_INT > Build.VERSION_CODES.LOLLIPOP_MR1); }

权限申请方法

复制代码
1
2
3
4
5
6
7
protected void askPermissions() { String[] permissions = { "android.permission.CALL_PHONE" }; int requestCode = 200; requestPermissions(permissions, requestCode); }

在onCreate中调用

复制代码
1
2
3
if(shouldAskPermissions()){ askPermissions(); }

请求权限加入代码时要保证程序处于运行状态,不然代码加进去会报错。

二、效果演示

随便输入得数字号码,提示为空号。

以上就是Android studio案例之实现电话拨号的详细内容,更多关于Android studio电话拨号的资料请关注靠谱客其它相关文章!

最后

以上就是现代钢笔最近收集整理的关于Android studio案例之实现电话拨号的全部内容,更多相关Android内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(122)

评论列表共有 0 条评论

立即
投稿
返回
顶部