我是android的新手我的问题是,当我通过电源按钮启动录音时,在服务类的帮助下,代码正常工作,当我关闭手机并再次打开时,我的应用程序崩溃。请帮助我.. < / p>
我的服务类是: -
public class PowerButtonService extends Service implements MediaListner {
BroadcastReceiver mReceiver;
static final String AUDIO_RECORDER_FILE_EXT_MP3 = ".mp3";
static final String AUDIO_RECORDER_FILE_EXT_3GP = ".3gp";
static final String AUDIO_RECORDER_FILE_EXT_MP4 = ".mp4";
private Handler mHandler;
SharedPreferences prefs;
private Runnable mRunnable;
String file_exts[] = { AUDIO_RECORDER_FILE_EXT_MP3,
AUDIO_RECORDER_FILE_EXT_MP4, AUDIO_RECORDER_FILE_EXT_3GP };
private static final String AUDIO_RECORDER_FOLDER = "AudioRecorder";
private int count;
Sessions sessions;
int recordCount = 0;
private Editor mEditor;
private MediaRecorder recorder = null;
private int currentFormat = 0;
private int output_formats[] = { MediaRecorder.OutputFormat.MPEG_4,
MediaRecorder.OutputFormat.THREE_GPP };
@Override
public void onCreate() {
super.onCreate();
IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_ON);
filter.addAction(Intent.ACTION_SCREEN_OFF);
mReceiver = new MyReceiver();
registerReceiver(mReceiver, filter);
sessions = new Sessions(PowerButtonService.this);
sessions.setServiceInstance(this);
prefs = getSharedPreferences("app", Context.MODE_PRIVATE);
mEditor = prefs.edit();
}
@Override
public void onDestroy() {
mEditor.putBoolean("PowerButtonServiceStart", false);
mEditor.commit();
unregisterReceiver(mReceiver);
Toast.makeText(getApplicationContext(),
"Recording from power button is stopped", Toast.LENGTH_SHORT)
.show();
Log.i("onDestroy Reciever", "Called");
super.onDestroy();
}
@Override
public void onStart(Intent intent, int startId) {
final Vibrator vibe = (Vibrator) this
.getSystemService(Context.VIBRATOR_SERVICE);
if (!prefs.getBoolean("check", false)) {
recordCount = recordCount + 1;
if (recordCount == 3) {
if (prefs.getBoolean("RecordingOnInsideApp", false)) {
vibe.vibrate(1000);
recordCount = 0;
Toast.makeText(getApplicationContext(),
"Recording is already running", Toast.LENGTH_LONG)
.show();
} else {
mEditor.putBoolean("powerbutton", true);
mEditor.commit();
if (null == recorder && !(Sessions.getRecordOnActivity())) {
recordCount = 0;
System.out.println("recoding on");
recorder = new MediaRecorder();
recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
recorder.setOutputFormat(output_formats[currentFormat]);
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
recorder.setOutputFile(getFilename());
recorder.setOnErrorListener(errorListener);
recorder.setOnInfoListener(infoListener);
try {
recorder.prepare();
recorder.start();
vibe.vibrate(700);
Notifyuser();
mHandler = new Handler();
mRunnable = new Runnable() {
@Override
public void run() {
if (null != recorder) {
mEditor.putBoolean("powerbutton", false);
mEditor.putString("PowerButtonTime",
"00:00:00");
count = 0;
mTimer.cancel();
mEditor.commit();
Sessions.setRecordOnService(false);
recordCount = 0;
recorder.stop();
recorder.reset();
recorder.release();
System.out.println("hello helloo");
Sessions.StopPowerButtonRecording();
vibe.vibrate(3000);
recorder = null;
}
Log.e("Ho rha hai", "goodndnd");
}
};
mHandler.postDelayed(mRunnable, 900000);
Sessions.setRecordOnService(true);
startTimer(mEditor);
Sessions.startPowerButtonServiceTime();
vibe.vibrate(1000);
} catch (Exception e) {
e.printStackTrace();
}
} else {
if ((Sessions.getRecordOnActivity())) {
Sessions.getServiceActivity();
vibe.vibrate(5000);
}
if (null != recorder) {
Sessions.setRecordOnService(false);
recordCount = 0;
recorder.stop();
recorder.reset();
recorder.release();
Sessions.stopServiceTimer();
vibe.vibrate(500);
recorder = null;
mEditor.putBoolean("powerbutton", false);
mEditor.putString("PowerButtonTime", "00:00:00");
count = 0;
mTimer.cancel();
mEditor.commit();
Sessions.setRecordOnService(false);
recordCount = 0;
recorder.stop();
recorder.reset();
recorder.release();
System.out.println("hello helloo");
Sessions.StopPowerButtonRecording();
vibe.vibrate(3000);
recorder = null;
}
}
}
}
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
System.out.println("time over...");
recordCount = 0;
}
}, 3000);
} else {
vibe.vibrate(1000);
Toast.makeText(getApplicationContext(),
"Recording from Widget is already Running",
Toast.LENGTH_LONG).show();
}
}
private String getFilename() {
String filepath = Environment.getExternalStorageDirectory().getPath();
File file = new File(filepath, AUDIO_RECORDER_FOLDER);
if (!file.exists()) {
file.mkdirs();
}
SimpleDateFormat formatter = new SimpleDateFormat("dd_MMM_yy_HH_mm_ss");
String dateString = formatter.format(new Date(System
.currentTimeMillis()));
return (file.getAbsolutePath() + "/" + "Rec_" + dateString + file_exts[currentFormat]);
}
private MediaRecorder.OnErrorListener errorListener = new MediaRecorder.OnErrorListener() {
@Override
public void onError(MediaRecorder mr, int what, int extra) {
}
};
private MediaRecorder.OnInfoListener infoListener = new MediaRecorder.OnInfoListener() {
@Override
public void onInfo(MediaRecorder mr, int what, int extra) {
}
};
private Timer mTimer;
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onMediaStopListner() {
if (null != recorder) {
if (mHandler != null) {
mHandler.removeCallbacks(mRunnable);
}
mEditor.putBoolean("powerbutton", false);
mEditor.putString("PowerButtonTime", "00:00:00");
count = 0;
if (mTimer != null) {
mTimer.cancel();
}
mEditor.commit();
recordCount = 0;
recorder.stop();
recorder.reset();
recorder.release();
Vibrator vibe = (Vibrator) this
.getSystemService(Context.VIBRATOR_SERVICE);
vibe.vibrate(1000);
System.out.println("onMediaStopListner in Power button service");
System.out.println("powerbutton "
+ prefs.getBoolean("powerbutton", false));
System.out.println("PowerButtonTime "
+ prefs.getString("PowerButtonTime", ""));
recorder = null;
}
}
@Override
public void onMediaStartListner() {
}
@SuppressWarnings("deprecation")
public void Notifyuser() {
NotificationManager notificationManager = (NotificationManager) this
.getSystemService(Context.NOTIFICATION_SERVICE);
Intent intent = new Intent(this, SplashActivity.class);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
intent, PendingIntent.FLAG_UPDATE_CURRENT);
Notification notification = new Notification(R.drawable.icon,
"Recording Start", System.currentTimeMillis());
notification.flags = Notification.FLAG_AUTO_CANCEL
| Notification.DEFAULT_LIGHTS | Notification.DEFAULT_SOUND;
notification.setLatestEventInfo(this, "Recording Find", ".....",
contentIntent);
notificationManager.notify(10, notification);
}
public void startTimer(final Editor editor) {
try {
mTimer = new Timer();
mTimer.scheduleAtFixedRate(new TimerTask() {
@Override
public void run() {
String timer = String.format("%02d:%02d:%02d",
count / 3600, (count % 3600) / 60, (count % 60));
count++;
editor.putString("PowerButtonTime", timer);
editor.commit();
System.out.println("This is only For Testing" + timer);
}
}, 1000, 1000);
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public void onTaskRemoved(Intent rootIntent) {
if (prefs.getBoolean("powerbutton", false)) {
Toast.makeText(getApplicationContext(),
"Recording has been saved successfully", Toast.LENGTH_LONG)
.show();
final Vibrator vibe = (Vibrator) this
.getSystemService(Context.VIBRATOR_SERVICE);
if (null != recorder) {
mEditor.putBoolean("powerbutton", false);
mEditor.putString("PowerButtonTime", "00:00:00");
count = 0;
mTimer.cancel();
mEditor.commit();
Sessions.setRecordOnService(false);
recordCount = 0;
recorder.stop();
recorder.reset();
recorder.release();
System.out.println("hello helloo");
Sessions.StopPowerButtonRecording();
String ns = Context.NOTIFICATION_SERVICE;
NotificationManager nMgr = (NotificationManager) getApplicationContext()
.getSystemService(ns);
nMgr.cancel(10);
vibe.vibrate(3000);
recorder = null;
}
}
super.onTaskRemoved(rootIntent);
}