android中關於call撥號功能的實現方法

這篇文章主要介紹了android中關於call撥號功能實現的記錄,非常不錯,具有一定的參考借鑑價值,需要的朋友可以參考下

 前幾天考試居然記錯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撥號功能的實現方法,希望對大家有所幫助,如果大家有任何疑問歡迎給我留言,小編會及時回覆大家的!

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章