M
M
Makhach Imangazaliev2016-02-12 20:24:08
Java
Makhach Imangazaliev, 2016-02-12 20:24:08

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;
    }
}

Here's how, it's declared in the manifest:
<service android:name="muslim.callrecorder.RecordService" android:enabled="true" android:process=":callrecord"/>

Note that the service is running in a separate process (although this is not the reason, the error was before).
And this is how I start and stop recording:
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);
    }

}

That is, recording starts and stops at the beginning and end of the call. I learned that Receivers and all other classes are recreated when Receiver is called, but I don’t understand where the service is, because it is running in a separate process. In the service's stopRecord method , calling mRecorder.stop() throws a NullPointerException . What could be the problem.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
L
lomikman, 2016-02-13
@lomikman

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 question

Ask a Question

731 491 924 answers to any question