当前位置: 首页 > 知识库问答 >
问题:

当奥利奥的应用程序在后台时,音乐服务在一秒钟后不工作

惠洛华
2023-03-14
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();*/

    }
}
}`

共有1个答案

彭胡媚
2023-03-14

由于Oreo限制了后台服务的使用,您需要启动前台服务。以下是关于奥利奥服务限制的文档。它的一段引文:

Android8.0引入了新方法startForegroundService()来在前台启动一个新服务。在系统创建服务之后,应用程序有五秒钟的时间调用服务的startForeground()方法来显示新服务的用户可见通知。如果应用程序在期限内没有调用startForeground(),系统将停止服务并声明该应用程序为ANR。

将服务置于前台主要涉及将服务附加到用户可见的通知中:

service.startForeground(NOTIFICATION_ID, notification);
 类似资料: