Android 在服务上调用stopForeground(true),会给我一个类名为null的问题
我是这样开始我的服务的:Android 在服务上调用stopForeground(true),会给我一个类名为null的问题,android,android-intent,service,classname,foreground-service,Android,Android Intent,Service,Classname,Foreground Service,我是这样开始我的服务的: public void startService(){ if (!isRunningInForeground()) { Log.i("#foregroundservice", "Starting service"); bindIntent = new Intent(PSLocationService.this, PSLocationService.class); startS
public void startService(){
if (!isRunningInForeground()) {
Log.i("#foregroundservice", "Starting service");
bindIntent = new Intent(PSLocationService.this, PSLocationService.class);
startService(bindIntent);
}
}
我通过onstart命令执行此操作:
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
PSLocationService.startId = startId;
if (PSApplicationClass.getInstance().isAutoPilotAllowed() || PSTrip.getActiveTrip() != null) {
Log.i("#foregroundservice", "onStartCommand");
Intent notificationIntent = new Intent(this, PSTimelineFragmentsActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
Notification notification = new Notification.Builder(this)
.setPriority(Notification.PRIORITY_MAX)
.setContentTitle(getText(R.string.passenger_name))
.setContentText(getText(R.string.getStarted_switch_on_toplabel))
.setSmallIcon(R.drawable.notification_icon)
.setContentIntent(pendingIntent)
.build();
startForeground(1338, notification);
}
return super.onStartCommand(intent, flags, startId);
}
当我断开与GoogleAppClient的连接时,我想这样阻止它:
if (isRunningInForeground()) {
Log.i("#foregroundservice", "Stopping service: " + bindIntent);
stopForeground(true);
stopSelf();
}
它总是在stopForeground(true)
方法时崩溃,并出现以下错误:
11-10 12:59:50.026: E/AndroidRuntime(10731): java.lang.NullPointerException: class name is null
11-10 12:59:50.026: E/AndroidRuntime(10731): at android.content.ComponentName.<init>(ComponentName.java:114)
11-10 12:59:50.026: E/AndroidRuntime(10731): at android.app.Service.stopForeground(Service.java:671)
11-10 12:59:50.026: E/AndroidRuntime(10731): at nl.hgrams.passenger.core.tracking.PSLocationService.disconnectLocationClient(PSLocationService.java:272)
11-10 12:59:50.026:E/AndroidRuntime(10731):java.lang.NullPointerException:类名为null
11-10 12:59:50.026:E/AndroidRuntime(10731):位于android.content.ComponentName.(ComponentName.java:114)
11-10 12:59:50.026:E/AndroidRuntime(10731):在android.app.Service.stopForeground(Service.java:671)
11-10 12:59:50.026:E/AndroidRuntime(10731):在nl.hgrams.passenger.core.tracking.PSLocationService.disconnectLocationClient(PSLocationService.java:272)
确实通过从活动类而不是我的应用程序类调用stopForeground(true)
使其正常工作。
另外,我没有停止前台,而是执行以下操作:
Intent serviceIntent = new Intent(context, PSLocationService.class);
context.stopService(serviceIntent);
您在哪里调用startService?从我的应用程序ClassComment stopSelf(在stopForeground之后),并让我知道它是否在以下位置再次崩溃:
new Intent(PSLocationService.this,PSLocationService.class)代码>您为什么通过了PSLocationService。这是?为什么不是应用程序上下文(这个)?我想你的服务只是从前台开始,当你试图停止自我时,它会抛出一个例外共享的例子,比如logcat或Firebase崩溃,这可能有助于试图找出发生这种情况的原因:)但是,是的,只是在4年前否决一个问题的答案,因为它对你不起作用。你在这里干什么?谷歌建议使用StEngFieldAssic或JooSuthPror的WorkMeor,服务是一个古老的古董类,而ItTeNoService已经被Android 11所拒绝,所以如果这对你不起作用,完全放弃你的服务逻辑,使用最佳实践^ ^将你的应用程序与2021相媲美。