在原生插件中写了一个广播用来监听接打电话,但是如何注册广播服务呢?在mainfest.json里面注册服务吗?如何注册,有没有朋友指教以下。
package io.dcloud.uniplugin;
import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.telephony.TelephonyManager;
import android.util.Log;
import java.io.File;
import java.util.ArrayList;
public class PhoneReceiver extends BroadcastReceiver {
private static final String TAG = PhoneReceiver.class.getSimpleName();
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(Intent.ACTION_NEW_OUTGOING_CALL)) {
// 如果是拨打电话
String phoneNumber = intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER);
String outNumber = this.getResultData();// 去电号码
Log.i(TAG, "call OUT 1:" + phoneNumber);
Log.i(TAG, "call OUT 2:" + outNumber);
//调用接口保存一条跟进记录
//去电手机号,也就是本机的手机号来查询是哪个招商人员,
// 然后生成一条跟进记录如:张小明 2023-01-19 给湖南辣椒(湖南辣椒是客户)拨打了电话
//防止线程阻塞
// new Thread(new Runnable() {
// @Override
// public void run() {
//执行上传跟进记录
getAllDataFileName("/storage/emulated/0/Music/",phoneNumber,outNumber);
// }
// }).start();
} else if ("android.intent.action.PHONE_STATE".equals(intent.getAction())) {
// 如果是来电
TelephonyManager tManager = (TelephonyManager) context
.getSystemService(Service.TELEPHONY_SERVICE);
String state = intent.getStringExtra(TelephonyManager.EXTRA_STATE);
// 来电号码
String mIncomingNumber = intent.getStringExtra(TelephonyManager.EXTRA_INCOMING_NUMBER);
Log.i(TAG, "call IN 1:" + state);
Log.i(TAG, "call IN 2:" + mIncomingNumber);
switch (tManager.getCallState()) {
case TelephonyManager.CALL_STATE_RINGING:
Log.d(TAG, "**********************监测到电话呼入!!!!*****");
//防止线程阻塞
new Thread(new Runnable() {
@Override
public void run() {
//执行上传跟进记录
}
}).start();
break;
case TelephonyManager.CALL_STATE_OFFHOOK:
Log.d(TAG, "**********************监测到接听电话!!!!************");
break;
case TelephonyManager.CALL_STATE_IDLE:
Log.d(TAG, "**********************监测到挂断电话!!!!*******************");
break;
}
}
}
private void setFileName(String fileName,String myPhone,String goPhone) {
String localPath = "/storage/emulated/0/Music/";
String time = fileName.split("_")[1].split("\\.")[0];
//currentFileName = currentFileName.substring(1);
//Log.i("Current file name", currentFileName);
File from = new File(localPath, fileName);//老文件
File to = new File(localPath, myPhone+"fc"+goPhone+"time"+time+".mp3");//新文件
from.renameTo(to);
Log.i("To path is", to.toString());
}
public ArrayList<String> getAllDataFileName(String folderPath,String myPhone,String goPhone) {
ArrayList<String> fileList = new ArrayList<>();
File file = new File(folderPath);
File[] tempList = file.listFiles();
for (int i = 0; i < tempList.length; i++) {
if (tempList[i].isFile()) {
//System.out.println("文 件:" + tempList[i].getName());
String fileName = tempList[i].getName();
if (fileName.contains("_")){ // 根据自己的需要进行类型筛选
//fileList.add(fileName);
setFileName(fileName,myPhone,goPhone);//改名之后再上传、、上传之后进行删除,
}
}
}
return fileList;
}
}
j***@qq.com (作者)
我是来寻求帮助的,你想卖插件最好别来这里,省的被人厌,还什么最好用原生插件来搞,我TM写的不是原生插件吗?
2023-05-20 09:18