Erald Haka
Erald Haka

Reputation: 182

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

Upvotes: 2

Views: 493

Answers (2)

user1767754
user1767754

Reputation: 25134

The link below is an extensive example. Keep in mind that this requires another permission level. https://developer.android.com/training/scheduling/wakelock.html

Upvotes: 0

shagi
shagi

Reputation: 663

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

Upvotes: 2

Related Questions