title: How to keep AudioRecord or MediaRecorder running when screen is locked Android
body: <p>I'm creating an baby monitor (only audio) app, but every time screen is locked my app is stopped, tried use Service, WakeLock, AudioRecord Lib and others solutions, but this do not work. Following code:</p>
  <p>Thank you for help!</p>
  <p><strong>MainActivity:</strong></p>
  <pre><code>public class MainActivity extends AppCompatActivity {
      private Thread thread;
  
   @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
          getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
  
          verifyPermissions();
      }
  
  public void buttonStart(View view) {
          thread = new Thread(new Runnable() {
              @Override
              public void run() {
                    Intent serviceIntent = new Intent(this, RecordService.class);
                    serviceIntent.putExtra(&quot;inputExtra&quot;, &quot;Foreground Service Example in Android&quot;);
                    ContextCompat.startForegroundService(this, serviceIntent);             
              }
          });
  
          thread.start();
      }
  
      public void buttonStop(View view) {
              Intent serviceIntent = new Intent(this.getApplicationContext(), RecordService.class);
              this.stopService(serviceIntent);
      }
  }
  
  </code></pre>
  <p><strong>RecordService:</strong> this service is started and stopped from MainActivity on buttonStart and buttonStop methods.</p>
  <pre><code>public class RecordService extends Service {
  
      private MediaRecorder mediaRecorder;
      private MediaPlayer player;
      private String fileName;
  
      @Override
      public void onCreate() {
          super.onCreate();
  
          createNotification();
      }
  
      private PowerManager.WakeLock wakeLockPartial = null;
  
  public int onStartCommand(Intent intent, int flags, int startId) {
  
          createNotification();
  
          String input = intent.getStringExtra(&quot;inputExtra&quot;);
  
          Thread thread = new Thread(new Runnable() {
              @Override
              public void run() {
                  PowerManager pm = (PowerManager) getApplicationContext().getSystemService(getApplicationContext().POWER_SERVICE);
                  wakeLockPartial = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, &quot;angelear:mywakelocktag&quot;);
                  wakeLockPartial.acquire();
  
                  mediaRecorder = new MediaRecorder();
                  resetRecorder();
                  mediaRecorder.start();
              }
          });
  
          thread.start();
  
          return START_STICKY; //tried use START_NOT_STICKY
      }
  }
  
  @Override
      public void onDestroy() {
          super.onDestroy();
          if (mediaRecorder != null) {
              mediaRecorder.stop();
              mediaRecorder.release();
              mediaRecorder = null;
              wakeLockPartial.release();
          }
      }
  
  private void createNotification() {
          if (Build.VERSION.SDK_INT &gt;= 26) {
              String CHANNEL_ID = &quot;my_channel_01&quot;;
              NotificationChannel channel = new NotificationChannel(CHANNEL_ID,
                      &quot;Channel human readable title&quot;,
                      NotificationManager.IMPORTANCE_DEFAULT);
  
              ((NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE)).createNotificationChannel(channel);
  
              Notification notification = new NotificationCompat.Builder(this, CHANNEL_ID)
                      .setContentTitle(&quot;&quot;)
                      .setContentText(&quot;&quot;).build();
  
              startForeground(1, notification);
          }
      }
  
      private void resetRecorder() {
          long currentTimestamp = System.currentTimeMillis();
          fileName = this.getExternalFilesDir(null).getAbsolutePath() + &quot;/&quot; + currentTimestamp + &quot;.3gp&quot;;
  
          mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
          mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
          mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
          mediaRecorder.setAudioEncodingBitRate(16);
          mediaRecorder.setAudioSamplingRate(44100);
          mediaRecorder.setOutputFile(fileName);
  
          try {
              mediaRecorder.prepare();
          } catch (IllegalStateException e) {
              e.printStackTrace();
          } catch (IOException e) {
              e.printStackTrace();
          }
      }
  }
  </code></pre>