Android 9上的java.lang.IllegalStateException
最近我将API目标更新为28。我必须做一些改变。我收到了大量用户(8396)的崩溃转储错误,异常为IllegalStateExceptionAndroid 9上的java.lang.IllegalStateException,java,android,crash-reports,illegalstateexception,android-9.0-pie,Java,Android,Crash Reports,Illegalstateexception,Android 9.0 Pie,最近我将API目标更新为28。我必须做一些改变。我收到了大量用户(8396)的崩溃转储错误,异常为IllegalStateException 游戏机崩溃 WorldClockWidgetProvider.java:147在context.startService(service)报告;。从下面的功能快照 ClockWidgetProvider.java:115表示函数快照下方的“onClockTick(context);” 所有报告的设备都是Android 9和API 29 Bui
- 游戏机崩溃
- WorldClockWidgetProvider.java:147在context.startService(service)报告;。从下面的功能快照
- ClockWidgetProvider.java:115表示函数快照下方的“onClockTick(context);”
- Build.gradile代码段
- 呼叫服务
- 意图的上下文服务
public class WorldClockWidgetProvider extends ClockWidgetProvider {
private static final boolean SANS_JELLY_BEAN_MR1 = Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR1;
static {
registerClockWidget(WorldClockWidgetProvider.class);
}
@Override
protected void updateAppWidget(Context context, AppWidgetManager appWidgetManager, int appWidgetId) {
updateAppWidgetStatic(context, appWidgetManager, appWidgetId);
}
private static void updateAppWidgetStatic(Context context, AppWidgetManager appWidgetManager, int appWidgetId) {
// Create an Intent to launch WorldClockActivity
Intent intent = new Intent(context, WorldClockActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);
// Get the layout for the App Widget and attach an on-click listener
// to the button
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.world_clock_widget);
views.setOnClickPendingIntent(R.id.app_widget, pendingIntent);
// update view
updateViews(context, views);
// Tell the AppWidgetManager to perform an update on the current app widget
appWidgetManager.updateAppWidget(appWidgetId, views);
}
private static final String[] PROJECTION = {
Clocks.TIMEZONE_ID,
Clocks.CITY
};
private static final int[] CITY_IDS = {
R.id.city_text1,
R.id.city_text2,
R.id.city_text3,
R.id.city_text4,
};
private static final int[] TIME_IDS = {
R.id.time_text1,
R.id.time_text2,
R.id.time_text3,
R.id.time_text4,
};
private static void updateViews(Context context, RemoteViews views) {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
boolean autoSort = prefs.getBoolean(context.getString(R.string.auto_sort_clocks_key), true);
Cursor cursor = Clocks.widgetList(context, PROJECTION, autoSort);
try {
int n = 0;
DateFormat df = android.text.format.DateFormat.getTimeFormat(context);
long now = DateTimeUtils.currentTimeMillis();
final int maxEntries = context.getResources().getInteger(R.integer.worldclock_widget_max_entries);
while (cursor.moveToNext() && n < CITY_IDS.length
&& n < maxEntries) {
String id = cursor.getString(cursor.getColumnIndex(Clocks.TIMEZONE_ID));
String city = cursor.getString(cursor.getColumnIndex(Clocks.CITY));
views.setTextViewText(CITY_IDS[n], city);
DateTimeZone tz = DateTimeZone.forID(id);
if (SANS_JELLY_BEAN_MR1) {
views.setTextViewText(TIME_IDS[n], TimeZoneInfo.formatDate(df, tz, now));
} else {
TimeZone javaTimeZone = TimeZoneInfo.convertToJavaTimeZone(tz, now);
views.setViewVisibility(TIME_IDS[n], View.VISIBLE);
RemoteViewUtil.setTextClockTimeZone(views, TIME_IDS[n], javaTimeZone.getID());
}
n++;
}
int showEmptyText = (n == 0) ? View.VISIBLE : View.INVISIBLE;
views.setViewVisibility(R.id.empty_text, showEmptyText);
for (; n < CITY_IDS.length; n++) {
views.setTextViewText(CITY_IDS[n], "");
if (SANS_JELLY_BEAN_MR1) {
views.setTextViewText(TIME_IDS[n], "");
} else {
views.setViewVisibility(TIME_IDS[n], View.INVISIBLE);
}
}
boolean customColors = prefs.getBoolean(context.getString(R.string.use_custom_colors_key), false);
int textColor = Color.WHITE;
if (customColors) {
int color = prefs.getInt(context.getString(R.string.background_color_key), Color.BLACK);
RemoteViewUtil.setBackgroundColor(views, R.id.app_widget, color);
textColor = prefs.getInt(context.getString(R.string.foreground_color_key), Color.WHITE);
} else {
RemoteViewUtil.setBackground(views, R.id.app_widget, R.drawable.appwidget_dark_bg);
}
for (int i = 0; i < CITY_IDS.length; i++) {
views.setTextColor(CITY_IDS[i], textColor);
views.setTextColor(TIME_IDS[i], textColor);
}
} finally {
if (cursor != null) {
cursor.close();
}
}
}
@Override
protected void onClockTick(Context context) {
Intent service = new Intent(context, WorldClockWidgetService.class);
context.startService(service);
}
static void updateTime(Context context) {
// update on the hour
final long minutes = System.currentTimeMillis() / (60000);
if (minutes % 60 == 0) {
Clocks.updateOrder(context);
}
// Get the widget manager and ids for this widget provider, then call the shared
// clock update method.
ComponentName thisAppWidget = new ComponentName(context.getPackageName(), WorldClockWidgetProvider.class.getName());
AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
int[] ids = appWidgetManager.getAppWidgetIds(thisAppWidget);
for (int appWidgetID: ids) {
updateAppWidgetStatic(context, appWidgetManager, appWidgetID);
}
}
WorldClockWidgetProvider的内容
public class WorldClockWidgetProvider extends ClockWidgetProvider {
private static final boolean SANS_JELLY_BEAN_MR1 = Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR1;
static {
registerClockWidget(WorldClockWidgetProvider.class);
}
@Override
protected void updateAppWidget(Context context, AppWidgetManager appWidgetManager, int appWidgetId) {
updateAppWidgetStatic(context, appWidgetManager, appWidgetId);
}
private static void updateAppWidgetStatic(Context context, AppWidgetManager appWidgetManager, int appWidgetId) {
// Create an Intent to launch WorldClockActivity
Intent intent = new Intent(context, WorldClockActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);
// Get the layout for the App Widget and attach an on-click listener
// to the button
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.world_clock_widget);
views.setOnClickPendingIntent(R.id.app_widget, pendingIntent);
// update view
updateViews(context, views);
// Tell the AppWidgetManager to perform an update on the current app widget
appWidgetManager.updateAppWidget(appWidgetId, views);
}
private static final String[] PROJECTION = {
Clocks.TIMEZONE_ID,
Clocks.CITY
};
private static final int[] CITY_IDS = {
R.id.city_text1,
R.id.city_text2,
R.id.city_text3,
R.id.city_text4,
};
private static final int[] TIME_IDS = {
R.id.time_text1,
R.id.time_text2,
R.id.time_text3,
R.id.time_text4,
};
private static void updateViews(Context context, RemoteViews views) {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
boolean autoSort = prefs.getBoolean(context.getString(R.string.auto_sort_clocks_key), true);
Cursor cursor = Clocks.widgetList(context, PROJECTION, autoSort);
try {
int n = 0;
DateFormat df = android.text.format.DateFormat.getTimeFormat(context);
long now = DateTimeUtils.currentTimeMillis();
final int maxEntries = context.getResources().getInteger(R.integer.worldclock_widget_max_entries);
while (cursor.moveToNext() && n < CITY_IDS.length
&& n < maxEntries) {
String id = cursor.getString(cursor.getColumnIndex(Clocks.TIMEZONE_ID));
String city = cursor.getString(cursor.getColumnIndex(Clocks.CITY));
views.setTextViewText(CITY_IDS[n], city);
DateTimeZone tz = DateTimeZone.forID(id);
if (SANS_JELLY_BEAN_MR1) {
views.setTextViewText(TIME_IDS[n], TimeZoneInfo.formatDate(df, tz, now));
} else {
TimeZone javaTimeZone = TimeZoneInfo.convertToJavaTimeZone(tz, now);
views.setViewVisibility(TIME_IDS[n], View.VISIBLE);
RemoteViewUtil.setTextClockTimeZone(views, TIME_IDS[n], javaTimeZone.getID());
}
n++;
}
int showEmptyText = (n == 0) ? View.VISIBLE : View.INVISIBLE;
views.setViewVisibility(R.id.empty_text, showEmptyText);
for (; n < CITY_IDS.length; n++) {
views.setTextViewText(CITY_IDS[n], "");
if (SANS_JELLY_BEAN_MR1) {
views.setTextViewText(TIME_IDS[n], "");
} else {
views.setViewVisibility(TIME_IDS[n], View.INVISIBLE);
}
}
boolean customColors = prefs.getBoolean(context.getString(R.string.use_custom_colors_key), false);
int textColor = Color.WHITE;
if (customColors) {
int color = prefs.getInt(context.getString(R.string.background_color_key), Color.BLACK);
RemoteViewUtil.setBackgroundColor(views, R.id.app_widget, color);
textColor = prefs.getInt(context.getString(R.string.foreground_color_key), Color.WHITE);
} else {
RemoteViewUtil.setBackground(views, R.id.app_widget, R.drawable.appwidget_dark_bg);
}
for (int i = 0; i < CITY_IDS.length; i++) {
views.setTextColor(CITY_IDS[i], textColor);
views.setTextColor(TIME_IDS[i], textColor);
}
} finally {
if (cursor != null) {
cursor.close();
}
}
}
@Override
protected void onClockTick(Context context) {
Intent service = new Intent(context, WorldClockWidgetService.class);
context.startService(service);
}
static void updateTime(Context context) {
// update on the hour
final long minutes = System.currentTimeMillis() / (60000);
if (minutes % 60 == 0) {
Clocks.updateOrder(context);
}
// Get the widget manager and ids for this widget provider, then call the shared
// clock update method.
ComponentName thisAppWidget = new ComponentName(context.getPackageName(), WorldClockWidgetProvider.class.getName());
AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
int[] ids = appWidgetManager.getAppWidgetIds(thisAppWidget);
for (int appWidgetID: ids) {
updateAppWidgetStatic(context, appWidgetManager, appWidgetID);
}
}
公共类WorldClockWidgetProvider扩展了ClockWidgetProvider{
私有静态最终布尔值SANS\u JELLY\u BEAN\u MR1=Build.VERSION.SDK\u INT@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");
}
public class WorldClockWidgetService extends IntentService {
public WorldClockWidgetService() {
super("WorldClockWidgetService");
}
@Override
protected void onHandleIntent(Intent intent) {
WorldClockWidgetProvider.updateTime(this);
}
}
public class WorldClockWidgetService extends JobIntentService {
public WorldClockWidgetService() {
super("WorldClockWidgetService");
}
@Override
protected void onHandleWork(Intent intent) {
WorldClockWidgetProvider.updateTime(this);
}
}
public class WorldClockWidgetProvider extends ClockWidgetProvider {
private static final boolean SANS_JELLY_BEAN_MR1 = Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR1;
static {
registerClockWidget(WorldClockWidgetProvider.class);
}
@Override
protected void updateAppWidget(Context context, AppWidgetManager appWidgetManager, int appWidgetId) {
updateAppWidgetStatic(context, appWidgetManager, appWidgetId);
}
private static void updateAppWidgetStatic(Context context, AppWidgetManager appWidgetManager, int appWidgetId) {
// Create an Intent to launch WorldClockActivity
Intent intent = new Intent(context, WorldClockActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);
// Get the layout for the App Widget and attach an on-click listener
// to the button
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.world_clock_widget);
views.setOnClickPendingIntent(R.id.app_widget, pendingIntent);
// update view
updateViews(context, views);
// Tell the AppWidgetManager to perform an update on the current app widget
appWidgetManager.updateAppWidget(appWidgetId, views);
}
private static final String[] PROJECTION = {
Clocks.TIMEZONE_ID,
Clocks.CITY
};
private static final int[] CITY_IDS = {
R.id.city_text1,
R.id.city_text2,
R.id.city_text3,
R.id.city_text4,
};
private static final int[] TIME_IDS = {
R.id.time_text1,
R.id.time_text2,
R.id.time_text3,
R.id.time_text4,
};
private static void updateViews(Context context, RemoteViews views) {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
boolean autoSort = prefs.getBoolean(context.getString(R.string.auto_sort_clocks_key), true);
Cursor cursor = Clocks.widgetList(context, PROJECTION, autoSort);
try {
int n = 0;
DateFormat df = android.text.format.DateFormat.getTimeFormat(context);
long now = DateTimeUtils.currentTimeMillis();
final int maxEntries = context.getResources().getInteger(R.integer.worldclock_widget_max_entries);
while (cursor.moveToNext() && n < CITY_IDS.length
&& n < maxEntries) {
String id = cursor.getString(cursor.getColumnIndex(Clocks.TIMEZONE_ID));
String city = cursor.getString(cursor.getColumnIndex(Clocks.CITY));
views.setTextViewText(CITY_IDS[n], city);
DateTimeZone tz = DateTimeZone.forID(id);
if (SANS_JELLY_BEAN_MR1) {
views.setTextViewText(TIME_IDS[n], TimeZoneInfo.formatDate(df, tz, now));
} else {
TimeZone javaTimeZone = TimeZoneInfo.convertToJavaTimeZone(tz, now);
views.setViewVisibility(TIME_IDS[n], View.VISIBLE);
RemoteViewUtil.setTextClockTimeZone(views, TIME_IDS[n], javaTimeZone.getID());
}
n++;
}
int showEmptyText = (n == 0) ? View.VISIBLE : View.INVISIBLE;
views.setViewVisibility(R.id.empty_text, showEmptyText);
for (; n < CITY_IDS.length; n++) {
views.setTextViewText(CITY_IDS[n], "");
if (SANS_JELLY_BEAN_MR1) {
views.setTextViewText(TIME_IDS[n], "");
} else {
views.setViewVisibility(TIME_IDS[n], View.INVISIBLE);
}
}
boolean customColors = prefs.getBoolean(context.getString(R.string.use_custom_colors_key), false);
int textColor = Color.WHITE;
if (customColors) {
int color = prefs.getInt(context.getString(R.string.background_color_key), Color.BLACK);
RemoteViewUtil.setBackgroundColor(views, R.id.app_widget, color);
textColor = prefs.getInt(context.getString(R.string.foreground_color_key), Color.WHITE);
} else {
RemoteViewUtil.setBackground(views, R.id.app_widget, R.drawable.appwidget_dark_bg);
}
for (int i = 0; i < CITY_IDS.length; i++) {
views.setTextColor(CITY_IDS[i], textColor);
views.setTextColor(TIME_IDS[i], textColor);
}
} finally {
if (cursor != null) {
cursor.close();
}
}
}
@Override
protected void onClockTick(Context context) {
Intent service = new Intent(context, WorldClockWidgetService.class);
context.startService(service);
}
static void updateTime(Context context) {
// update on the hour
final long minutes = System.currentTimeMillis() / (60000);
if (minutes % 60 == 0) {
Clocks.updateOrder(context);
}
// Get the widget manager and ids for this widget provider, then call the shared
// clock update method.
ComponentName thisAppWidget = new ComponentName(context.getPackageName(), WorldClockWidgetProvider.class.getName());
AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
int[] ids = appWidgetManager.getAppWidgetIds(thisAppWidget);
for (int appWidgetID: ids) {
updateAppWidgetStatic(context, appWidgetManager, appWidgetID);
}
}