2

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

Jonas
  • 121,568
  • 97
  • 310
  • 388
Erald Haka
  • 182
  • 2
  • 10

2 Answers2

2

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

shagi
  • 629
  • 8
  • 25
  • Thanks for your reply and your suggestion, I will try to make an example for that, please if you can suggest me an example it will be great, Thanks man – Erald Haka Jun 15 '17 at 08:19
  • How to start foreground service you can find by link. For the music panel control I found this: http://sapandiwakar.in/android-media-player-lock-screen/ – shagi Jun 15 '17 at 08:31
  • Thank You, I really appreciate this, you saved me! – Erald Haka Jun 15 '17 at 08:45
0

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

user1767754
  • 23,311
  • 18
  • 141
  • 164
  • Thanks for your reply and your suggestion, but if I click the button wich turn the screen off what happens? – Erald Haka Jun 15 '17 at 08:22
  • according to documentation in Doze mode *The system ignores wake locks.* https://developer.android.com/training/monitoring-device-state/doze-standby.html – shagi Jun 15 '17 at 08:26