我试图避免在这里发布问题,因为我通常会为自己解决大部分问题,但我认为我已经碰到了其中一个编程砖墙。很抱歉询问之前是否有人问过这个问题(我已经用谷歌搜索,直到我全部用谷歌搜索)并且我也在这里搜索过,但我在以下问题的示例中几乎没有找到。
是否可以创建一个小部件,其中包含两个或多个按钮,例如 Android 中的按钮,您可以在其中打开和关闭 wifi 蓝牙等。我正在努力研究如何在单击时识别每个按钮(当然使用服务) 在阅读时,我得到的印象是 appwidgetids 在使用时会明显改变。
因此,例如,我有两个按钮,单击时我需要它们按各自的类别写入 sqlite 数据库。例如食品和饮料类别。
显然我可以让一个单独的按钮工作,但使用两个或更多我看不出如何。创建两个提供程序和服务只需创建同一组按钮的副本,然后更改它所基于的那个。
很抱歉缺少代码,因为我在网上浏览了这么多示例,我不会认为在这里放置任何示例会有所帮助。
编辑*
自从发布此消息以来,我仍然继续尝试解决此问题,我认为我已经使用以下链接作为示例找到了正确的方法:[ http://shkspr.mobi/blog/2010/11/howto-android-audio-widget /][1]
目前我的代码如下:
AppWidgetProvider(请原谅注释行):
package co.uk.timcs.myappwidget;
import co.uk.timcs.myappwidget.R;
import android.appwidget.AppWidgetProvider;
import java.util.Random;
import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.widget.RemoteViews;
public class MyWidgetProvider extends AppWidgetProvider {
// private static final String LOG = "de.vogella.android.widget.example";
public static String ACTION_WIDGET_CLICK = "CLICK1";
public static String ClickText2 = "CLICK2";
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
int[] appWidgetIds) {
// // Get all ids
// ComponentName thisWidget = new ComponentName(context,
// MyWidgetProvider.class);
//
// int[] allWidgetIds = appWidgetManager.getAppWidgetIds(thisWidget);
// for (int widgetId : allWidgetIds) {
// // Create some random data
RemoteViews remoteViews = new RemoteViews(context.getPackageName(),
R.layout.widget_layout);
// RemoteViews remoteViews1 = new RemoteViews(context.getPackageName(),
// R.layout.widget_layout);
//Log.w("WidgetExample", String.valueOf(number));
// Set the text
//remoteViews.setTextViewText(R.id.update, String.valueOf(number));
//remoteViews1.setTextViewText(R.id.update1, "Second TextView :"+String.valueOf(number));
// Register an onClickListener
Intent intent = new Intent(context, MyWidgetProvider.class);
// intent.setAction(AppWidgetManager.ACTION_APPWIDGET_UPDATE);
intent.setAction(ACTION_WIDGET_CLICK);
intent.putExtra("IDS", appWidgetIds);
Intent intent1 = new Intent(context, MyWidgetProvider.class);
//intent1.setAction(AppWidgetManager.ACTION_APPWIDGET_UPDATE);
intent1.setAction(ClickText2);
intent1.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, appWidgetIds);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context,
0, intent,0);
PendingIntent pendingIntent1 = PendingIntent.getBroadcast(context,
0, intent1, 0);
remoteViews.setOnClickPendingIntent(R.id.update, pendingIntent);
remoteViews.setOnClickPendingIntent(R.id.update1, pendingIntent1);
appWidgetManager.updateAppWidget(appWidgetIds, remoteViews);
// }
}
@Override
public void onReceive(Context context,Intent intent) {
RemoteViews updateviews = new RemoteViews(context.getPackageName(),
R.layout.widget_layout);
if (intent.getAction().equals(ACTION_WIDGET_CLICK)) {
updateviews.setTextViewText(R.id.update, "Clicked!!!");
intent.putExtra("CLICKED?", 1);
} else {
super.onReceive(context, intent);
}
// } else if (intent.getAction()==ClickText1 && intent.getExtras().getInt("CLICKED?")==1) {
// updateviews.setTextViewText(R.id.update, "No Way Clicked!!! Again!!");
// intent.putExtra("CLICKED?", 0);
// }
}
}
Android 清单如下:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="co.uk.timcs.myappwidget"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="8" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="co.uk.timcs.myappwidget.MyAppWidget"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name="MyWidgetProvider" >
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
<action android:name="co.uk.timcs.myappwidget.ACTION_WIDGET_CLICK" />
</intent-filter>
<meta-data android:name="android.appwidget.provider"
android:resource="@xml/widget_info" />
</receiver>
</application>
</manifest>
但是,当我单击 TextView 时,什么也没有发生,也没有任何变化。
编辑21:59 在 onReceive 中进行了两项更改,在清单中进行了一项更改,但两者都没有产生任何差异。