这是我到目前为止所做的。这不记录所有呼叫。它不会产生任何错误,但录制的文件有时是空的。我想记录所有来电和去电。我究竟做错了什么 ?
public class CallReceiver extends BroadcastReceiver {
private static MediaRecorder callrecorder;
private static Boolean recording = false;
@Override
public void onReceive(Context context, Intent intent) {
SharedPreferences prefs = context.getSharedPreferences("MyPreference", Context.MODE_PRIVATE);
String check = prefs.getString("isCheckedIn", null);
Log.d("Checked In :", check);
if (check.equals("Yes")) {
TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
try {
Class c = Class.forName(tm.getClass().getName());
Method m = c.getDeclaredMethod("getITelephony");
m.setAccessible(true);
Bundle bundle = intent.getExtras();
Bundle extras = intent.getExtras();
String state = extras.getString(TelephonyManager.EXTRA_STATE);
String phoneNumber = bundle.getString("incoming_number");
TelephonyManager mTelephonyMgr = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
// Log.d("INCOMING", phoneNumber);
Log.d("State", state);
switch (state) {
case "IDLE":
Toast.makeText(context, state + "", Toast.LENGTH_SHORT).show();
if (recording) {
stopRecording();
Toast.makeText(context, "stopped", Toast.LENGTH_SHORT).show();
}
break;
case "OFFHOOK":
PreferencesHelper Prefs = new PreferencesHelper(context.getApplicationContext());
String checked = Prefs.GetPreferences("isCheckedIn");
Toast.makeText(context, "Is CheckedIn :" + checked, Toast.LENGTH_SHORT).show();
if (checked.equals("Yes")) {
if (!recording) {
startRecording();
Toast.makeText(context, "started", Toast.LENGTH_SHORT).show();
}
}
break;
case "RINGING":
Toast.makeText(context, state + "" + phoneNumber, Toast.LENGTH_LONG).show();
break;
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
void startRecording() {
//String root="/storage/sdcard0/";
String fname = "/sales/rec" + System.currentTimeMillis() / 1000 + ".amr";
//File file=new File(root,fname);
callrecorder=new MediaRecorder();
callrecorder.setAudioSource(MediaRecorder.AudioSource.VOICE_CALL);
callrecorder.setOutputFormat(MediaRecorder.OutputFormat.AMR_NB);
callrecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
callrecorder.setOutputFile(Environment.getExternalStorageDirectory().getAbsolutePath()+fname);
//callrecorder.setOutputFile(file.getAbsolutePath()
//callrecorder.setOutputFile(getCacheDir().getAbsolutePath() + "/" + fname);
//);
try {
callrecorder.prepare();
callrecorder.start();
recording = true;
// Toast.makeText(getApplicationContext(), "Started", Toast.LENGTH_SHORT).show();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
// Toast.makeText(getApplicationContext(),e.printStackTrace(),3000 ).show();
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
void stopRecording() {
try {
callrecorder.stop();
// callrecorder.release();
callrecorder.reset();
callrecorder = null;
recording = false;
} catch (IllegalStateException e) {
e.printStackTrace();
}
}
}