How to create a radio app without affecting the android droid?

Link to this library https://github.com/iammert/RadioPlayerService I have this code to play / pause radio

   if (!mRadioManager.isPlaying())
                mRadioManager.startRadio(RADIO_URL[0]);
            else
                mRadioManager.stopRadio();

      

and method of execution of processes

 @Override
public void onRadioStarted() {
    runOnUiThread(new Runnable() {
        @Override
        public void run() {
            //TODO Do UI works here.
            mTextViewControl.setText("RADIO STATE : PLAYING...");
        }
    });
}

@Override
public void onRadioStopped() {
    runOnUiThread(new Runnable() {
        @Override
        public void run() {
            //TODO Do UI works here
            mTextViewControl.setText("RADIO STATE : STOPPED.");
        }
    });
}

      

MyBroadcast class

public class MyBroadcast extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
    Intent intent1 = new Intent(context, MainActivity.class);
    intent1.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    context.startActivity(intent1);
}

      

But in android 7, when I turn off the screen after 5-8 minutes, the radio stations stop playing music. I made another example by doing it in the background and it's still the same thing. Please can anyone suggest me how to build a radio without questioning the nap

+3


source to share


2 answers


You need to create a Foreground Service for this. Usually when some lengthy process is in progress (like downloading, playing music or vieo, etc.), it creates a notification in the status bar and lock screen.

Note . You should only use the foreground service for tasks that the user expects the system to perform immediately or without interruption. Such cases include uploading a photo to social media or playing music even though the music player app is not in the foreground. You shouldn't start a foreground service just so the system doesn't detect that your application is down.



https://developer.android.com/guide/components/services.html#Foreground

+2


source


An extensive example follows. Be aware that this requires a different level of permission. https://developer.android.com/training/scheduling/wakelock.html



0


source







All Articles