首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >stopForeground(真)调用一个服务,给我一个类名为空的问题

stopForeground(真)调用一个服务,给我一个类名为空的问题
EN

Stack Overflow用户
提问于 2017-11-10 12:00:53
回答 1查看 2K关注 0票数 1

我就是这样开始服务的:

代码语言:javascript
复制
    public void startService(){
        if (!isRunningInForeground()) {
            Log.i("#foregroundservice", "Starting service");
            bindIntent = new Intent(PSLocationService.this, PSLocationService.class);
            startService(bindIntent);
        }
    }

我在onStartCommand上这样做:

代码语言:javascript
复制
   @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);
}

当我断开与googleApiClient的连接时,我想这样阻止它:

代码语言:javascript
复制
  if (isRunningInForeground()) {
        Log.i("#foregroundservice", "Stopping service: " + bindIntent);
        stopForeground(true);
        stopSelf();
    }

它总是在stopForeground(true)方法处崩溃,出现此错误:

代码语言:javascript
复制
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)
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-11-13 17:48:15

通过调用活动类中的stopForeground(true) (而不是我的ApplicationClass ),确实成功地使其工作。另外,我没有使用stopForeground,而是这样做:

代码语言:javascript
复制
Intent serviceIntent = new Intent(context, PSLocationService.class);
context.stopService(serviceIntent);
票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/47222334

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档