gpt4 book ai didi

android - 如何在不受 doze android 影响的情况下构建广播应用程序?

转载 作者:太空狗 更新时间:2023-10-29 13:08:19 25 4
gpt4 key购买 nike

引用了这个库https://github.com/iammert/RadioPlayerService我有播放/暂停 radio 的代码

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

和处理方法

 @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.");
}
});
}

我的广播类

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);
}

但是在 android 7 中,当我在 radio 停止播放音乐 5-8 分钟后关闭屏幕时。我在后台做了另一个例子,它仍然是一回事。请任何人都可以建议我如何构建一个不受打瞌睡影响的广播应用程序

最佳答案

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

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

关于android - 如何在不受 doze android 影响的情况下构建广播应用程序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44561881/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com