3

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

4

2 回答 2

2

您必须为此创建前台服务。通常当任何长时间的进程运行时(如下载、播放音乐或视频等),它会在状态栏和锁定屏幕中创建通知。

注意:您应该只对用户期望系统立即执行或不中断执行的任务使用前台服务。此类情况包括将照片上传到社交媒体,或者在音乐播放器应用程序不在前台时播放音乐。您不应该仅仅为了防止系统确定您的应用程序处于空闲状态而启动前台服务。

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

于 2017-06-15T08:04:54.120 回答
0

下面的链接是一个广泛的例子。请记住,这需要另一个权限级别。 https://developer.android.com/training/scheduling/wakelock.html

于 2017-06-15T08:08:51.397 回答