android – 如何在AppWidgetProvider中调用onUpdate()?

前端之家收集整理的这篇文章主要介绍了android – 如何在AppWidgetProvider中调用onUpdate()?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

这是我的代码……

String[] show = new String[2];
RemoteViews remoteViews;
public static String ACTION_WIDGET_CONFIGURE = "ConfigureWidget";
public static String ACTION_WIDGET_RECEIVER = "ActionReceiverWidget";   

@Override
public void onUpdate( Context context,AppWidgetManager appWidgetManager,int[] appWidgetIds )
{
    show = getString();

    remoteViews = new RemoteViews( context.getPackageName(),R.layout.widget );
    remoteViews.setTextViewText( R.id.widget_title,show[0]);
    remoteViews.setTextViewText( R.id.widget_textview,show[1]);

    Intent configIntent = new Intent(context,SuitAuto.class);
    configIntent.setAction(ACTION_WIDGET_CONFIGURE);

    Intent active = new Intent(context,WordWidget.class);
    active.setAction(ACTION_WIDGET_RECEIVER);
    active.putExtra("msg","Message for Button 1");

    PendingIntent actionPendingIntent = PendingIntent.getBroadcast(context,active,0);
    PendingIntent configPendingIntent = PendingIntent.getActivity(context,configIntent,0);

    remoteViews.setOnClickPendingIntent(R.id.update,actionPendingIntent);
    remoteViews.setOnClickPendingIntent(R.id.Lilo,configPendingIntent);

    appWidgetManager.updateAppWidget(appWidgetIds,remoteViews );
}

private String[] getString() {
    String[] hon = new String[2];
    KamusWidget ad = new KamusWidget(null);
    ad.open();
    hon = ad.getwordday();
    ad.close();
    return hon;
}

@Override
public void onReceive(Context context,Intent intent) {
    // v1.5 fix that doesn't call onDelete Action
    final String action = intent.getAction();
    if (AppWidgetManager.ACTION_APPWIDGET_DELETED.equals(action)) {
        final int appWidgetId = intent.getExtras().getInt(
                AppWidgetManager.EXTRA_APPWIDGET_ID,AppWidgetManager.INVALID_APPWIDGET_ID);

        if (appWidgetId != AppWidgetManager.INVALID_APPWIDGET_ID) {
            this.onDeleted(context,new int[] { appWidgetId });
        }
    } else {
        // check,if our Action was called
        if (intent.getAction().equals(ACTION_WIDGET_RECEIVER)) {
            String msg = "null";
            try {
                msg = intent.getStringExtra("msg");
            } catch (NullPointerException e) {
                Log.e("Error","msg = null");
            }
            Toast.makeText(context,msg,Toast.LENGTH_SHORT).show();


        } else {
            // do nothing
        }

        super.onReceive(context,intent);
    }
}

 }

我只想在单击R.id.update时更新widget_title和widget_textview
它应该运行函数getString来从其他类中获取字符串,以便新的字符串可以传递给textview …

任何的想法?

最佳答案
在onReceive函数的最后,你应该能够创建一个RemoteView并更新小部件:

ComponentName name = new ComponentName(context,WordWidget.class);
int[] ids = appWidgetManager.getAppWidgetIds(name);
if(ids != null && ids.length > 0){
RemoteView remoteViews = new RemoteViews( context.getPackageName(),R.layout.widget );
remoteViews.setTextViewText( R.id.widget_title,context.getString(R.id.new_widget_title));
remoteViews.setTextViewText( R.id.widget_textview,context.getString(R.id.new_widget_text));

Intent configIntent = new Intent(context,SuitAuto.class);
configIntent.setAction(ACTION_WIDGET_CONFIGURE);

Intent active = new Intent(context,WordWidget.class);
active.setAction(ACTION_WIDGET_RECEIVER);
active.putExtra("msg","Message for Button 1");

PendingIntent actionPendingIntent = PendingIntent.getBroadcast(context,0);
PendingIntent configPendingIntent = PendingIntent.getActivity(context,0);

remoteViews.setOnClickPendingIntent(R.id.update,actionPendingIntent);
remoteViews.setOnClickPendingIntent(R.id.Lilo,configPendingIntent);

appWidgetManager.updateAppWidget(ids,remoteViews );
}

我相信您还需要设置Intents:如果不这样做,一旦更新RemoteView,按钮将不再有效.

猜你在找的Android相关文章