android中關于call撥號功能的實現(xiàn)方法
前幾天考試居然記錯dial和call,故在此寫上小demo來作區(qū)別,加深印象。
主要是實現(xiàn)call(撥通電話)功能,dial(撥電話)功能用作對比,話不多說,貼上代碼。
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>
也就是添加了兩個按鈕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中添加上權限申明哦:)
實現(xiàn)效果截圖:
截圖1.點擊CALL按鈕彈出提示框

截圖2.點擊確認按鈕直接跳轉至通話界面

截圖3.點擊DIAL按鈕進入撥號界面
總結
以上所述是小編給大家介紹的android中關于call撥號功能的實現(xiàn)方法,希望對大家有所幫助,如果大家有任何疑問歡迎給我留言,小編會及時回復大家的!
- Android 中 ActivityLifecycleCallbacks的實例詳解
- Android Call(打電話)的基本知識詳解
- Android定時器實現(xiàn)的幾種方式整理及removeCallbacks失效問題解決
- 基于Android CALL && SendMes Test的相關介紹
- Android Studio Intent隱式啟動,發(fā)短信,撥號,打電話,訪問網頁等實例代碼
- Android跳轉到系統(tǒng)聯(lián)系人及撥號或短信界面
- Android開發(fā)之電話撥號器實例詳解
- Android獲取手機通訊錄、sim卡聯(lián)系人及調用撥號界面方法
- Android 2.3 撥號上網流程從源碼角度進行分析
- android2.3.5 CDMA/EVDO撥號APN解決方案
- Android撥號盤 支持T9搜索和號碼搜索等撥號盤案例
相關文章
圖文詳解Android Studio搭建Android集成開發(fā)環(huán)境的過程
這篇文章主要以圖文的方式詳細介紹了Android Studio搭建Android集成開發(fā)環(huán)境的過程,文中安裝步驟介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2015-12-12
Android自定義View實現(xiàn)可以拖拽的GridView
這篇文章主要為大家詳細介紹了Android自定義View實現(xiàn)可以拖拽的GridView,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2016-06-06
Android抽屜導航Navigation Drawer實例解析
這篇文章主要為大家詳細介紹了Android抽屜導航NavigationDrawer實例,感興趣的小伙伴們可以參考一下2016-05-05
android 點擊EditText始終不彈出軟件鍵盤實現(xiàn)代碼
這篇文章主要介紹了android 點擊EditText始終不彈出軟件鍵盤實現(xiàn)代碼的相關資料,需要的朋友可以參考下2016-11-11

