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

所属分类: 软件编程 / Android 阅读数: 74
收藏 0 赞 0 分享

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

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

1.创建布局文件如下:

<?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代码:

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中添加上权限申明哦:)

 实现效果截图:

       

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

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

 

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

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

更多精彩内容其他人还在看

Android框架学习之Volley和Glide详解

这篇文章主要给大家介绍了关于Android框架学习之Volley和Glide的相关资料,文中通过示例代码介绍的非常详细,对各位Android开发者们具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

Android中Fragment的基本用法示例总结

Fragment是activity的界面中的一部分或一种行为,下面这篇文章主要给大家介绍了关于Android中Fragment的基本用法的相关资料,文中通过示例代码介绍的非常详细,需要的朋友可以参考下
收藏 0 赞 0 分享

Android.mk引入第三方jar包和so库文件的方法

这篇文章主要介绍了Android.mk引入第三方jar包和so库文件的方法,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

Android仿微信录制小视频

这篇文章主要为大家详细介绍了Android仿微信录制小视频,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

android实现一键锁屏和一键卸载的方法实例

这篇文章主要给大家介绍了关于android如何实现一键锁屏和一键卸载的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧。
收藏 0 赞 0 分享

Android手势密码--设置和校验功能的实现代码

这篇文章主要介绍了Android手势密码--设置和校验功能的实现代码,非常不错,具有一定的参考校验价值,需要的朋友可以参考下
收藏 0 赞 0 分享

Kotlin学习笔记之const val与val

这篇文章主要给大家介绍了关于Kotlin学习笔记之const val与val的相关资料,并给大家介绍了const val和val区别以及Kotlin中var和val的区别,需要的朋友可以参考借鉴,下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

Android实现调用系统分享功能示例的总结

这篇文章主要介绍了通过Android调用系统分享文本信息、单张图片、多个文件和指定分享到微信、QQ,同时分享图片和文字的功能示例,小编觉得挺不错,一起跟随小编过来看看吧
收藏 0 赞 0 分享

Android自定义view实现输入控件

这篇文章主要为大家详细介绍了Android自定义view实现输入控件,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Android使用Intent.ACTION_SEND分享图片和文字内容的示例代码

这篇文章主要介绍了Android使用Intent.ACTION_SEND分享图片和文字内容的示例代码的实例代码,具有很好的参考价值,希望对大家有所帮助,一起跟随小编过来看看吧
收藏 0 赞 0 分享
查看更多