zoukankan      html  css  js  c++  java
  • android打电话方法(直接拨通)

    新建了CallPhone方法,如下:

     1 private void CallPhone() {
     2         String number = et_number.getText().toString();
     3         if (TextUtils.isEmpty(number)) {
     4             // 提醒用户
     5             Toast.makeText(MainActivity.this, "我叫吐司,请填写号码,行不行!!!", Toast.LENGTH_SHORT).show();
     6         } else {
     7             // 拨号:激活系统的拨号组件
     8             Intent intent = new Intent(); // 意图对象:动作 + 数据
     9             intent.setAction(Intent.ACTION_CALL); // 设置动作
    10             Uri data = Uri.parse("tel:" + number); // 设置数据
    11             intent.setData(data);
    12             startActivity(intent); // 激活Activity组件
    13         }
    14     }

    要记得在清单文件AndroidManifest.xml中添加权限

     1 <?xml version="1.0" encoding="utf-8"?>
     2 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
     3     package="***">
     4 
     5     <application
     6         ......
     7     </application>
     8     <uses-permission android:name="android.permission.CALL_PHONE"/>
     9 
    10 </manifest>   

    为方便理解,给出activyti_main.xml:

     1 <?xml version="1.0" encoding="utf-8"?>
     2 <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
     3     xmlns:app="http://schemas.android.com/apk/res-auto"
     4     xmlns:tools="http://schemas.android.com/tools"
     5     android:layout_width="match_parent"
     6     android:layout_height="match_parent"
     7     tools:context=".MainActivity">
     8 
     9     <LinearLayout
    10         android:layout_width="match_parent"
    11         android:layout_height="match_parent"
    12         android:orientation="vertical">
    13 
    14         <EditText
    15             android:id="@+id/ed"
    16             android:layout_width="match_parent"
    17             android:layout_height="wrap_content"
    18             android:hint="请输入手机号" />
    19 
    20         <Button
    21             android:id="@+id/button"
    22             android:layout_width="wrap_content"
    23             android:layout_height="wrap_content"
    24             android:text="拨打" />
    25     </LinearLayout>
    26 
    27 
    28 </android.support.constraint.ConstraintLayout>

    在onCreate()中找到控件,给南牛添加点击事件,调用此方法即可。

    顺便写一下从网上找到的android6.0动态获取权限的代码(亲测可用):

     1 // 检查是否获得了权限(Android6.0运行时权限)
     2                 //权限没有获得
     3                 if (ContextCompat.checkSelfPermission(MainActivity.this,
     4                         Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED){
     5                     // 没有获得授权,申请授权
     6                     if (ActivityCompat.shouldShowRequestPermissionRationale(MainActivity.this,
     7                             Manifest.permission.CALL_PHONE)) {
     8                         // 返回值:
     9 //                          如果app之前请求过该权限,被用户拒绝, 这个方法就会返回true.
    10 //                          如果用户之前拒绝权限的时候勾选了对话框中”Don’t ask again”的选项,那么这个方法会返回false.
    11 //                          如果设备策略禁止应用拥有这条权限, 这个方法也返回false.
    12                         // 弹窗需要解释为何需要该权限,再次请求授权
    13                         Toast.makeText(MainActivity.this, "请授权!", Toast.LENGTH_LONG).show();
    14 
    15                         // 帮跳转到该应用的设置界面,让用户手动授权
    16                         Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
    17                         Uri uri = Uri.fromParts("package", getPackageName(), null);
    18                         intent.setData(uri);
    19                         startActivity(intent);
    20                     }else{
    21                         // 不需要解释为何需要该权限,直接请求授权
    22                         ActivityCompat.requestPermissions(MainActivity.this,
    23                                 new String[]{Manifest.permission.CALL_PHONE},
    24                                 MY_PERMISSIONS_REQUEST_CALL_PHONE);
    25                     }
    26                 }else {
    27                     // 已经获得授权,可以打电话
    28                     CallPhone();
    29                 }    
    昔日我曾苍老,如今风华正茂(ง •̀_•́)ง
  • 相关阅读:
    JS 获取鼠标位置
    Asp 2.0动态加载用户控件(Ascx)
    水晶报表学习之三(参数传递问题)
    C#中分割字符串的几种方法
    StringUtils类使用
    页面自动刷新,HTML代码,呵呵,可以去刷新人气拉!
    水晶报表预览时有线条,打印的时候无线条,这个该怎么处理?
    Online、Interline的意思。
    linux下的g++编译【转载】
    c++中const与指针总结
  • 原文地址:https://www.cnblogs.com/lgqrlchinese/p/9884439.html
Copyright © 2011-2022 走看看