我应该对API> = 26使用StartService还是StartForegroundService吗?

时间:2019-06-06 04:48:17

标签: java android service

我有点困惑,因为我读过一些帖子,如果API> = 26,我也应该使用ContextCompat.StartForegroundService();

现在我仍然只使用 StartService ,即使我应该在API> = 26(电话上当前的api为27)上获得IllegalStateException的情况下,它仍然可以工作帖子。

https://medium.com/mindorks/mastering-android-service-of-2018-a4a1df5ed5a6

  

我知道服务是一个古老的概念。让我向您保证,我们将不讨论基础知识,我们将学习对Android 8.0+中服务层所做的最新更改,我们将解决著名的 IllegalStateException 和RemoteServiceException之谜。本文不是了解服务的常规方法,请耐心等待。

所以我的问题是我应该更改startForeGroundService还是仅将startService用于API> = 26?

处理我的服务连接的我的班级:

/**This establishes the connection to the MediaPlayerService. */
public static ServiceConnection serviceConnection = new ServiceConnection() {

    @Override
    public void onServiceConnected(ComponentName name, IBinder service) {
        MediaPlayerService.MusicBinder binder = (MediaPlayerService.MusicBinder)service;
        mediaPlayerService = binder.getService();
        mediaPlayerService.musicBound = true;
    }

    @Override
    public void onServiceDisconnected(ComponentName name) {
        mediaPlayerService.musicBound = false;
    }
};

/**This is called to start the MediaPlayerService. */
private static Intent mediaPlayerServiceIntent = null;
public static void startMusicService(Context c) {

    /*mediaPlayerServiceIntent binds our connection to the MediaPlayerService. */
    mediaPlayerServiceIntent = new Intent(c, MediaPlayerService.class);
    c.bindService(mediaPlayerServiceIntent, serviceConnection, Context.BIND_AUTO_CREATE);
    c.startService(mediaPlayerServiceIntent);
    mServiceIsActive = true;
}

/**This is called to stop the MediaPlayerService. (onDestroy) */
public static void stopMusicService(Context c) {

    if (mediaPlayerServiceIntent == null)
        return;
    c.unbindService(serviceConnection);
    c.stopService(mediaPlayerServiceIntent);
    mediaPlayerServiceIntent = null;

    mediaPlayerService = null;
}

MainActivity:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Main.startMusicService(getApplicationContext());

}

1 个答案:

答案 0 :(得分:0)

startService将不适用于api> = 26

您可以在以下代码的帮助下将服务更改为前台服务。它将显示通知。

value

更多参考-https://android-developers.googleblog.com/2018/12/effective-foreground-services-on-android_11.html

https://developer.android.com/guide/components/services

另一种方法(不推荐。目标sdk必须小于等于26)

private void runAsForeground(){
Intent notificationIntent = new Intent(this, MediaPlayerService.class);
PendingIntent pendingIntent=PendingIntent.getActivity(this, 0,
        notificationIntent, Intent.FLAG_ACTIVITY_NEW_TASK);

Notification notification=new NotificationCompat.Builder(this)
                            .setSmallIcon(R.drawable.ic_launcher)
                            .setContentText(getString(R.string.isRecording))
                            .setContentIntent(pendingIntent).build();

startForeground(NOTIFICATION_ID, notification);
}

致电

public static void startService(Context context, Class className) {
    try {
        if (Build.VERSION.SDK_INT > Build.VERSION_CODES.N) {
            Intent restartServiceIntent = new Intent(context, className);
            restartServiceIntent.setPackage(context.getPackageName());
            PendingIntent restartServicePendingIntent = PendingIntent.getService(context, 1, restartServiceIntent, PendingIntent.FLAG_ONE_SHOT);
            AlarmManager alarmService = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
            if (alarmService != null) {
                alarmService.set(
                        AlarmManager.ELAPSED_REALTIME,
                        SystemClock.elapsedRealtime() + 500,
                        restartServicePendingIntent);
            }
        } else {
            Intent i = new Intent(context, className);
            context.startService(i);
        }
    } catch (Exception e) {
        MyLog.e(TAG, "startService: ", e);
    }
}