androidinternet-radioandroid-doze-and-standbydoze

How to build a radio app without being affected by doze android?


Refered to this library https://github.com/iammert/RadioPlayerService I have this code for playing/pause radio

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

and method for doing 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 the screen off after 5-8 min radio stops playing music. I have done another example by doing in background and it is still the same thing. Please can anyone suggest me how to build a radio app without being affected by doze


Solution

  • You have to create Foreground Service for that. Usually when any long process is running (like downloading, playing music or vieo etc.) it creates notification in status bar and lock screen.

    Note: You should only use a foreground service for tasks the user expects the system to execute immediately or without interruption. Such cases include uploading a photo to social media, or playing music even while the music-player app is not in the foreground. You should not start a foreground service simply to prevent the system from determining that your app is idle.

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