【问题标题】:Android widget, start intentAndroid 小部件,启动意图
【发布时间】:2011-05-12 12:27:13
【问题描述】:

我正在尝试制作一个 Android 小部件,以显示我的应用中的一些信息。

我已经成功地展示了一些信息,但我知道我的应用程序中有什么功能可以“切换页面”。这意味着按下一个按钮,就会显示新信息。但我该怎么做?我尝试了不同的方法,最终得到以下代码:

public class HelloWidget extends AppWidgetProvider {
    RemoteViews remoteViews;

    @Override
    public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
        Toast.makeText(context, "onUpdate", Toast.LENGTH_SHORT).show();

        RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget_layout);

        Intent intent = new Intent();
        intent.setAction(TestReceiver.TEST_INTENT);
        intent.setClassName(TestReceiver.class.getPackage().getName(), TestReceiver.class.getName());
        PendingIntent pendingIntent = PendingIntent.getBroadcast(context.getApplicationContext(), 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);

        remoteViews.setOnClickPendingIntent(R.id.bbutton1, pendingIntent);
        remoteViews.setTextViewText(R.id.widgetTxt, "The textview");
        appWidgetManager.updateAppWidget(appWidgetIds[0], remoteViews);

        super.onUpdate(context, appWidgetManager, appWidgetIds);
    }

    public class TestReceiver extends BroadcastReceiver {
        public static final String TEST_INTENT = "MyTestIntent";

        @Override
        public void onReceive(Context context, Intent intent) {
            Toast.makeText(context, "You reached the TestReceiver", Toast.LENGTH_SHORT).show();

            if (intent.getAction() == TEST_INTENT) {
                remoteViews.setTextViewText(R.id.widgetTxt, "Is changed");
            }
        }
    }

    @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 {
            super.onReceive(context, intent);
        }
    }

    @Override
    public void onDeleted(Context context, int[] appWidgetIds) {
        Toast.makeText(context, "onDelete", Toast.LENGTH_SHORT).show();
        super.onDeleted(context, appWidgetIds);
    }
}

Toast 显示“onUpdate”,然后我在“onDeleted”将其删除。文本视图在 onUpdate 中也发生了变化,但是当我按下按钮时没有任何反应。

我的清单如下所示:

    <receiver android:name=".HelloWidget" android:label="@string/app_name">
        <intent-filter>
            <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
        </intent-filter>
        <meta-data android:name="android.appwidget.provider"
            android:resource="@xml/hello_widget_provider" />
    </receiver>

    <receiver android:name=".TestReceiver" android:label="@string/app_name">
        <intent-filter>
            <action android:name="MyTestIntent">
            </action>
        </intent-filter>
    </receiver>

我做错了什么?

谢谢

【问题讨论】:

    标签: android android-widget android-intent


    【解决方案1】:

    您的 TestReceiver 是 HelloWidget 的内部类吗?

    那么你没有在清单中指定正确的名称。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-03-07
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多