我想在smsreciver上创建小型应用MediaRecord。当我记录消息录制开始和停止自动。我的问题是短信recive录音是开始但它永远不会停止下一个短信recive和我的录制文件成功创建但内部没有任何东西。请帮忙
public class BroadCast extends BroadcastReceiver {
private static final String SMS_RECEIVED = "android.provider.Telephony.SMS_RECEIVED";
private static final String TAG = "SMSBroadcastReceiver";
private String recivedKey;
private MediaRecorder recorder;
@Override
public void onReceive(Context context, Intent intent) {
Log.i(TAG, "Intent recieved: " + intent.getAction());
if (intent.getAction().equals(SMS_RECEIVED)) {
Bundle bundle = intent.getExtras();
if (bundle != null) {
Object[] pdus = (Object[]) bundle.get("pdus");
final SmsMessage[] messages = new SmsMessage[pdus.length];
for (int i = 0; i < pdus.length; i++) {
messages[i] = SmsMessage.createFromPdu((byte[]) pdus[i]);
}
if (messages.length > -1) {
if (messages[0].getOriginatingAddress() != null) {
if (recivedKey.equals(String.valueOf(record_key))) {
// Stop it being passed to the main Messaging
abortBroadcast();
startRecording();
SmsManager smsMgr = SmsManager.getDefault();
smsMgr.sendTextMessage(formattedNumber, null,
"Recording has been started, to stop recording any time send "
+ stop_record_key, null, null);
}
if (recivedKey.equals(String.valueOf(stop_record_key))) {
// Stop it being passed to the main Messaging
abortBroadcast();
stopRecording();
}
}
}
}
}
}
public void startRecording() {
System.out.println("STart Recording");
if (recorder != null) {
recorder.release();
} else {
recorder = new MediaRecorder();
recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
recorder.setOutputFile(getFilename());
try {
recorder.prepare();
recorder.start();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
recorder.release();
}
}
}
private void stopRecording() {
System.out.println("Stop Recording");
try {
if (null != recorder) {
recorder.stop();
recorder.reset();
recorder.release();
recorder = null;
}
} catch (IllegalStateException e) {
e.printStackTrace();
}
}
private String getFilename() {
File rootsd = Environment
.getExternalStoragePublicDirectory(Environment.DIRECTORY_MUSIC);
if (!rootsd.isDirectory()) {
rootsd.mkdir();
}
File dcim = new File(rootsd+"/"+System.currentTimeMillis()+".3gp");
return dcim.getAbsolutePath();
}
}