Answer the question
In order to leave comments, you need to log in
Why are Service variables reset?
I have a Service running in the background:
public class RecordService extends Service {
private static final int NOTIF_ID = 0;
/** Используются как ключи в Intent*/
public static final String START_RECORD_ACTION = "start_record";
public static final String STOP_RECORD_ACTION = "stop_record";
public static final String RECORD_INFO_INTENT = "call_record_info";
public static final String RECORD_FINISHED_ACTION = "RECORD_FINISHED_ACTION";
private final String LOG_TAG = "RecordService";
/** Статус записи. true - идет запись. */
private boolean mIsRecording = false;
private Mp3Recorder mRecorder = null;
/** Файл, в который сохраняется запись */
private File mRecordingFile = null;
/**
* Если поступила команда начала записи, начинаем запись.
* Если поступила команда остановки записи, останавливаем запись.
*/
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
if (intent != null && START_RECORD_ACTION.equals(intent.getAction())) {
startRecord();
}else if (intent != null && STOP_RECORD_ACTION.equals(intent.getAction())) {
stopRecord();
}
return START_NOT_STICKY;
}
/**
* Начинает запись разговора
*/
private void startRecord() {
//Проверяем, идет ли запись
if (mIsRecording) {
return;
}
NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
builder.setPriority(Notification.PRIORITY_MIN);
startForeground(NOTIF_ID, builder.build());
//создаем файл, в который будет сохраняться запись
mRecordingFile = ...
mRecorder = new Mp3Recorder();
mRecorder.setOutputFile(mRecordingFile);
mRecorder.start();
mIsRecording = true;
}
/**
* Останавливает запись разговора
*/
private void stopRecord() {
Toast.makeText(RecordService.this, "Остановка записи", Toast.LENGTH_SHORT).show();
mRecorder.stop();
stopForeground(true);
stopSelf();
}
public IBinder onBind(Intent intent) {
return null;
}
public boolean onUnbind(Intent intent) {
return false;
}
}
<service android:name="muslim.callrecorder.RecordService" android:enabled="true" android:process=":callrecord"/>
public class CallReceiver extends PhoneCallReceiver {
@Override
protected void onIncomingCallStarted(Context context, String number, Date start) {
callStarted(context);
}
@Override
protected void onOutgoingCallStarted(Context context, String number, Date start) {
callStarted(context);
}
@Override
protected void onIncomingCallEnded(Context context, String number, Date start, Date end) {
callEnded(context);
}
@Override
protected void onOutgoingCallEnded(Context context, String number, Date start, Date end) {
callEnded(context);
}
@Override
protected void onMissedCall(Context context, String number, Date start) {
callEnded(context);
}
private void callStarted(Context context) {
startRecord(context);
}
private void callEnded(Context context) {
stopRecord(context);
}
private void startRecord(Context context) {
Intent intent = new Intent(context, RecordService.class);
intent.setAction(RecordService.START_RECORD_ACTION);
context.startService(intent); //начинаем запись
}
private void stopRecord(Context context) {
Intent intent = new Intent(context, RecordService.class);
intent.setAction(RecordService.STOP_RECORD_ACTION);
context.startService(intent);
}
}
Answer the question
In order to leave comments, you need to log in
your service is not running in a separate process.
make sure startRecord is called with toast and the method exits, i.e. Mp3Recorder is being initialized
make sure that the stopRecord() method is not called twice
, maybe this code will help you
https://github.com/esnyder/callrecorder
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question