我试图避免在这里发布一个问题,因为我通常会为自己解决大部分事情,但我认为我已经击中了其中一个编程砖墙。很抱歉询问之前是否曾经问过这个问题(我已经用谷歌搜索了所有用Google搜索过)并且我也在这里搜索了一下,但我发现以下问题的例子很少。
是否有可能创建一个小部件,其中包含两个或更多按钮,例如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 Manifest如下:
<?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中进行了两次更改,在清单中进行了一次更改,但两者都没有任何差异。