Android小部件背景颜色未更新

问题描述

我想动态更改小部件背景透明度。用户可以在我的 SettingsActivity 中定义一个以百分比为单位的自定义值。从我的活动中调用 updateWidget() 后,小部件不会立即更新。仅当我重新添加小部件时它才有效。

当我设置中断时,我看到 updateWidgetListView() 被调用

widget_layout_black.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/widget_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<LinearLayout
    android:layout_width="match_parent"
    android:background="@drawable/primary_rounded_background_black"
    android:layout_height="wrap_content">
    <TextView
        android:id="@+id/newTaskButton"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:clickable="false"
        android:layout_gravity="center_vertical"
        android:textIsSelectable="false"
        android:fontFamily="sans-serif-light"
        android:layout_marginBottom="2dp"
        android:paddingTop="5dp"
        android:paddingRight="10dp"
        android:paddingLeft="10dp"
        android:text="@string/tasks"
        android:background="@color/black"
        android:gravity="center_vertical"
        android:textColor="@color/white"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:drawableRight="@drawable/add" />
</LinearLayout>
<ListView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/widgetScheduledTasksListView"
    android:layout_marginRight="16dp"
    android:layout_marginLeft="16dp"
    android:layout_marginBottom="16dp"
    android:dividerHeight="0dp"
    android:divider="@null"
    android:layout_centerHorizontal="true" />

<TextView
    android:id="@+id/empty_view"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    android:text="@string/noScheduledTasks"
    android:textAppearance="?android:attr/textAppearanceMedium"
    android:textColor="#000000"
    android:visibility="gone" />

SettingsActivity.java

    public void updateWidget() {
      Intent intent = new Intent(this,WidgetProvider.class);
      intent.setAction(AppWidgetManager.ACTION_APPWIDGET_UPDATE);
      int[] ids = {R.xml.widgetinfo};
      intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS,ids);
      sendbroadcast(intent);
}

WidgetProvider.java

    @Override
public void onUpdate(Context context,AppWidgetManager appWidgetManager,int[] appWidgetIds) {
    final int numberOfWidgets = appWidgetIds.length;
    for (int i = 0; i < numberOfWidgets; ++i) {
        RemoteViews remoteViews = updateWidgetListView(context,appWidgetIds[i]);
        remoteViews.setonClickPendingIntent(R.id.newTaskButton,getPendingSelfIntent(context,ACTION_NEW));
        appWidgetManager.updateAppWidget(appWidgetIds[i],remoteViews);
    }
    super.onUpdate(context,appWidgetManager,appWidgetIds);
}

private RemoteViews updateWidgetListView(Context context,int appWidgetId) {
    RemoteViews remoteViews = new RemoteViews(context.getPackageName(),R.layout.widget_layout_black);
    Intent svcIntent = new Intent(context,WidgetService.class);
    svcIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID,appWidgetId);
    svcIntent.setData(Uri.parse(svcIntent.toUri(Intent.URI_INTENT_SCHEME)));
    remoteViews.setRemoteAdapter(appWidgetId,R.id.widgetScheduledTasksListView,svcIntent);
    remoteViews.setEmptyView(R.id.widgetScheduledTasksListView,R.id.empty_view);
    final Intent onClickIntent = new Intent(context,WidgetProvider.class);
    onClickIntent.setAction(WidgetProvider.ACTION_OPEN);
    onClickIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID,appWidgetId);
    onClickIntent.setData(Uri.parse(onClickIntent.toUri(Intent.URI_INTENT_SCHEME)));
    final PendingIntent onClickPendingIntent = PendingIntent.getbroadcast(context,onClickIntent,PendingIntent.FLAG_UPDATE_CURRENT);
    remoteViews.setPendingIntentTemplate(R.id.widgetScheduledTasksListView,onClickPendingIntent);
    remoteViews.setInt(R.id.widget_layout,"setBackgroundColor",getBackgroundWithTransparencyColor(context)); // Set background color

    return remoteViews;
}

public int getBackgroundWithTransparencyColor(Context context) {
    int white = context.getResources().getColor(R.color.white);
    int transparencyInPercent = preferences.getInt(context.getResources().getString(R.string.keyWidgetBackgroundTransparency),25);
    try {
        double value = 255.0 / 100 * (100 - transparencyInPercent);
        return ColorUtils.setAlphaComponent(white,(int) value);
    } catch (Exception e) {
        e.printstacktrace();
        return white;
    }
}

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)