【问题标题】:java.lang.IllegalStateException at android.app.ContextImpl.startServiceCommonandroid.app.ContextImpl.startServiceCommon 处的 java.lang.IllegalStateException
【发布时间】:2018-12-29 08:12:23
【问题描述】:

我在 Playstore 上有一个应用程序,最近我只在 Android 8.0 设备上遇到这些错误。请查看我从 Google Play 控制台收到的 android stacktrace。我还包括了 firebase 助手类。在此先感谢:)

java.lang.IllegalStateException: 
  at android.app.ContextImpl.startServiceCommon (ContextImpl.java:1737)
  at android.app.ContextImpl.startService (ContextImpl.java:1693)
  at android.content.ContextWrapper.startService (ContextWrapper.java:650)
  at android.support.v4.content.WakefulBroadcastReceiver.startWakefulService (Unknown Source:22)
  at com.google.firebase.iid.zzg.zzg (Unknown Source:9)
  at com.google.firebase.iid.zzg.zzb (Unknown Source:78)
  at com.google.firebase.iid.zzg.zzf (Unknown Source:2)
  at com.google.firebase.iid.FirebaseInstanceIdService.a (Unknown Source:23)
  at com.google.firebase.iid.FirebaseInstanceIdService.a (Unknown Source:34)
  at com.google.firebase.iid.FirebaseInstanceId.<init> (Unknown Source:31)
  at com.google.firebase.iid.FirebaseInstanceId.getInstance (Unknown Source:47)
  at com.google.firebase.iid.FirebaseInstanceId.getInstance (Unknown Source:4)
  at com.google.firebase.iid.FirebaseInstanceIdService.zza (Unknown Source:14)
  at com.google.firebase.iid.FirebaseInstanceIdService.handleIntent (Unknown Source:35)
  at com.google.firebase.iid.zzb$zza$1.run (Unknown Source:24)
  at java.util.concurrent.ThreadPoolExecutor.runWorker (ThreadPoolExecutor.java:1162)
  at java.util.concurrent.ThreadPoolExecutor$Worker.run (ThreadPoolExecutor.java:636)
  at java.lang.Thread.run (Thread.java:784) 

MyFirebaseInstanceIdService 类

public class MyFirebaseInstanceIdService extends FirebaseInstanceIdService {

    private static final String REG_TOKEN = "REG_TOKEN";

    @Override
    public void onTokenRefresh() {

        String recent_token = FirebaseInstanceId.getInstance().getToken();
        Log.d(REG_TOKEN, recent_token);

    }
}

MyFirebaseMessagingService 类

public class MyFirebaseMessagingService extends FirebaseMessagingService {

    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {
        Intent intent = new Intent(getApplicationContext(), OnlineActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        PendingIntent pendingIntent = PendingIntent.getActivity(getApplicationContext(), 0, intent, PendingIntent.FLAG_ONE_SHOT);
        NotificationCompat.Builder notifcationBuilder = new NotificationCompat.Builder(getApplicationContext());
        notifcationBuilder.setContentTitle("Dictionary Notification");
        notifcationBuilder.setContentText(remoteMessage.getNotification().getBody());
        notifcationBuilder.setAutoCancel(true);
        notifcationBuilder.setSmallIcon(R.mipmap.ic_launcher);
        notifcationBuilder.setContentIntent(pendingIntent);
        NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        if (notificationManager != null) {
            notificationManager.notify(0, notifcationBuilder.build());
        }

    }
}

Gradle 文件:

dependencies {
    implementation 'com.android.support:support-vector-drawable:27.1.1'

    implementation 'com.google.android.gms:play-services-plus:15.0.1'
    implementation fileTree(dir: 'libs', include: ['*.jar'])


    implementation 'com.google.firebase:firebase-core:16.0.1'

    implementation 'com.android.support:support-v4:27.1.1'
    implementation 'com.android.support:recyclerview-v7:27.1.1'
    implementation 'com.android.support:design:27.1.1'
    implementation 'com.readystatesoftware.sqliteasset:sqliteassethelper:+'
    implementation 'com.android.support.constraint:constraint-layout:1.1.2'
    implementation 'com.github.Hitomis:CircleMenu:v1.1.0'


    implementation 'com.squareup.okhttp3:okhttp:3.10.0'
    implementation 'com.google.code.gson:gson:2.8.2'
    implementation 'com.squareup.retrofit2:retrofit:2.4.0'
    implementation 'com.squareup.retrofit2:converter-gson:2.3.0'
    implementation 'com.mindorks:placeholderview:0.2.7'


    implementation('com.crashlytics.sdk.android:crashlytics:2.6.8@aar') {
        transitive = true;
    }
    testImplementation 'junit:junit:4.12'


    implementation 'com.android.support:cardview-v7:27.1.1'
    implementation 'com.github.amlcurran.showcaseview:library:5.4.3'
    implementation 'com.pacioianu.david:ink-page-indicator:1.2.0'
    implementation 'com.github.zcweng:switch-button:0.0.3@aar'


    // Google
    implementation 'com.google.android.gms:play-services-auth:15.0.1'

    // Firebase
    implementation 'com.google.firebase:firebase-database:16.0.1'
    implementation 'com.google.firebase:firebase-auth:16.0.2'
    implementation 'com.google.firebase:firebase-storage:16.0.1'
    implementation 'com.firebaseui:firebase-ui-database:1.2.0'
    implementation 'com.google.firebase:firebase-messaging:17.1.0'



    implementation 'com.github.bumptech.glide:glide:3.7.0'
    implementation 'de.hdodenhof:circleimageview:1.3.0'
    implementation 'com.codemybrainsout.onboarding:onboarder:1.0.4'
    implementation 'com.google.android.gms:play-services-vision:15.0.2'
    implementation 'com.huxq17.android:SwipeCardsView:1.3.5'
    implementation 'com.squareup.picasso:picasso:2.5.2'
    implementation 'com.airbnb.android:lottie:2.5.4'


}

apply plugin: 'com.google.gms.google-services'

【问题讨论】:

  • 您是否为您发布的应用程序使用了 proguard?如果是,您是否在控制台中上传了映射文件?
  • @MRah 抱歉,很遗憾,我忘记上传最新版本的映射文件。我刚刚上传了它,希望我能找到一些东西。谢谢提醒。
  • Firebase version 10.2.1 添加了对 Android 8 的支持。您是否使用该版本或更高版本的 Firebase 库进行构建?
  • @Bob 谢谢。回家后我会尝试用最新版本更新我的 gradle。
  • @Bob,我尝试将 firebase 版本更新到我得到的最新版本“请通过更新 google-services 插件的版本来修复版本冲突”。我更新了谷歌服务,但似乎没有任何效果。抱歉回复晚了。

标签: java android firebase android-studio


【解决方案1】:

这可能是由于 Android 8 Oreo 中引入了新的后台服务限制,请参阅: https://developer.android.com/about/versions/oreo/android-8.0-changes#back-all

一个建议的解决方案是使用 JobScheduler(我这样做了),但这似乎不适合 Firebase 消息接收器,因此如果有一些提示,您可能需要查看文档。

【讨论】:

  • 你能举例说明如何实现jobscheduler来启动一个抛出这个异常的服务吗?
猜你喜欢
  • 2011-06-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-03-11
  • 1970-01-01
  • 1970-01-01
  • 2015-12-21
相关资源
最近更新 更多