如何在奥利奥的后台运行服务?这个服务类在Oreo下面的所有Android版本中运行良好,我在清单中声明了这个服务。在我的活动课程中,我使用startservice(getApplicationContext,ExoService.class)
启动。
public class ExoService extends Service {
private static Context context;
private static ItemRadio station;
private static ExoService service;
public static SimpleExoPlayer exoPlayer;
private static Uri uri;
private WifiManager.WifiLock wifiLock;
static ProgressTask task;
/*binder return null*/
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
task = new ProgressTask();
task.execute();
return START_STICKY;
/*Alrady set Start Sticky*/
}
/*here is initilize service class*/
static public void initialize(Context context, ItemRadio station) {
ExoService.context = context;
ExoService.station = station;
Log.e("inwhich", "");
}
/*this is my service instance*/
static public ExoService getInstance() {
if (service == null) {
service = new ExoService();
}
return service;
}
/*Oncreate method */
@Override
public void onCreate() {
super.onCreate();
try {
TrackSelector trackSelector = new DefaultTrackSelector();
LoadControl loadControl = new DefaultLoadControl();
exoPlayer = ExoPlayerFactory.newSimpleInstance(this, trackSelector, loadControl);
} catch (NullPointerException e) {
e.printStackTrace();
}
}
/*public void stop() {
if (exoPlayer != null && exoPlayer.getPlayWhenReady()) {
exoPlayer.stop();
exoPlayer.release();
exoPlayer = null;
this.wifiLock.release();
this.stopForeground(true);
}
}*/
public void start() {
if (exoPlayer != null) {
exoPlayer.setPlayWhenReady(true);
}
}
/*after some second ondstroy method call in oreo.*/
public void onDestroy() {
if (exoPlayer != null) {
exoPlayer.stop();
}
Log.e("Destroyed", "Called");
}
/*public void pause() {
if (exoPlayer != null) {
exoPlayer.stop();
// exoPlayer.setPlayWhenReady(false);
}
}*/
public ItemRadio getPlayingRadioStation() {
return station;
}
用于解码歌曲的异步任务url:
@SuppressLint("StaticFieldLeak")
private class ProgressTask extends AsyncTask<String, Void, Boolean> {
protected void onPreExecute() {
}
protected Boolean doInBackground(final String... args) {
/*boolean bool = true;*/
try {
uri = Uri.parse(station.getRadiourl());
DefaultBandwidthMeter bandwidthMeterA = new DefaultBandwidthMeter();
DefaultDataSourceFactory dataSourceFactory = new DefaultDataSourceFactory(context, Util.getUserAgent(context, getString(R.string.app_name)), bandwidthMeterA);
ExtractorsFactory extractorsFactory = new DefaultExtractorsFactory();
MediaSource audioSource = new ExtractorMediaSource(uri, dataSourceFactory, extractorsFactory, null, null);
/* exoPlayer.addListener(eventListener);
MediaSource videoSource = new HlsMediaSource(uri, dataSourceFactory, 1, null, null);*/
final LoopingMediaSource loopingSource = new LoopingMediaSource(videoSource);
if (station.getRadiourl().endsWith(".m3u8")) {
exoPlayer.prepare(loopingSource);
} else {
exoPlayer.prepare(audioSource);
}
/*exoPlayer.setPlayWhenReady(true);*/
} catch (IllegalArgumentException | IllegalStateException | SecurityException | NullPointerException e1) {
e1.printStackTrace();
}
return true;
}
@SuppressLint("WifiManagerPotentialLeak")
@Override
protected void onPostExecute(final Boolean success) {
try {
if (success) {
wifiLock = ((WifiManager) context.getSystemService(Context.WIFI_SERVICE))
.createWifiLock(WifiManager.WIFI_MODE_FULL, "RadiophonyLock");
wifiLock.acquire();
exoPlayer.setPlayWhenReady(true);
} else {
/*Toast.makeText(context, context.getString(R.string.internet_disabled), Toast.LENGTH_SHORT).show();*/
}
} catch (NullPointerException e) {
e.printStackTrace();
}
/* dialog.dismiss();*/
}
}
}`
一段时间后,会在oreo中自动调用ondestroy
方法。我怎么处理这个?
答案 0 :(得分:0)
由于Oreo限制后台服务的使用,您需要启动前台服务。这里the documentation关于奥利奥的服务限制。引用它:
Android 8.0引入了新方法startForegroundService() 在前台启动新服务。系统创建完成后 该服务,该应用程序有五秒钟来调用该服务 startForeground()方法显示新服务的用户可见性 通知。如果应用程序没有调用startForeground() 时间限制,系统停止服务并声明应用程序 ANR
将服务放在前台基本上涉及将服务附加到用户可见的通知:
service.startForeground(NOTIFICATION_ID, notification);
startForeground服务自API 5起可用,而startForegroundService随Oreo一起提供,因此您需要检查设备的API级别并相应地启动服务。