0

我想在我的小部件中添加一个按钮,以便它可以通过按下一个按钮来运行一个功能(更改 APN 并在小部件中显示更改的消息)。实际上,自从我在活动中进行了测试以来,该功能就可以使用。有人知道怎么做吗?谢谢!

问候, 陈比尔

4

1 回答 1

1

嗯,有几种方法。

例如,您可以在注册的 AppWidgetProvider 中进行类似的操作:

public static String ACTION_WIDGET_RECEIVER = "Action!";

@Override
    public void onUpdate(Context context, AppWidgetManager appWidgetManager,
            int[] appWidgetIds) {
        RemoteViews remoteViews = new RemoteViews(context.getPackageName(),
                R.layout.widget_layout);

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

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

        remoteViews.setOnClickPendingIntent(R.id.button, actionPendingIntent);


        appWidgetManager.updateAppWidget(appWidgetIds, remoteViews);
    }

    @Override
    public void onReceive(Context context, Intent intent) {
        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 {
            if (intent.getAction().equals(ACTION_WIDGET_RECEIVER)) {
                doYourStuff();
            super.onReceive(context, intent);
        }
    }
于 2012-05-23T02:20:22.820 回答