網站首頁 編程語言 正文
1.Android 動態申請權限
首先,需要在AndroidManifest.xml靜態申請電話權限,否則無法動態申請權限,以下代碼位置不能放錯(在application之外):
<uses-permission android:name="android.permission.CALL_PHONE" />
然后,在java代碼中寫動態申請權限的邏輯(這是官網的寫法)
public void requestPower() { //判斷是否已經賦予權限 if (ContextCompat.checkSelfPermission(this, Manifest.permission.上面電話權限字符) != PackageManager.PERMISSION_GRANTED) { //如果應用之前請求過此權限但用戶拒絕了請求,此方法將返回 true。 if (ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.上面電話權限字符)) {//這里可以寫個對話框之類的項向用戶解釋為什么要申請權限,并在對話框的確認鍵后續再次申請權限.它在用戶選擇"不再詢問"的情況下返回false } else { //申請權限,字符串數組內是一個或多個要申請的權限,1是申請權限結果的返回參數,在onRequestPermissionsResult可以得知申請結果 ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.上面電話字符,}, 1); } } }
示例圖:
2.創建一個AIDL文件ITelephony包名必須是(com.android.internal.telephony)
// ITelephony.aidl package com.android.internal.telephony; // Declare any non-default types here with import statements interface ITelephony { boolean endCall(); void answerRingingCall(); }
示例圖:
3.寫java代碼實現掛斷電話
private void endCall() { try { //通過反射得到隱藏的API,并獲得隱藏類的Class對象 Class<?> clazz = Class.forName("android.os.ServiceManager"); //獲取 Method類對象(方法對象) Method method = clazz.getMethod("getService", String.class); //調用方法獲到iBinder對象 IBinder ibinder = (IBinder) method.invoke(null, Context.TELEPHONY_SERVICE); ITelephony iTelephony = ITelephony.Stub.asInterface(ibinder); iTelephony.endCall();//結束電話 } catch (Exception e) { e.printStackTrace(); } }
記得一定要動態添加電話權限哦!不然掛電話這個操作實現不了。
總結
原文鏈接:https://blog.csdn.net/liujibo520/article/details/122983914
相關推薦
- 2022-10-01 Go編譯原理之函數內聯_Golang
- 2022-11-20 python合并多個excel的詳細過程_python
- 2022-07-04 C#實現中文日歷Calendar_C#教程
- 2022-09-17 C++中String類型的逆序方式_C 語言
- 2022-02-19 Android~日志工具Log
- 2022-11-22 ubuntu20.04中vscode使用ROS的詳細方法_C 語言
- 2024-03-06 SpringBoot 項目 批量刪除的操作
- 2023-05-19 python?txt中的文件,逐行讀取并且每行賦值給變量問題_python
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支