gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 04:47:42 25 4
gpt4 key购买 nike

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

现在我仍然只使用 StartService 并且它可以工作,即使我应该在 API >= 26 上得到 IllegalStateException (手机上当前的 api 是 27)根据这篇文章。

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

I know Service is an old concept. Let me assure you we will not discuss the basics and we will learn the recent changes made to the service layer in Android 8.0+, we will solve the mystery of famous IllegalStateException and RemoteServiceException. This article is not a conventional way of understanding services, hang tight till you can.

所以我的问题是我是否应该更改 startForeGroundService 还是只保留 API >=26 的 startService

处理我的服务连接的类:

/**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;
}

主要 Activity :

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

Main.startMusicService(getApplicationContext());

}

最佳答案

startService 不适用于 api >=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);
}

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

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

另一种方式(不推荐。目标 SDK 必须为 26 或更少)

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);
}
}

调用方式

 startService(context,MediaPlayerService.class);

关于java - 对于 API >= 26,我应该使用 StartService 还是 StartForegroundService 吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56471033/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com