我开始发疯试图弄清楚这一点。看起来应该很容易,我开始怀疑是否有可能。
我想要做的是创建一个主屏幕小部件,它只包含一个 ImageButton。当它被按下时,想法是改变一些设置(比如 wi-fi 切换),然后改变按钮图像。
我在 main.xml 中有这样声明的 ImageButton
<ImageButton android:id="@+id/buttonOne"
android:src="@drawable/button_normal_ringer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center" />
Run Code Online (Sandbox Code Playgroud)
我的 AppWidgetProvider 类,名为 ButtonWidget
*** 请注意, RemoteViews 类是本地存储的变量。这让我可以访问 RViews 布局元素......或者我是这么想的。
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
int[] appWidgetIds) {
remoteViews = new RemoteViews(context.getPackageName(),
R.layout.main);
Intent active = new Intent(context, ButtonWidget.class);
active.setAction(VIBRATE_UPDATE);
active.putExtra("msg","TESTING");
PendingIntent actionPendingIntent = PendingIntent.getBroadcast(context,
0, active, 0);
remoteViews.setOnClickPendingIntent(R.id.buttonOne,
actionPendingIntent);
appWidgetManager.updateAppWidget(appWidgetIds, remoteViews);
}
@Override
public void onReceive(Context context, Intent intent) {
// v1.5 fix that doesn't call onDelete Action
final String action …Run Code Online (Sandbox Code Playgroud)