如何在不受瞌睡影响的情况下构建广播应用程序 android?
How to build a radio app without being affected by doze android?
引用了这个库https://github.com/iammert/RadioPlayerService
我有 playing/pause 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.");
}
});
}
我的广播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);
}
但是在 android 7 中,当我在收音机停止播放音乐 5-8 分钟后关闭屏幕时。我在后台做了另一个例子,它仍然是一回事。请任何人都可以建议我如何构建一个不受打瞌睡影响的广播应用程序
您必须为此创建 Foreground 服务。通常当任何长进程是 运行(如下载、播放音乐或 vieo 等)时,它会在状态栏和锁定屏幕中创建通知。
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
下面的 link 是一个广泛的例子。请记住,这需要另一个权限级别。
https://developer.android.com/training/scheduling/wakelock.html
引用了这个库https://github.com/iammert/RadioPlayerService 我有 playing/pause 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.");
}
});
}
我的广播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);
}
但是在 android 7 中,当我在收音机停止播放音乐 5-8 分钟后关闭屏幕时。我在后台做了另一个例子,它仍然是一回事。请任何人都可以建议我如何构建一个不受打瞌睡影响的广播应用程序
您必须为此创建 Foreground 服务。通常当任何长进程是 运行(如下载、播放音乐或 vieo 等)时,它会在状态栏和锁定屏幕中创建通知。
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
下面的 link 是一个广泛的例子。请记住,这需要另一个权限级别。 https://developer.android.com/training/scheduling/wakelock.html