我在android中创建了一个Call Recorder应用程序。唯一的问题是它从我按下呼叫按钮的那一刻开始记录呼叫,虽然它需要一些时间来连接呼叫并且有更多时间来开始呼叫。我希望它从收到呼叫的那一刻开始录制,而不是从呼叫振铃的地方开始录制。 OFF_HOOK
也未显示正确的结果。当我按下通话按钮时,会调用OFF_HOOK
。
public void onReceive(Context context, Intent intent) {
// SharedPreferences settings = context.getSharedPreferences(
// LISTEN_ENABLED, 0);
// boolean silent = settings.getBoolean("silentMode", true);
phoneNumber = intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER);
Toast.makeText(context, phoneNumber + "RAVI", Toast.LENGTH_LONG).show();
if (phoneNumber == null) {
String state = intent.getStringExtra(TelephonyManager.EXTRA_STATE);
if (state.equals(TelephonyManager.EXTRA_STATE_OFFHOOK)) {
Intent myIntent = new Intent(context, CallRecordService.class);
myIntent.putExtra("commandType", STATE_CALL_START);
myIntent.putExtra("phoneNumber", phoneNumber);
context.startService(myIntent);
} else if (state.equals(TelephonyManager.EXTRA_STATE_IDLE)) {
Intent myIntent = new Intent(context, CallRecordService.class);
myIntent.putExtra("commandType", STATE_CALL_END);
context.startService(myIntent);
} else if (state.equals(TelephonyManager.EXTRA_STATE_RINGING)) {
phoneNumber = intent
.getStringExtra(TelephonyManager.EXTRA_INCOMING_NUMBER);
Intent myIntent = new Intent(context, CallRecordService.class);
myIntent.putExtra("commandType", STATE_INCOMING_NUMBER);
myIntent.putExtra("phoneNumber", phoneNumber);
context.startService(myIntent);
}
} else {
Intent myIntent = new Intent(context, CallRecordService.class);
myIntent.putExtra("commandType",
TelephonyManager.EXTRA_INCOMING_NUMBER);
myIntent.putExtra("phoneNumber", phoneNumber);
context.startService(myIntent);
}
}
另一个CallRecordService类:
public int onStartCommand(Intent intent, int flags, int startId) {
int commandType = 0;
commandType = intent.getIntExtra("commandType", STATE_CALL_START);
Toast.makeText(this, commandType + "::", Toast.LENGTH_SHORT).show();
if (commandType == STATE_INCOMING_NUMBER) {
if (phoneNumber == null)
phoneNumber = intent.getStringExtra("phoneNumber");
} else if (commandType == STATE_CALL_START) {
if (phoneNumber == null)
phoneNumber = intent.getStringExtra("phoneNumber");
try {
recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
myFileName = getFilePath();
recorder.setOutputFile(myFileName);
} catch (IllegalStateException e) {
Log.e("Call recorder IllegalStateException: ", "");
terminateAndEraseFile(1);
} catch (Exception e) {
Log.e("Call recorder Exception: ", "");
terminateAndEraseFile(2);
}
OnErrorListener errorListener = new OnErrorListener() {
public void onError(MediaRecorder arg0, int arg1, int arg2) {
Log.e("Call recorder OnErrorListener: ", arg1 + "," + arg2);
arg0.stop();
arg0.reset();
arg0.release();
arg0 = null;
terminateAndEraseFile(3);
}
};
recorder.setOnErrorListener(errorListener);
OnInfoListener infoListener = new OnInfoListener() {
public void onInfo(MediaRecorder arg0, int arg1, int arg2) {
Log.e("Call recorder OnInfoListener: ", arg1 + "," + arg2);
arg0.stop();
arg0.reset();
arg0.release();
arg0 = null;
terminateAndEraseFile(4);
}
};
recorder.setOnInfoListener(infoListener);
try {
recorder.prepare();
recorder.start();
Toast toast = Toast.makeText(this, "Receive Start Call",
Toast.LENGTH_SHORT);
toast.show();
manger = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = new Notification(R.drawable.ic_cl5,
"Call Recording", System.currentTimeMillis());
notification.flags = Notification.FLAG_NO_CLEAR;
Intent intent2 = new Intent(this, MainActivity.class);
intent2.putExtra("RecordStatus", true);
PendingIntent contentIntent = PendingIntent.getActivity(
getBaseContext(), 0, intent2, 0);
notification.setLatestEventInfo(this, "Notification Title",
"Notification Text", contentIntent);
// manger.notify(0, notification);
startForeground(1337, notification);
} catch (IllegalStateException e) {
Log.e("Call recorder IllegalStateException: ", "");
terminateAndEraseFile(5);
e.printStackTrace();
} catch (IOException e) {
Log.e("Call recorder IOException: ", "");
terminateAndEraseFile(6);
e.printStackTrace();
} catch (Exception e) {
Log.e("Call recorder Exception: ", "");
terminateAndEraseFile(7);
e.printStackTrace();
}
} else if (commandType == STATE_CALL_END) {
try {
recorder.stop();
recorder.reset();
recorder.release();
recorder = null;
Toast.makeText(this, "Receiver End Call", Toast.LENGTH_SHORT)
.show();
} catch (IllegalStateException e) {
e.printStackTrace();
}
if (manger != null)
manger.cancel(0);
stopForeground(true);
this.stopSelf();
}
return super.onStartCommand(intent, flags, startId);
}
但它从开始而不是从实际开始的地方开始记录。
我该如何解决这个问题?
答案 0 :(得分:0)
您只能检测到电话何时开始拨打电话以及何时拨打电话,但您无法确定何时拨打电话"开始了。
唯一的解决方法是在拨出电话开始后开始录制并通过几秒钟,但是,您再也不能保证手机会响铃:
public class PhoneCallListener extends PhoneStateListener {
int lastState = TelephonyManager.CALL_STATE_IDLE;
boolean isIncoming;
//Incoming call- IDLE to RINGING when it rings, to OFFHOOK when it's answered, to IDLE when hung up
//Outgoing call- from IDLE to OFFHOOK when dialed out, to IDLE when hunged up
@Override
public void onCallStateChanged(int state, String incomingNumber) {
super.onCallStateChanged(state, incomingNumber);
if(lastState == state){
//No change
return;
}
switch (state) {
case TelephonyManager.CALL_STATE_RINGING:
isIncoming = true;
//incoming call started
break;
case TelephonyManager.CALL_STATE_OFFHOOK:
//Transition of ringing->offhook are pickups of incoming calls. Nothing down on them
if(lastState != TelephonyManager.CALL_STATE_RINGING){
isIncoming = false;
//outgoing call started
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
//startRecording
}
}, 7000);
}
break;
case TelephonyManager.CALL_STATE_IDLE:
//End of call(Idle). The type depends on the previous state(s)
if(lastState == TelephonyManager.CALL_STATE_RINGING){
// missed call
}
else if(isIncoming){
//incoming call ended
}
else{
//outgoing call ended
}
break;
}
lastState = state;
}
}
}