服务器之家:专注于VPS、云服务器配置技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - Android - android中关于call拨号功能的实现方法

android中关于call拨号功能的实现方法

2022-10-17 14:14HyperMan Android

这篇文章主要介绍了android中关于call拨号功能实现的记录,非常不错,具有一定的参考借鉴价值,需要的朋友可以参考下

 前几天考试居然记错dial和call,故在此写上小demo来作区别,加深印象。

主要是实现call(拨通电话)功能,dial(拨电话)功能用作对比,话不多说,贴上代码。

1.创建布局文件如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<?xml version="1.0" encoding="utf-8"?>
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
>
<button
  android:id="@+id/btn_dial"
  android:text="dial"
  android:layout_width="match_parent"
  android:layout_height="wrap_content" />
<button
  android:id="@+id/call"
  android:text="call"
  android:layout_width="match_parent"
  android:layout_height="wrap_content" />
</linearlayout>

也就是添加了两个按钮dial和call,废话

2.添加java代码:

?
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package com.cnblogs.dialandcall;
 
import android.manifest;
import android.content.intent;
import android.content.pm.packagemanager;
import android.net.uri;
import android.support.annotation.nonnull;
import android.support.v4.app.activitycompat;
import android.support.v4.content.contextcompat;
import android.support.v7.app.appcompatactivity;
import android.os.bundle;
import android.view.view;
import android.widget.button;
import android.widget.toast;
 
public class mainactivity extends appcompatactivity implements view.onclicklistener {
 
  private button btn_dial;
  private button btn_call;
  @override
  protected void oncreate(bundle savedinstancestate) {
    super.oncreate(savedinstancestate);
    setcontentview(r.layout.activity_main);
 
    btn_call = (button)findviewbyid(r.id.btn_call);
    btn_call.setonclicklistener(this);
 
    btn_dial = (button)findviewbyid(r.id.btn_dial);
    btn_dial.setonclicklistener(this);
  }
 
  @override
  public void onclick(view v) {
    switch (v.getid()){
      case r.id.btn_call:
        oncall();
        break;
      case r.id.btn_dial:
        intent dialintent = new intent(intent.action_dial);
        dialintent.setdata(uri.parse("tel:10086"));
        startactivity(dialintent);
        break;
    }
  }
 
  private void oncall() {
    int permissioncheck = contextcompat.checkselfpermission(this, manifest.permission.call_phone);
    if(permissioncheck!= packagemanager.permission_granted){
      activitycompat.requestpermissions(this,new string[]{manifest.permission.call_phone}, integer.parseint("001"));
    }
    else{
      startactivity(new intent(intent.action_call).setdata(uri.parse("tel:10086")));
    }
  }
 
  @override
  public void onrequestpermissionsresult(int requestcode, @nonnull string[] permissions, @nonnull int[] grantresults) {
    switch (requestcode){
      case 001:
        if(grantresults.length>0&&(grantresults[0]==packagemanager.permission_granted)){
          oncall();
        }
        else {
          toast.maketext(getbasecontext(),"you need allow the permission to run this app",toast.length_short).show();
        }
        break;
    }
  }
}

•需要注意的是,我在btn_call按钮点击事件中添加了单独的方法来进行处理,这是因为call_phone在android 6.0及以上版本被认为是危险权限,需要在程序运行时申请。

•关于android中权限的分类请参考以下链接:

https://developer.android.google.cn/guide/topics/security/permissions.html#normal-dangerous

 3.添加manifest.xml文件代码:

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

千万不要忘记在androidmanifest.xml中添加上权限申明哦:)

 实现效果截图:

  android中关于call拨号功能的实现方法     

  截图1.点击call按钮弹出提示框 

android中关于call拨号功能的实现方法

  截图2.点击确认按钮直接跳转至通话界面

 android中关于call拨号功能的实现方法

  截图3.点击dial按钮进入拨号界面    

总结

以上所述是小编给大家介绍的android中关于call拨号功能的实现方法,希望对大家有所帮助,如果大家有任何疑问欢迎给我留言,小编会及时回复大家的!

原文链接:https://www.cnblogs.com/coroner/archive/2019/05/11/10849475.html

延伸 · 阅读

精彩推荐
  • Android谷歌被屏蔽后如何搭建安卓环境

    谷歌被屏蔽后如何搭建安卓环境

    从5月27日开始,谷歌(Google)在华的几乎所有的服务都处于无法使用的状态,除了搜索引擎遭到屏蔽之外,谷歌的邮箱(Gmail)、日历(Calendar)、翻译(...

    Android开发网7972021-03-02
  • Android简介Android 中的AsyncTask

    简介Android 中的AsyncTask

    AsyncTask是一个很常用的API,尤其异步处理数据并将数据应用到视图的操作场合。接下来通过本文给大家介绍Android 中的AsyncTask,感兴趣的朋友一起学习吧...

    Devin Zhang4982021-06-23
  • AndroidAndroid实现图片文字轮播特效

    Android实现图片文字轮播特效

    这篇文章主要介绍了Android图片文字轮播效果,分别实现图片和文字自动滚动,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...

    xu佳佳8682021-05-06
  • Androidandroid 传感器(OnSensorChanged)使用介绍

    android 传感器(OnSensorChanged)使用介绍

    当传感器的值发生变化时,例如磁阻传感器方向改变时会调用OnSensorChanged(). 当传感器的精度发生变化时会调用OnAccuracyChanged()方法...

    Android开发网6752021-03-11
  • AndroidAndroid中判断网络连接状态的方法

    Android中判断网络连接状态的方法

    App判断用户是否联网是很普遍的需求,这篇文章主要介绍了Android中判断网络连接状态的方法,感兴趣的朋友可以参考一下...

    Android开发网11402021-05-27
  • AndroidAndroid build.gradle版本名打包配置的方法

    Android build.gradle版本名打包配置的方法

    这篇文章主要介绍了Android build.gradle版本名打包配置的方法,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧...

    淡淡_孩子气10762022-09-23
  • AndroidAndroid反编译代码和防止反编译

    Android反编译代码和防止反编译

    这篇文章主要介绍了Android反编译代码和防止反编译的相关资料,需要的朋友可以参考下...

    Android开发网6762021-03-27
  • Androidandroid通过代码的形式来实现应用程序的方法

    android通过代码的形式来实现应用程序的方法

    因为应用程序的安装与卸载模块在android系统中已经写好了,所以我们只需要激活就行了...

    Android开发网9612021-02-05