I have created a small application in android which records incoming calls, its working fine, it records the call but after recording the application crashes.I get the message that Unfortunately application has stopped
here below is my code.
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();
}
}
}
}
Here is the edited code:
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();
}
}
}
}