最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

android中關(guān)于call撥號(hào)功能的實(shí)現(xiàn)方法

 更新時(shí)間:2019年05月12日 10:25:47   作者:HyperMan  
這篇文章主要介紹了android中關(guān)于call撥號(hào)功能實(shí)現(xiàn)的記錄,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下

 前幾天考試居然記錯(cuò)dial和call,故在此寫(xiě)上小demo來(lái)作區(qū)別,加深印象。

主要是實(shí)現(xiàn)call(撥通電話)功能,dial(撥電話)功能用作對(duì)比,話不多說(shuō),貼上代碼。

1.創(chuàng)建布局文件如下:

<?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>

也就是添加了兩個(gè)按鈕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按鈕點(diǎn)擊事件中添加了單獨(dú)的方法來(lái)進(jìn)行處理,這是因?yàn)镃ALL_PHONE在Android 6.0及以上版本被認(rèn)為是危險(xiǎn)權(quán)限,需要在程序運(yùn)行時(shí)申請(qǐng)。

•關(guān)于Android中權(quán)限的分類請(qǐng)參考以下鏈接:

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

 3.添加Manifest.xml文件代碼:

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

千萬(wàn)不要忘記在AndroidManifest.xml中添加上權(quán)限申明哦:)

 實(shí)現(xiàn)效果截圖:

       

  截圖1.點(diǎn)擊CALL按鈕彈出提示框 

  截圖2.點(diǎn)擊確認(rèn)按鈕直接跳轉(zhuǎn)至通話界面

 

  截圖3.點(diǎn)擊DIAL按鈕進(jìn)入撥號(hào)界面    

總結(jié)

以上所述是小編給大家介紹的android中關(guān)于call撥號(hào)功能的實(shí)現(xiàn)方法,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)歡迎給我留言,小編會(huì)及時(shí)回復(fù)大家的!

相關(guān)文章

最新評(píng)論

开阳县| 德惠市| 青冈县| 翼城县| 吉木萨尔县| 芜湖县| 奇台县| 连云港市| 郴州市| 潞西市| 故城县| 宣城市| 金川县| 墨江| 荆门市| 凤山县| 耿马| 繁峙县| 平定县| 桐城市| 台中县| 弥渡县| 会同县| 德清县| 公主岭市| 永年县| 峨眉山市| 建瓯市| 叶城县| 潮州市| 西城区| 枣阳市| 杭锦后旗| 株洲县| 白城市| 梁平县| 平潭县| 五常市| 恩平市| 芷江| 开化县|