Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/201.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android小部件,启动意图_Android_Android Widget_Android Intent - Fatal编程技术网

Android小部件,启动意图

Android小部件,启动意图,android,android-widget,android-intent,Android,Android Widget,Android Intent,我正在尝试制作一个Android小部件,以显示我应用程序中的一些信息 我已经成功地显示了一些信息,但我知道我的应用程序中应该有一个“切换页面”的功能。这意味着按下一个按钮,就会显示新的信息。但是我该怎么做呢?我尝试了不同的方法,最终得到了以下代码: public class HelloWidget extends AppWidgetProvider { RemoteViews remoteViews; @Override public void onUpdate(Con

我正在尝试制作一个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);
    }
}
土司显示“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>

我做错了什么


谢谢

您的TestReceiver是HelloWidget的内部类吗

然后您没有在清单中指定正确的名称