我在android中创建了一个小应用程序来记录传入的调用,它工作正常,它记录了调用但是在记录应用程序崩溃之后。我得到了令人遗憾的应用已经停止的消息
下面是我的代码。
public class CallReceiver extends BroadcastReceiver
{
MediaRecorder recorder;
File audiofile;
String name, phonenumber;
String audio_format;
public String Audio_Type;
int audioSource;
Context context;
private Handler handler;
Timer timer;
Boolean offHook = false, ringing = false;
Toast toast;
Boolean isOffHook = false;
public static boolean recordstarted = false;
public static boolean wasRinging = false;
private static final String ACTION_IN = "android.intent.action.PHONE_STATE";
private static final String ACTION_OUT = "android.intent.action.NEW_OUTGOING_CALL";
Bundle bundle;
String state;
String inCall, outCall;
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(ACTION_IN)) {
if ((bundle = intent.getExtras()) != null) {
state = bundle.getString(TelephonyManager.EXTRA_STATE);
if (state.equals(TelephonyManager.EXTRA_STATE_RINGING)) {
inCall = bundle.getString(TelephonyManager.EXTRA_INCOMING_NUMBER);
wasRinging = true;
Toast.makeText(context, "IN : " + inCall, Toast.LENGTH_LONG).show();
} else if (state.equals(TelephonyManager.EXTRA_STATE_OFFHOOK)) {
if (wasRinging == true) {
Toast.makeText(context, "ANSWERED", Toast.LENGTH_LONG).show();
File sampleDir = new File(Environment.getExternalStorageDirectory(), "/TestRecordingDasa");
if (!sampleDir.exists()) {
sampleDir.mkdirs();
}
String file_name = "Rec";
try {
audiofile = File.createTempFile(file_name, ".amr", sampleDir);
} catch (IOException e) {
e.printStackTrace();
}
String path = Environment.getExternalStorageDirectory().getAbsolutePath();
recorder = new MediaRecorder();
recorder.setAudioSource(MediaRecorder.AudioSource.VOICE_COMMUNICATION);
recorder.setOutputFormat(MediaRecorder.OutputFormat.AMR_NB);
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
recorder.setOutputFile(audiofile.getAbsolutePath());
try {
recorder.prepare();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
recordstarted = true;
recorder.start();
}
} else if (state.equals(TelephonyManager.EXTRA_STATE_IDLE)) {
wasRinging = false;
Toast.makeText(context, "REJECT || DISCO", Toast.LENGTH_LONG).show();
if (recordstarted) {
recorder.stop();
recordstarted = false;
}
}
}
} else if (intent.getAction().equals(ACTION_OUT)) {
if ((bundle = intent.getExtras()) != null) {
outCall = intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER);
Toast.makeText(context, "OUT : " + outCall, Toast.LENGTH_LONG).show();
}
}
}
}
答案 0 :(得分:1)
以下是编辑过的代码:
package com.example.callreceiver;
import java.io.File;
import java.io.IOException;
import java.util.Timer;
import java.util.logging.Handler;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.media.MediaRecorder;
import android.os.Bundle;
import android.os.Environment;
import android.telephony.TelephonyManager;
import android.util.Log;
import android.widget.Toast;
public class CallReceiver extends BroadcastReceiver {
private static MediaRecorder recorder;
private File audiofile;
private String name, phonenumber;
private String audio_format;
private String Audio_Type;
private int audioSource;
private Context context;
private Handler handler;
private Timer timer;
private Boolean offHook = false, ringing = false;
private Toast toast;
private Boolean isOffHook = false;
private static boolean recordstarted = false;
private static boolean wasRinging = false;
private static final String ACTION_IN = "android.intent.action.PHONE_STATE";
private static final String ACTION_OUT = "android.intent.action.NEW_OUTGOING_CALL";
private Bundle bundle;
private String state;
private String inCall, outCall;
@Override
public void onReceive(Context context, Intent intent) {
Log.i("onReceive", "Receive successfully");
if (intent.getAction().equals(ACTION_IN)) {
Log.i("onReceive", "ACTION IN");
if ((bundle = intent.getExtras()) != null) {
Log.i("onReceive", "Bundle != NULL");
state = bundle.getString(TelephonyManager.EXTRA_STATE);
Log.i("onReceive", "state:"+state);
if (state.equals(TelephonyManager.EXTRA_STATE_RINGING)) {
Log.i("onReceive", "Phone ringing");
inCall = bundle
.getString(TelephonyManager.EXTRA_INCOMING_NUMBER);
wasRinging = true;
Toast.makeText(context, "IN : " + inCall, Toast.LENGTH_LONG)
.show();
} else if (state.equals(TelephonyManager.EXTRA_STATE_OFFHOOK)) {
Log.i("onReceive", "Phone off hook ");
if (wasRinging == true) {
Log.i("onReceive", "Phone was ringing");
Toast.makeText(context, "ANSWERED", Toast.LENGTH_LONG)
.show();
File sampleDir = new File(
Environment.getExternalStorageDirectory(),
"/TestRecordingDasa");
if (!sampleDir.exists()) {
sampleDir.mkdirs();
}
String file_name = "Rec";
try {
audiofile = File.createTempFile(file_name, ".amr",
sampleDir);
} catch (IOException e) {
e.printStackTrace();
}
String path = Environment.getExternalStorageDirectory()
.getAbsolutePath();
recorder = new MediaRecorder();
recorder.setAudioSource(MediaRecorder.AudioSource.VOICE_COMMUNICATION);
recorder.setOutputFormat(MediaRecorder.OutputFormat.AMR_NB);
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
recorder.setOutputFile(audiofile.getAbsolutePath());
try {
recorder.prepare();
Log.i("onReceive", "Prepared");
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
recordstarted = true;
Log.i("onReceive", "Start recording");
recorder.start();
}
} else if (state.equals(TelephonyManager.EXTRA_STATE_IDLE)) {
Log.i("onReceive", "State idle");
wasRinging = false;
Toast.makeText(context, "REJECT || DISCO",
Toast.LENGTH_LONG).show();
if (recordstarted) {
recorder.stop();
recorder.release();
Log.i("onReceive", "Record Stopped");
recordstarted = false;
}
}
}
} else if (intent.getAction().equals(ACTION_OUT)) {
Log.i("onReceive", "ACTION_OUT");
if ((bundle = intent.getExtras()) != null) {
outCall = intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER);
Toast.makeText(context, "OUT : " + outCall, Toast.LENGTH_LONG)
.show();
}
}
}
}
答案 1 :(得分:0)
it records the call but after recording the application crashes.
I get the message that Unfortunately application has stopped
在你的代码中,当它被摘机状态时它开始录制但是它会阻止“recorder.start();”上的函数。当空闲状态广播时,它会崩溃。
所以,你可以做两件事:
- >将您的录制代码开始和停止在不同的线程中并相应处理。
或者
- >提供服务,开始和停止录制代码。当这些呼叫状态空闲时,你可以发送广播到服务,摘机然后开始并相应地开始和停止记录。
答案 2 :(得分:0)
您已在if else部分recorder = new MediaRecorder();
中创建了一个实例,试图将此声明放在一边&让它成为全球,这可能会解决你的问题。
我遇到了同样的问题。
问题是你在if语句中实例化了一个新的MediaRecorder,它掩盖了MediaRecorder mRecorder类成员。所以改变: