如何在android小部件中设置onclicklistner?

时间:2017-07-13 13:12:13

标签: android

MyCollectionWidgetProvider

public class CollectionWidget扩展AppWidgetProvider {     private static final String MyOnClick =" myOnClickTag&#34 ;;

void updateAppWidget(Context context, AppWidgetManager appWidgetManager,
                     int appWidgetId) {
    RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.collection_widget);

// views.setTextViewText(R.id.appwidget_text,widgetText);

    // Set up the collection
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
        setRemoteAdapter(context, views);
    } else {
        setRemoteAdapterV11(context, views);
    }
    // Instruct the widget manager to update the widget
    appWidgetManager.updateAppWidget(appWidgetId, views);

    views.setOnClickPendingIntent(android.R.id.text1,
            getPendingSelfIntent(context, MyOnClick));



}

@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
    // There may be multiple widgets active, so update all of them
    for (int appWidgetId : appWidgetIds) {
        updateAppWidget(context, appWidgetManager, appWidgetId);
    }
    super.onUpdate(context, appWidgetManager, appWidgetIds);
}

@Override
public void onEnabled(Context context) {
    // Enter relevant functionality for when the first widget is created
}

@Override
public void onDisabled(Context context) {
    // Enter relevant functionality for when the last widget is disabled
}

/**
 * Sets the remote adapter used to fill in the list items
 *
 * @param views RemoteViews to set the RemoteAdapter
 */
@TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH)
private static void setRemoteAdapter(Context context, @NonNull final RemoteViews views) {
    views.setRemoteAdapter(R.id.widget_list,
            new Intent(context, WidgetService.class));
}

/**
 * Sets the remote adapter used to fill in the list items
 *
 * @param views RemoteViews to set the RemoteAdapter
 */
@SuppressWarnings("deprecation")
private static void setRemoteAdapterV11(Context context, @NonNull final RemoteViews views) {
    views.setRemoteAdapter(0, R.id.widget_list,
            new Intent(context, WidgetService.class));


}
protected PendingIntent getPendingSelfIntent(Context context, String action) {
    Intent intent = new Intent(context, getClass());
    intent.setAction(action);
    return PendingIntent.getBroadcast(context, 0, intent, 0);
}

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

    if (MyOnClick.equals(intent.getAction())){
        Toast.makeText(context, "clicked", Toast.LENGTH_SHORT).show();        }
}

}

WidgetDataProvider

public class WidgetDataProvider实现RemoteViewsService.RemoteViewsFactory {

List<String> mCollection = new ArrayList<>();
Context mContext = null;

public WidgetDataProvider(Context context, Intent intent) {
    mContext = context;
}

@Override
public void onCreate() {
    initData();
}

@Override
public void onDataSetChanged() {
    initData();
}

@Override
public void onDestroy() {

}

@Override
public int getCount() {
    return mCollection.size();
}

@Override
public RemoteViews getViewAt(int position) {
    RemoteViews view = new RemoteViews(mContext.getPackageName(),
            android.R.layout.simple_list_item_1);
    view.setTextViewText(android.R.id.text1, mCollection.get(position));




    return view;
}

@Override
public RemoteViews getLoadingView() {
    return null;
}

@Override
public int getViewTypeCount() {
    return 1;
}

@Override
public long getItemId(int position) {
    return position;
}

@Override
public boolean hasStableIds() {
    return true;
}

private void initData() {
    mCollection.clear();
    for (int i = 1; i <= 10; i++) {
        mCollection.add("ListView item " + i);
    }
}

}

1 个答案:

答案 0 :(得分:0)

在您的Broadcast Receiver类中,捕获Intent的附加内容并根据附加内容,使用更新的UI更改再次创建RemoteView。