由于Android在更新日志中屏蔽了通话录音,目前没有官方支持的API来记录所有的呼入和呼出电话。然而,你可以使用TelephonyManager和BroadcastReceiver来监听电话状态,并在电话接通和挂断的时候记录相关信息。
下面是一个简单的示例代码:
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.telephony.TelephonyManager;
import android.util.Log;
public class PhoneStateReceiver extends BroadcastReceiver {
private static final String TAG = "PhoneStateReceiver";
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (action != null && action.equals(Intent.ACTION_NEW_OUTGOING_CALL)) {
// 获取拨打的电话号码
String phoneNumber = intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER);
Log.d(TAG, "拨打电话: " + phoneNumber);
} else {
// 通过TelephonyManager获取电话状态
TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
if (telephonyManager != null) {
int phoneState = telephonyManager.getCallState();
switch (phoneState) {
case TelephonyManager.CALL_STATE_RINGING:
// 获取来电号码
String incomingNumber = intent.getStringExtra(TelephonyManager.EXTRA_INCOMING_NUMBER);
Log.d(TAG, "来电: " + incomingNumber);
break;
case TelephonyManager.CALL_STATE_IDLE:
// 电话挂断
Log.d(TAG, "电话挂断");
break;
}
}
}
}
}
import android.Manifest;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.pm.PackageManager;
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
public class MainActivity extends AppCompatActivity {
private static final int REQUEST_PERMISSION = 1;
private PhoneStateReceiver phoneStateReceiver;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 动态请求权限
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.READ_PHONE_STATE)
!= PackageManager.PERMISSION_GRANTED ||
ActivityCompat.checkSelfPermission(this, Manifest.permission.PROCESS_OUTGOING_CALLS)
!= PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this,
new String[]{Manifest.permission.READ_PHONE_STATE, Manifest.permission.PROCESS_OUTGOING_CALLS},
REQUEST_PERMISSION);
} else {
registerPhoneStateReceiver();
}
}
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (requestCode == REQUEST_PERMISSION) {
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
registerPhoneStateReceiver();
} else {
// 权限被拒绝
}
}
}
private void registerPhoneStateReceiver() {
phoneStateReceiver = new PhoneStateReceiver();
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(TelephonyManager.ACTION_PHONE_STATE_CHANGED);
intentFilter.addAction(Intent.ACTION_NEW_OUTGOING_CALL);
registerReceiver(phoneStateReceiver, intentFilter);
}
@Override
protected void onDestroy() {
super.onDestroy();
if (phoneStateReceiver != null) {
unregisterReceiver(phoneStateReceiver);
}
}
}
这个示例代码中,通过TelephonyManager和BroadcastReceiver来监听电话状态。在电话接通和挂断的时候,会在Logcat中打印相关信息。你可以根据自己的需求,将相关信息保存到数据库或文件中。
请注意,由于Android系统的限制,无法获取对方的通话内容。这个解决方案仅能记录电话的接通和挂断事件。