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;
}
@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限制了后台服务的使用,您需要启动前台服务。以下是关于奥利奥服务限制的文档。它的一段引文:
Android8.0引入了新方法startForegroundService()来在前台启动一个新服务。在系统创建服务之后,应用程序有五秒钟的时间调用服务的startForeground()方法来显示新服务的用户可见通知。如果应用程序在期限内没有调用startForeground(),系统将停止服务并声明该应用程序为ANR。
将服务置于前台主要涉及将服务附加到用户可见的通知中:
service.startForeground(NOTIFICATION_ID, notification);
我的目标是显示一个活动在每一个地方(甚至锁定屏幕)在一个特定的时间。
我正在使用后台服务,以获得用户的位置和上传到服务器。 我使用了一个扩展Service类的LocationTrack类和扩展BroadcastReciver的AlramReciver。 我已经在manifest中添加了所有权限,并在manifest文件中注册了AlarmReciver和LocationTracker类。 Oreo-在前台启动服务 https://developer.android.c
Ths是我的服务班 }
我如何防止android系统不杀服务?。 我不想在我的服务运行时一直显示通知,因为我将运行我的服务24*7为新消息,所以它感觉便宜的UI体验给用户。
我正在使用服务作为前台之一。在android pre O版本上,一切都很好。但在Android奥利奥当我关闭app时,前台服务也关闭了…… 你知道为什么它与应用程序关闭以及如何防止它吗? 谢了!