gpt4 book ai didi

java - 后台服务在 API 28 上使应用程序崩溃,但在 API 23 上运行

转载 作者:行者123 更新时间:2023-12-03 20:30:42 24 4
gpt4 key购买 nike

我有一个在后台启动服务以监控接近传感器的应用程序。这一切在 targetSdkVersion 23 上运行良好,但在 28 上运行不佳。服务:

public class ForegroundService (intent.getAction().equals(Constants.ACTION.STARTFOREGROUND_ACTION)) {
Log.i(LOG_TAG, "Received Start Foreground Intent ");
Intent notificationIntent = new Intent(this, MainActivity.class);
notificationIntent.setAction(Constants.ACTION.MAIN_ACTION);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
| Intent.FLAG_ACTIVITY_CLEAR_TASK);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0,
notificationIntent, 0);

Intent previousIntent = new Intent(this, ForegroundService.class);
previousIntent.setAction(Constants.ACTION.PREV_ACTION);
PendingIntent ppreviousIntent = PendingIntent.getService(this, 0,
previousIntent, 0);

Intent playIntent = new Intent(this, ForegroundService.class);
playIntent.setAction(Constants.ACTION.PLAY_ACTION);
PendingIntent pplayIntent = PendingIntent.getService(this, 0,
playIntent, 0);


Intent

当应用程序崩溃时,我也会收到 logcat:

020-01-03 03:24:00.466
26636-26636/com.personal.ramakrishnanak.autoscreenoff
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.personal.ramakrishnanak.autoscreenoff, PID: 26636
android.app.RemoteServiceException: Bad notification for startForeground: java.lang.RuntimeException: invalid channel for
service notification: Notification(channel=null pri=0 contentView=null
vibrate=null sound=null tick defaults=0x0 flags=0x42 color=0x00000000
actions=1 vis=PRIVATE semFlags=0x0 semPriority=0 semMissedCount=0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2067)
at android.os.Handler.dispatchMessage(Handler.java:107)
at android.os.Looper.loop(Looper.java:237)
at android.app.ActivityThread.main(ActivityThread.java:7762)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1047)

最佳答案

从API 28开始,如果你想设置前台服务,你必须添加FOREGROUND_SERVICE权限。

这是 official document :

Apps that target Android 9 or higher and use foreground services must request the FOREGROUND_SERVICE permission. This is a normal permission, so the system automatically grants it to the requesting app. If an app that targets Android 9 or higher attempts to create a foreground service without requesting FOREGROUND_SERVICE, the system throws a SecurityException.

另一个问题可能是通知。从 API 26+ 开始,您必须添加通知 channel 。

private void createNotificationChannel() {
// Create the NotificationChannel, but only on API 26+ because
// the NotificationChannel class is new and not in the support library
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
String CHANNEL_ID = "some_channel_id";
CharSequence name = getString(R.string.channel_name);
String description = getString(R.string.channel_description);
int importance = NotificationManager.IMPORTANCE_DEFAULT;
// add the NotificationChannel
NotificationChannel channel = new NotificationChannel(CHANNEL_ID, name, importance);
channel.setDescription(description);
NotificationManager notificationManager = getSystemService(NotificationManager.class);
notificationManager.createNotificationChannel(channel);
}

关于java - 后台服务在 API 28 上使应用程序崩溃,但在 API 23 上运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59572842/

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