从活动更新小部件并将其设置为可单击不起作用

时间:2014-06-04 19:05:37

标签: android android-intent android-widget android-broadcast

我尝试创建一个更新窗口小部件的配置活动。我也想让小部件可以点击。我设法更新了小部件,但它不会让我点击它(没有任何事情发生)。

这是我的代码。

private void generate() {

    // Create an Intent to launch WidgetConfigurationActivity screen
    Intent intent = new Intent(context, ConfigureWidgetActivity.class);

    intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, mAppWidgetId);

    // This is needed to make this intent different from its previous intents
    intent.setData(Uri.parse("tel:/" + (int) System.currentTimeMillis()));

    // Creating a pending intent, which will be invoked when the user
    // clicks on the widget
    PendingIntent pendingIntent = PendingIntent.getActivity(context, 0,
            intent, PendingIntent.FLAG_UPDATE_CURRENT);

    // Getting an instance of WidgetManager
    AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);

    // Instantiating the class RemoteViews with widget_layout
    RemoteViews views = new RemoteViews(getBaseContext().getPackageName(), R.layout.widget_layout);

    //  Attach an on-click listener to the widget
    views.setOnClickPendingIntent(R.id.widget, pendingIntent);

    // Tell the AppWidgetManager to perform an update on the app widget
     appWidgetManager.updateAppWidget(mAppWidgetId, views);

    //-----BROADCAST
    Intent refreshIntent = new Intent(this, WidgetProvider.class);
    refreshIntent.putExtra("parser", parser);
    sendBroadcast(refreshIntent);
    //-----

    // Return RESULT_OK from this activity
    Intent resultValue = new Intent();
    resultValue.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, mAppWidgetId);
    setResult(RESULT_OK, resultValue);
    finish();
}

现在,如果我把这段代码放在评论中

Intent refreshIntent = new Intent(this, WidgetProvider.class);
refreshIntent.putExtra("parser", parser);
sendBroadcast(refreshIntent);

小部件当然不会更新,但可以点击。

当我这样做时

//----FIRST NOW
Intent refreshIntent = new Intent(this, WidgetProvider.class);
refreshIntent.putExtra("parser", parser);
sendBroadcast(refreshIntent);

//----THIS COME AFTER
//  Attach an on-click listener to the widget
views.setOnClickPendingIntent(R.id.widget, pendingIntent);

// Tell the AppWidgetManager to perform an update on the app widget
appWidgetManager.updateAppWidget(mAppWidgetId, views);    

这是我的AppWidgetProvider(以防万一)

public class WidgetProvider extends AppWidgetProvider {
/** https://stackoverflow.com/questions/17434898/sending-data-to-my-homescreen-widget */
@Override
public void onReceive(Context context, Intent intent) {

    Parser parser = (Parser) intent.getSerializableExtra("parser");

    final AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
    ComponentName name = new ComponentName(context, WidgetProvider.class);
    int[] appWidgetId = AppWidgetManager.getInstance(context).getAppWidgetIds(name);
    final int N = appWidgetId.length;
    if (N < 1)
    {
        return ;
    }
    else {
        int id = appWidgetId[N-1];
        updateWidget(context, appWidgetManager, id , parser);
    }
}

static void updateWidget(Context context, AppWidgetManager appWidgetManager, int appWidgetId,
                         Parser parser){

    RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget_layout);
    if (parser != null) {
        views.setTextViewText(R.id.regio, parser.getRegio());
        views.setTextViewText(R.id.max, parser.getMax());
        views.setTextViewText(R.id.min, parser.getMin());
        views.setTextViewText(R.id.windf, parser.getWindf());
        views.setImageViewResource(R.id.wind, parser.getWind());
        views.setImageViewResource(R.id.first, parser.getFirst());
        views.setImageViewResource(R.id.second, parser.getSecond());
    }

    appWidgetManager.updateAppWidget(appWidgetId, views);
}
}

我认为这是愚蠢的事情:)。

发现了一些东西(与我的代码相同?) - How to update a widget on app start

1 个答案:

答案 0 :(得分:0)

好吧,我解决了我的问题。

private void generate() {

    // Getting an instance of WidgetManager
    AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(getBaseContext());

    WidgetProvider.updateWidget(context, appWidgetManager, mAppWidgetId, parser);

    // Return RESULT_OK from this activity
    Intent resultValue = new Intent();
    resultValue.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, mAppWidgetId);
    setResult(RESULT_OK, resultValue);
    finish();
}

用于绑定窗口小部件中的单击的所有代码都在提供程序中:

static void updateWidget(Context context, AppWidgetManager appWidgetManager, int appWidgetId,
                         Parser parser){
    Toast.makeText(context, "updateWidget", Toast.LENGTH_LONG).show();
    RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget_layout);

    if (parser != null) {
        views.setTextViewText(R.id.regio, parser.getRegio());
        views.setTextViewText(R.id.max, parser.getMax());
        views.setTextViewText(R.id.min, parser.getMin());
        views.setTextViewText(R.id.windf, parser.getWindf());
        views.setImageViewResource(R.id.wind, parser.getWind());
        views.setImageViewResource(R.id.first, parser.getFirst());
        views.setImageViewResource(R.id.second, parser.getSecond());
    }

    // Create an Intent to launch WidgetConfigurationActivity screen
    Intent intent = new Intent(context, ConfigureWidgetActivity.class);

    intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);

    // This is needed to make this intent different from its previous intents
    intent.setData(Uri.parse("tel:/" + (int) System.currentTimeMillis()));

    // Creating a pending intent, which will be invoked when the user
    // clicks on the widget
    PendingIntent pendingIntent = PendingIntent.getActivity(context, 0,
            intent, PendingIntent.FLAG_UPDATE_CURRENT);

    // Getting an instance of WidgetManager
    appWidgetManager = AppWidgetManager.getInstance(context);

    //  Attach an on-click listener to the widget
    views.setOnClickPendingIntent(R.id.widget, pendingIntent);

    appWidgetManager.updateAppWidget(appWidgetId, views);
}

这解决了我的问题。我不知道这是否是正确的做法;)