android设置系统语言,字体大小,字体样式导致widget控件失效

在setting中设置系统的字体大小,样式,以及系统语言等等会对widget产生影响,造成widget控件失效。

由于在setting中设置系统的字体大小,样式,以及系统语言等等系统发出 android.appwidget.action.APPWIDGET_UPDATE的 广播,

此时的解决方案如下:

自定义的MyAppWidgetProvider继承于AppWidgetProvider,监听android.appwidget.action.APPWIDGET_UPDATE广播

在该类的onReceive()回调方法中,重新注册控件监听。

private void initListener(Context context){
		AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
		int[] appWidgetIds = appWidgetManager.getAppWidgetIds(new ComponentName(context, MyAppWidgetProvider.class));
		RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.appwidget_myprovider);

		Intent intent;
		PendingIntent pendingIntent;
		ComponentName serviceName = new ComponentName(context, MyAppWidgetService.class);

		/** 刷新按钮 */
		intent = new Intent(MyAppWidgetService.MSG_REFRESH);
		intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, appWidgetIds);
		intent.setComponent(serviceName);
		pendingIntent = PendingIntent.getService(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
		views.setOnClickPendingIntent(R.id.appwidget_refresh_new, pendingIntent);

		/** 前一页 */
		intent = new Intent(MyAppWidgetService.MSG_PRE);
		intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, appWidgetIds);
		intent.setComponent(serviceName);
		pendingIntent = PendingIntent.getService(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
		views.setOnClickPendingIntent(R.id.appwidget_last, pendingIntent);

		/** 后一页 */
		intent = new Intent(MyAppWidgetService.MSG_NEXT);
		intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, appWidgetIds);
		intent.setComponent(serviceName);
		pendingIntent = PendingIntent.getService(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
		views.setOnClickPendingIntent(R.id.appwidget_next, pendingIntent);

		appWidgetManager.updateAppWidget(appWidgetIds, views);
	}

 

@Override
	public void onReceive(Context context, Intent intent) {
		super.onReceive(context, intent);
				initListener(context);
	}



 

你可能感兴趣的:(android,android,android,widget)