java - android.app.ContextImpl.startServiceCommon 处的 java.lang.IllegalStateException
问题描述
我在 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());
}
}
}
摇篮文件:
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'
解决方案
这可能是由于 Android 8 Oreo 中引入了新的后台服务限制,请参阅: https ://developer.android.com/about/versions/oreo/android-8.0-changes#back-all
一个建议的解决方案是使用 JobScheduler(我做过),但这似乎不适合 Firebase 消息接收器,因此如果有一些提示,您可能需要查看文档。
推荐阅读
- assembly - 是否有一个 ida 插件可以在 Visual c 内联汇编中反汇编?
- ruby-on-rails - 调用 FactoryBot.create 时是否运行 after_update?
- r - 数据框:在几行中的单元格中单独的ID
- html - 如何使背景图像透明并且上面写的文字应该突出显示
- python - 使用 Python 将音频文件发送到 DialogFlow
- angularjs - 如何使用指令表达式`&`绑定调用控制器函数
- html - IE11 flexbox 不考虑换行文本的高度
- macos - 如何使用 GoLang 以编程方式禁用 Mac 终端的回显
- git - 使用名称中的特定字符串构建分支
- vbscript - 我如何在 VBscript 中制作一个假的进度条