本文章主要介绍安卓检查通话自动录音功能是否开启,并跳转到相应的开启页面,主要介绍小米,华为、vivo和oppo手机,其他手机暂不做介绍
/**
* 检查小米手机自动录音功能是否开启,true已开启 false未开启
*
* @return
*/
private boolean checkXiaomiRecord() throws Settings.SettingNotFoundException {
int key = Settings.System.getInt(mContext.getContentResolver(), "button_auto_record_call");
// XLog.d(TAG, "Xiaomi key:" + key);
//0是未开启,1是开启
return key != 0;
}
/**
* 检查OPPO手机自动录音功能是否开启,true已开启 false未开启
*
* @return
*/
private boolean checkOppoRecord() throws Settings.SettingNotFoundException {
int key = Settings.Global.getInt(mContext.getContentResolver(), "oplus_customize_all_call_audio_record");
// int key = Settings.Global.getInt(mContext.getContentResolver(), "oppo_all_call_audio_record");
// XLog.d(TAG, "Oppo key:" + key);
//0代表OPPO自动录音未开启,1代表OPPO自动录音已开启
return key != 0;
}
/**
* 检查VIVO自动录音功能是否开启,true已开启 false未开启
*
* @return
*/
private boolean checkVivoRecord() throws Settings.SettingNotFoundException {
int key = Settings.Global.getInt(mContext.getContentResolver(), "call_record_state_global");
// XLog.d(TAG, "Vivo key:" + key);
//0代表VIVO自动录音未开启,1代表VIVO所有通话自动录音已开启,2代表指定号码自动录音
return key == 1;
}
/**
* 检查华为手机自动录音功能是否开启,true已开启 false未开启
*
* @return
*/
private boolean checkHuaweiRecord() throws Settings.SettingNotFoundException {
int key = Settings.Secure.getInt(mContext.getContentResolver(), "enable_record_auto_key");
// XLog.d(TAG, "Huawei key:" + key);
//0代表华为自动录音未开启,1代表华为自动录音已开启
return key != 0;
}
/**
* 跳转到小米开启通话自动录音功能页面
*/
private void startXiaomiRecord() {
ComponentName componentName = new ComponentName("com.android.phone", "com.android.phone.settings.CallRecordSetting");
Intent intent = new Intent();
intent.setComponent(componentName);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
mContext.startActivity(intent);
}
/**
* 跳转到OPPO开启通话自动录音功能页面
*/
private void startOppoRecord() {
ComponentName componentName = new ComponentName("com.android.phone", "com.android.phone.OplusCallFeaturesSetting");
Intent intent = new Intent();
intent.setComponent(componentName);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
mContext.startActivity(intent);
}
/**
* 跳转到VIVO开启通话自动录音功能页面
*/
private void startVivoRecord() {
ComponentName componentName = new ComponentName("com.android.incallui", "com.android.incallui.record.CallRecordSetting");
Intent intent = new Intent();
intent.setComponent(componentName);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
mContext.startActivity(intent);
}
/**
* 跳转到华为开启通话自动录音功能页面
*/
private void startHuaweiRecord() {
ComponentName componentName = new ComponentName("com.android.phone", "com.android.phone.MSimCallFeaturesSetting");
// ComponentName componentName = new ComponentName("com.android.phone", "com.android.phone.recorder");
Intent intent = new Intent();
intent.setComponent(componentName);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
mContext.startActivity(intent);
}
作者通过上面的例子做了一个开箱即用的工具包,代码复制过去直接使用即可,代码如下:
package com.outsource.uni.phone.service;
import android.annotation.SuppressLint;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Build;
import android.os.Environment;
import android.provider.Settings;
import com.alibaba.fastjson.JSONObject;
import com.outsource.uni.phone.utils.FileUtil;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
public class PhoneUtils {
private static PhoneUtils instance;
private static Context mContext;
public PhoneUtils(Context context) {
mContext = context;
}
public static synchronized PhoneUtils getInstance(Context context) {
mContext = context;
if (instance == null) {
instance = new PhoneUtils(context);
}
return instance;
}
/**
* 判断是否开启通话自动录音功能
*/
public boolean checkIsAutoRecord() throws Settings.SettingNotFoundException {
boolean isOpen = false;
String manufacturer = Build.MANUFACTURER.toLowerCase();
if (manufacturer.contains("huawei")) {
isOpen = checkHuaweiRecord();
} else if (manufacturer.contains("xiaomi")) {
isOpen = checkXiaomiRecord();
} else if (manufacturer.contains("oppo")) {
isOpen = checkOppoRecord();
} else if (manufacturer.contains("vivo")) {
isOpen = checkVivoRecord();
}
return isOpen;
}
/**
* 跳转到通话自动录音页面
*/
public void toCallAutoRecorderPage() {
String manufacturer = Build.MANUFACTURER.toLowerCase();
if (manufacturer.contains("huawei")) {
startHuaweiRecord();
} else if (manufacturer.contains("xiaomi")) {
startXiaomiRecord();
} else if (manufacturer.contains("oppo")) {
startOppoRecord();
} else if (manufacturer.contains("vivo")) {
startVivoRecord();
}
}
/**
* 获取所有录音文件
*/
public List<String> getAllRecorderFiles() {
//获取录音文件夹
File recorderDirFile = getRecorderDirFile();
return FileUtil.getAllFiles(recorderDirFile, false);
}
/**
* 获取设置的key和value
*/
public JSONObject getSettingsKeyValue() {
JSONObject object = new JSONObject();
List<String> systemKeyValue = getSettingsKeyValue(Settings.System.CONTENT_URI);
List<String> secureKeyValue = getSettingsKeyValue(Settings.Secure.CONTENT_URI);
List<String> globalKeyValue = getSettingsKeyValue(Settings.Global.CONTENT_URI);
object.put("system", systemKeyValue);
object.put("secure", secureKeyValue);
object.put("global", globalKeyValue);
return object;
}
/**
* 搜索录音文件
*/
public File searchRecorderFile(String number) {
File audioFile = null;
//获取录音文件夹
File recorderDirFile = getRecorderDirFile();
List<String> list = FileUtil.getAllFiles(recorderDirFile, false);
long thisTime = System.currentTimeMillis();
if (list.size() > 0) {
for (String path : list) {
System.out.println("文件路径:" + path);
File file = new File(path);
long lastModified = file.lastModified();
if (thisTime - lastModified <= 10000) {
audioFile = file;
System.out.println("退出循环");
break;
}
}
}
return audioFile;
}
/**
* 检查小米手机自动录音功能是否开启,true已开启 false未开启
*
* @return
*/
private boolean checkXiaomiRecord() throws Settings.SettingNotFoundException {
int key = Settings.System.getInt(mContext.getContentResolver(), "button_auto_record_call");
// XLog.d(TAG, "Xiaomi key:" + key);
//0是未开启,1是开启
return key != 0;
}
/**
* 检查OPPO手机自动录音功能是否开启,true已开启 false未开启
*
* @return
*/
private boolean checkOppoRecord() throws Settings.SettingNotFoundException {
int key = Settings.Global.getInt(mContext.getContentResolver(), "oplus_customize_all_call_audio_record");
// int key = Settings.Global.getInt(mContext.getContentResolver(), "oppo_all_call_audio_record");
// XLog.d(TAG, "Oppo key:" + key);
//0代表OPPO自动录音未开启,1代表OPPO自动录音已开启
return key != 0;
}
/**
* 检查VIVO自动录音功能是否开启,true已开启 false未开启
*
* @return
*/
private boolean checkVivoRecord() throws Settings.SettingNotFoundException {
int key = Settings.Global.getInt(mContext.getContentResolver(), "call_record_state_global");
// XLog.d(TAG, "Vivo key:" + key);
//0代表VIVO自动录音未开启,1代表VIVO所有通话自动录音已开启,2代表指定号码自动录音
return key == 1;
}
/**
* 检查华为手机自动录音功能是否开启,true已开启 false未开启
*
* @return
*/
private boolean checkHuaweiRecord() throws Settings.SettingNotFoundException {
int key = Settings.Secure.getInt(mContext.getContentResolver(), "enable_record_auto_key");
// XLog.d(TAG, "Huawei key:" + key);
//0代表华为自动录音未开启,1代表华为自动录音已开启
return key != 0;
}
/**
* 跳转到VIVO开启通话自动录音功能页面
*/
private void startVivoRecord() {
ComponentName componentName = new ComponentName("com.android.incallui", "com.android.incallui.record.CallRecordSetting");
Intent intent = new Intent();
intent.setComponent(componentName);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
mContext.startActivity(intent);
}
/**
* 跳转到小米开启通话自动录音功能页面
*/
private void startXiaomiRecord() {
ComponentName componentName = new ComponentName("com.android.phone", "com.android.phone.settings.CallRecordSetting");
Intent intent = new Intent();
intent.setComponent(componentName);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
mContext.startActivity(intent);
}
/**
* 跳转到华为开启通话自动录音功能页面
*/
private void startHuaweiRecord() {
ComponentName componentName = new ComponentName("com.android.phone", "com.android.phone.MSimCallFeaturesSetting");
// ComponentName componentName = new ComponentName("com.android.phone", "com.android.phone.recorder");
Intent intent = new Intent();
intent.setComponent(componentName);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
mContext.startActivity(intent);
}
/**
* 跳转到OPPO开启通话自动录音功能页面
*/
private void startOppoRecord() {
ComponentName componentName = new ComponentName("com.android.phone", "com.android.phone.OplusCallFeaturesSetting");
Intent intent = new Intent();
intent.setComponent(componentName);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
mContext.startActivity(intent);
}
/**
* 获取录音文件夹
*
* @return
*/
private File getRecorderDirFile() {
String manufacturer = Build.MANUFACTURER.toLowerCase();
String parentPath = Environment.getExternalStorageDirectory().getAbsolutePath();
File childFile = null;
if (manufacturer.contains("huawei")) {
if (FileUtil.isFileExist(parentPath + "/Sounds/CallRecord")) {
childFile = new File(parentPath + "/Sounds/CallRecord");
} else if (FileUtil.isFileExist(parentPath + "/record")) {
childFile = new File(parentPath + "record");
} else if (FileUtil.isFileExist(parentPath + "/Record")) {
childFile = new File(parentPath + "/Record");
} else {
childFile = new File("");
}
} else if (manufacturer.contains("xiaomi")) {
childFile = new File(parentPath + "/MIUI/sound_recorder/call_rec/");
} else if (manufacturer.contains("meizu")) {
childFile = new File(parentPath + "/Recorder");
} else if (manufacturer.contains("oppo")) {
childFile = new File(parentPath + "/Recordings");
} else if (manufacturer.contains("vivo")) {
if (FileUtil.isFileExist(parentPath + "/Recordings/Record/Call")) {
childFile = new File(parentPath + "/Recordings/Record/Call");
} else {
childFile = new File(parentPath + "/Record/Call");
}
} else {
childFile = new File("");
}
return childFile;
}
/**
* 获取key和value
*/
private List<String> getSettingsKeyValue(Uri uri){
List<String> list = new ArrayList<>();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
@SuppressLint("Recycle") Cursor cursor = mContext.getContentResolver().query(uri, null, null, null);
String[] columnNames = cursor.getColumnNames();
StringBuilder builder = new StringBuilder();
while (cursor.moveToNext()) {
for (String columnName : columnNames) {
@SuppressLint("Range") String string = cursor.getString(cursor.getColumnIndex(columnName));
try {
if(string.toLowerCase().contains("record") || string.toLowerCase().contains("call")){
builder.append(columnName).append(":").append(string).append("\n");
list.add(columnName + ":" + string);
}
}catch (Exception e){
e.printStackTrace();
}
}
}
}
return list;
}
}
package com.outsource.uni.phone.utils;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.os.Environment;
import android.util.Log;
import android.widget.Toast;
import com.alibaba.fastjson.JSONObject;
import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.LineNumberReader;
import java.io.OutputStream;
import java.io.Reader;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
public class FileUtil {
private static final String TAG = "FileUtil";
/**
* 判断SD卡上的文件是否存在
*/
public static boolean isFileExist(String fileName) {
File file = new File(fileName);
return file.exists();
}
/**
* searchFile 查找文件并加入到ArrayList 当中去
*
* @param context
* @param keyword
* @param filepath
* @return
*/
public static List<JSONObject> searchFile(Context context, String keyword, File filepath) {
List<JSONObject> list = new ArrayList<>();
JSONObject rowItem = null;
int index = 0;
// 判断SD卡是否存在
if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
File[] files = filepath.listFiles();
if (files != null && files.length > 0) {
for (File file : files) {
if (file.isDirectory()) {
if (file.getName().replace(" ", "").toLowerCase().contains(keyword)) {
rowItem = new JSONObject();
rowItem.put("number", index); // 加入序列号
rowItem.put("fileName", file.getName());// 加入名称
rowItem.put("path", file.getPath()); // 加入路径
rowItem.put("size", file.length() + ""); // 加入文件大小
list.add(rowItem);
}
// 如果目录可读就执行(一定要加,不然会挂掉)
if (file.canRead()) {
list.addAll(searchFile(context, keyword, file)); // 如果是目录,递归查找
}
} else {
// 判断是文件,则进行文件名判断
try {
if (file.getName().replace(" ", "").contains(keyword) || file.getName().replace(" ", "").contains(keyword.toUpperCase())) {
rowItem = new JSONObject();
rowItem.put("number", index); // 加入序列号
rowItem.put("fileName", file.getName());// 加入名称
rowItem.put("path", file.getPath()); // 加入路径
rowItem.put("size", file.length() + ""); // 加入文件大小
list.add(rowItem);
index++;
}
} catch (Exception e) {
// Toast.makeText(context, "查找发生错误!", Toast.LENGTH_SHORT).show();
e.printStackTrace();
}
}
}
}
}
return list;
}
/**
* 得到所有文件
*
* @param dir
* @return
*/
public static ArrayList<String> getAllFiles(File dir, boolean child) {
ArrayList<String> allFiles = new ArrayList<String>();
// 递归取得目录下的所有文件及文件夹
File[] files = dir.listFiles();
if (files == null) {
return allFiles;
}
for (int i = 0; i < Objects.requireNonNull(files).length; i++) {
File file = files[i];
// allFiles.add(file.getPath());
if (file.isDirectory() && child) {
if (file.canRead()) {
allFiles.addAll(getAllFiles(file, child));
}
} else {
allFiles.add(file.getPath());
}
}
return allFiles;
}
}