在 android 10 上使用 startForeground 调用后应用程序崩溃
app crash after using startForeground call on android 10
我正在使用我在 android 8 上编写的应用程序,没有任何问题。这个应用程序使用后台服务来读取 GPS 坐标、处理它们并执行一些逻辑。
我正在尝试将它移动到 android 10 并相应地更改 gradle 设置,但我在调用“startForeground”后立即崩溃(在设备 galaxy s20 上测试它) - 这是服务 onCreate 代码:
public class ScanService extends Service {
@Override
public void onCreate() {
List<UserLocationManager> locationsManagers = GetLocationsManagers();
_locListener = new MyLocationListener(locationsManagers);
_locManager = (LocationManager)getApplicationContext().getSystemService(Context.LOCATION_SERVICE);
Intent notificationIntent = new Intent(this, MainActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0,
notificationIntent, 0);
Notification notification = new Notification.Builder(this, "lsxChannel")
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle("Location Trigger")
.setContentText("Scanning Location...")
.setContentIntent(pendingIntent).build();
startForegroundService(notificationIntent);
startForeground(1337, notification); // after this one the app crashes
}
我在清单中有权限:
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
如果我评论“startForeground(1337, notification);”行 - 应用程序没有崩溃,所以我确定这是导致问题的行。
崩溃期间的 logcat 输出是:
2020-11-15 17:27:06.190 17455-17455/com.tandenkore.locationtrigger D/AndroidRuntime: Shutting down VM
2020-11-15 17:27:06.192 17455-17455/com.tandenkore.locationtrigger E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.tandenkore.locationtrigger, PID: 17455
android.app.RemoteServiceException: Bad notification for startForeground
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2188)
at android.os.Handler.dispatchMessage(Handler.java:107)
at android.os.Looper.loop(Looper.java:237)
at android.app.ActivityThread.main(ActivityThread.java:8154)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:496)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1100)
2020-11-15 17:27:06.195 6449-8919/? W/ActivityManager: crash : com.tandenkore.locationtrigger,0
2020-11-15 17:27:06.199 17455-17455/com.tandenkore.locationtrigger I/Process: Sending signal. PID: 17455 SIG: 9
2020-11-15 17:27:06.218 6449-6785/? W/InputDispatcher: channel '83d46a2 com.tandenkore.locationtrigger/com.tandenkore.locationtrigger.Activities.MainActivity (server)' ~ Consumer closed input channel or an error occurred. events=0x9, fd=599
2020-11-15 17:27:06.218 6449-6785/? E/InputDispatcher: channel '83d46a2 com.tandenkore.locationtrigger/com.tandenkore.locationtrigger.Activities.MainActivity (server)' ~ Channel is unrecoverably broken and will be disposed!
好的 - 发现问题 - 似乎 android 10 需要创建通知渠道 - 参考来自 https://ask.xiaolee.net/questions/1065268
现在工作。
我正在使用我在 android 8 上编写的应用程序,没有任何问题。这个应用程序使用后台服务来读取 GPS 坐标、处理它们并执行一些逻辑。 我正在尝试将它移动到 android 10 并相应地更改 gradle 设置,但我在调用“startForeground”后立即崩溃(在设备 galaxy s20 上测试它) - 这是服务 onCreate 代码:
public class ScanService extends Service {
@Override
public void onCreate() {
List<UserLocationManager> locationsManagers = GetLocationsManagers();
_locListener = new MyLocationListener(locationsManagers);
_locManager = (LocationManager)getApplicationContext().getSystemService(Context.LOCATION_SERVICE);
Intent notificationIntent = new Intent(this, MainActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0,
notificationIntent, 0);
Notification notification = new Notification.Builder(this, "lsxChannel")
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle("Location Trigger")
.setContentText("Scanning Location...")
.setContentIntent(pendingIntent).build();
startForegroundService(notificationIntent);
startForeground(1337, notification); // after this one the app crashes
}
我在清单中有权限:
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
如果我评论“startForeground(1337, notification);”行 - 应用程序没有崩溃,所以我确定这是导致问题的行。
崩溃期间的 logcat 输出是:
2020-11-15 17:27:06.190 17455-17455/com.tandenkore.locationtrigger D/AndroidRuntime: Shutting down VM
2020-11-15 17:27:06.192 17455-17455/com.tandenkore.locationtrigger E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.tandenkore.locationtrigger, PID: 17455
android.app.RemoteServiceException: Bad notification for startForeground
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2188)
at android.os.Handler.dispatchMessage(Handler.java:107)
at android.os.Looper.loop(Looper.java:237)
at android.app.ActivityThread.main(ActivityThread.java:8154)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:496)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1100)
2020-11-15 17:27:06.195 6449-8919/? W/ActivityManager: crash : com.tandenkore.locationtrigger,0
2020-11-15 17:27:06.199 17455-17455/com.tandenkore.locationtrigger I/Process: Sending signal. PID: 17455 SIG: 9
2020-11-15 17:27:06.218 6449-6785/? W/InputDispatcher: channel '83d46a2 com.tandenkore.locationtrigger/com.tandenkore.locationtrigger.Activities.MainActivity (server)' ~ Consumer closed input channel or an error occurred. events=0x9, fd=599
2020-11-15 17:27:06.218 6449-6785/? E/InputDispatcher: channel '83d46a2 com.tandenkore.locationtrigger/com.tandenkore.locationtrigger.Activities.MainActivity (server)' ~ Channel is unrecoverably broken and will be disposed!
好的 - 发现问题 - 似乎 android 10 需要创建通知渠道 - 参考来自 https://ask.xiaolee.net/questions/1065268
现在工作。