首页 > 技术文章 > 第五章 跳转到拨打电话 2.8

EndlessShw 2021-09-23 16:50 原文

1. MainActivity中定义函数:

 1     /**
 2      * 打电话给10086
 3      * 注意,对于高版本的安卓系统,调用时需要检查权限和动态赋予权限,以后会补充
 4      * @param view
 5      */
 6     public void Call(View view){
 7         //
 8         Intent intent = new Intent();
 9         // 这里的URI内容具体是啥需要查阅官方源码
10         Uri uri = Uri.parse("tel:10086");
11         intent.setData(uri);
12         // 在使用的时候记得要记录权限,可以详细查看该常量
13         // <uses-permission android:name="android.permission.CALL_PHONE" />
14         intent.setAction(Intent.ACTION_CALL);
15         intent.addCategory("android.intent.category.DEFAULT");
16 
17         // 简易写法
18 //        Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse("10086"));
19         startActivity(intent);
20     }
21 }

 

2. activity_main.xml中添加一个按钮:

1 <Button
2         android:layout_width="match_parent"
3         android:layout_height="wrap_content"
4         android:onClick="Call"
5         android:text="打电话给10086"/>

 

3. 在使用系统的setAction常量时,记得查看该常量需要注册的权限。要在Manifest中添加权限允许(这里是手机拨打权限):

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

 

推荐阅读