最近,我已将API目标更新为28。我必须做几处更改。我从IllegalStateException异常的大量用户(8,396)中收到崩溃转储错误。
mpirun
a.out
java.lang.RuntimeException: at android.app.ActivityThread.handleReceiver (ActivityThread.java:3584) at android.app.ActivityThread.access$1300 (ActivityThread.java:235) at android.app.ActivityThread$H.handleMessage (ActivityThread.java:1779) at android.os.Handler.dispatchMessage (Handler.java:106) at android.os.Looper.loop (Looper.java:214) at android.app.ActivityThread.main (ActivityThread.java:6981) at java.lang.reflect.Method.invoke (Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run (RuntimeInit.java:493) at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:1445) Caused by: java.lang.IllegalStateException: at android.app.ContextImpl.startServiceCommon (ContextImpl.java:1666) at android.app.ContextImpl.startService (ContextImpl.java:1611) at android.content.ContextWrapper.startService (ContextWrapper.java:677) at android.content.ContextWrapper.startService (ContextWrapper.java:677) at ch.corten.aha.worldclock.WorldClockWidgetProvider.onClockTick (WorldClockWidgetProvider.java:147) at ch.corten.aha.worldclock.ClockWidgetProvider.onReceive (ClockWidgetProvider.java:115) at android.app.ActivityThread.handleReceiver (ActivityThread.java:3575)
所有报告的设备均为Android 9和API 29。
@Override protected void onClockTick(Context context) { Intent service = new Intent(context, WorldClockWidgetService.class); context.startService(service); }
@Override public void onReceive(Context context, Intent intent) { Log.e(TAG, "Onrecive called Biswajit"); super.onReceive(context, intent); if (WIDGET_DATA_CHANGED_ACTION.equals(intent.getAction()) || CLOCK_TICK_ACTION.equals(intent.getAction())) { PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE); if (pm.isScreenOn()) { onClockTick(context); } } }
建议的解决方案?:
dependencies { //compile 'com.actionbarsherlock:actionbarsherlock:4.4.0@aar' //compile 'com.android.support:support-v4:19.1.0' implementation 'com.github.javiersantos:AppUpdater:2.7' implementation 'com.google.code.gson:gson:2.8.2' implementation 'net.danlew:android.joda:2.9.4.1' implementation 'com.google.android.gms:play-services-ads:9.8.0' //noinspection GradleCompatible implementation 'com.android.support:appcompat-v7:22.2.1' implementation 'com.facebook.android:audience-network-sdk:4.28.2' }
将由
替换的
import android.content.Intent; public class WorldClockWidgetService extends IntentService { public WorldClockWidgetService() { super("WorldClockWidgetService"); }
错误:出现错误
WorldClockWidgetProvider的内容
public class WorldClockWidgetService extends IntentService {
public WorldClockWidgetService() {
super("WorldClockWidgetService");
}
@Override
protected void onHandleIntent(Intent intent) {
WorldClockWidgetProvider.updateTime(this);
}
}
}
答案 0 :(得分:1)
如以下链接所述:
https://developer.android.com/about/versions/oreo/android-8.0-changes#back-all
Android 8.0(API级别26)还包括以下更改: 具体方法:
现在,
startService()
方法会抛出IllegalStateException
针对Android 8.0的应用尝试在以下情况下使用该方法 不允许创建后台服务。
https://developer.android.com/about/versions/oreo/background.html
注意:
IntentService
是一项服务,因此需要遵守新的 对后台服务的限制。结果,许多依赖 定位Android 8.0或 更高。因此,Android支持库26.0.0引入了 新的JobIntentService
类,它提供的功能与 IntentService,但在运行时使用作业而不是服务 Android 8.0或更高版本。
因此,您需要查看JobIntentService和JobScheduler类,并用它替换旧的后台服务代码。
有关此类工作的示例,请参见https://developer.android.com/reference/android/support/v4/app/JobIntentService。