如何在AppWidgetProvider中调用onUpdate()?

发布于 2024-11-16 12:11:08 字数 2624 浏览 3 评论 0原文

这是我的代码...

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, 0, active, 0);
    PendingIntent configPendingIntent = PendingIntent.getActivity(context, 0, 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 从其他类获取字符串,以便新字符串可以传递给文本视图......

有什么想法吗?

this is my code...

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, 0, active, 0);
    PendingIntent configPendingIntent = PendingIntent.getActivity(context, 0, 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);
    }
}

 }

i just want to update widget_title and widget_textview when R.id.update is clicked
it should run function getString to fetch string from other class so that, the new string can be pass to the textview...

any idea?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

痴骨ら 2024-11-23 12:11:08

在 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, active, 0);
PendingIntent configPendingIntent = PendingIntent.getActivity(context, 0, configIntent, 0);

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

appWidgetManager.updateAppWidget(ids, remoteViews );
}

我相信您还需要设置 Intents:如果您不这样做,按钮将不再更新 RemoteView 后即可工作。

In the end of the onReceive function you should be able to just create a RemoteView and update the widget:

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, active, 0);
PendingIntent configPendingIntent = PendingIntent.getActivity(context, 0, configIntent, 0);

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

appWidgetManager.updateAppWidget(ids, remoteViews );
}

I believe you will need to also set the Intents: if you do not do that the buttons will no longer work once you update the RemoteView.

冰之心 2024-11-23 12:11:08

我相信

Intent active = new Intent(context, WordWidget.class);
active.setAction(ACTION_WIDGET_RECEIVER);

如果没有用简单的字符串实例化它们的意图,则“应该

Intent active = new Intent(ACTION_WIDGET_RECEIVER);

广播待定意图”将不起作用。由于某种原因,setAction() 没有做同样的事情。

I believe

Intent active = new Intent(context, WordWidget.class);
active.setAction(ACTION_WIDGET_RECEIVER);

Should be

Intent active = new Intent(ACTION_WIDGET_RECEIVER);

Broadcast PendingIntents do not work if their intent was not instantiated with a simple string. setAction() does not do the same thing for some reason.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文