天哪,这里肯定有一千个关于 android appwidgets 的不同教程,甚至还有更多问题,但我就是不知道为什么我的不工作。叹气 反问:为什么这里的代码不能与 setOnClickListener (new new Button.OnClickListener() { // do stuff }... 无论如何,我的小部件显示在屏幕上和标签是正确的,但是当我点击小部件时,什么也没有发生。我在所有我认为会发生某些事情的地方都设置了断点,但没有执行任何操作。
问题1:点击一个小部件后执行什么代码?
我的小部件在被点击时并没有真正更新。相反,它只是在我的程序的其余部分执行一些代码。它只是制作一些网络 http 和/或套接字服务器命令。此外,我的小部件在放置在桌面上之前配置了一个活动。
这是清单:
<receiver android:name="PhcaAppWidgetProvider" >
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
<action android:name="com.skipmorrow.phca.PhcaAppWidgetProvider.WIDGET_CLICKED" />
</intent-filter>
<meta-data android:name="android.appwidget.provider"
android:resource="@xml/phca_widget_info" />
</receiver>
这是小部件配置器活动
private Activity act;
private static ListView listView;
private static ArrayAdapter<String> adapter;
private ArrayList<String> actionList;
private final String widgetPageName = "_widget";
int mAppWidgetId = AppWidgetManager.INVALID_APPWIDGET_ID;
private static final String PREFS_NAME = "PHCA";
private static final String PREF_PREFIX_KEY = "prefix_";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setTitle("Choose an action for this widget");
actionList = GetActionList();
if (!actionList.isEmpty()) {
listView = getListView();
adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, android.R.id.text1, actionList);
setListAdapter(adapter);
}
else {
// no objects on the widget page
}
// Find the widget id from the intent.
Intent intent = getIntent();
Bundle extras = intent.getExtras();
if (extras != null) {
mAppWidgetId = extras.getInt(
AppWidgetManager.EXTRA_APPWIDGET_ID, AppWidgetManager.INVALID_APPWIDGET_ID);
}
// If they gave us an intent without the widget id, just bail.
if (mAppWidgetId == AppWidgetManager.INVALID_APPWIDGET_ID) {
finish();
}
}
private ArrayList<String> GetActionList() {
ArrayList<String> l = new ArrayList<String>();
Page p = CommonActivity.GetPageNamed(getApplicationContext(), widgetPageName);
if (p!=null) {
if (p.pageObjects.size()==0) DisplayEmptyPageHelpDialog();
for (int i = 0; i < p.pageObjects.size(); i++) {
l.add(p.pageObjects.get(i).GetParsedMajorLabel(getApplicationContext()).toString());
}
}
else {
CreateWidgetPage();
DisplayEmptyPageHelpDialog();
}
return l;
}
private void CreateWidgetPage() {
Page widgetPage = new Page(getApplicationContext());
widgetPage.setPageName(widgetPageName);
widgetPage.SetPageType("list");
widgetPage.setNote("Widget Page");
widgetPage.setPageTitle("Widget Page");
widgetPage.setImageFilename("");
widgetPage.setTransparentImageOverlayFilename("");
widgetPage.InsertInstanceIntoDatabase(getApplicationContext());
}
private void DisplayEmptyPageHelpDialog() {
Dialog helpDialog = new Dialog(this);
helpDialog.setContentView(R.layout.phca_help_dialog);
helpDialog.setTitle("PHCA Widget");
TextView helpText = (TextView) helpDialog.findViewById(R.id.tvHelpText);
helpText.setText("Your _widget page is empty. Please add an action to the _widget page so it can be used in a widget.");
TextView subTitle = (TextView) helpDialog.findViewById(R.id.tvSubject);
subTitle.setText("PHCA Widget configurator");
helpDialog.show();
}
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
SharedPreferences.Editor prefs = getSharedPreferences(PREFS_NAME, 0).edit();
prefs.putInt(PREF_PREFIX_KEY + mAppWidgetId, position);
prefs.commit();
// Push widget update to surface with newly set prefix
String majorLabel = CommonActivity.GetPageObjectAtIndex(getApplicationContext(), widgetPageName, position).GetParsedMajorLabel(getApplicationContext()).toString();
String minorLabel = CommonActivity.GetPageObjectAtIndex(getApplicationContext(), widgetPageName, position).GetParsedMinorLabel(getApplicationContext()).toString();
AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(getApplicationContext());
PhcaAppWidgetProvider.updateAppWidget(getApplicationContext(), appWidgetManager,
mAppWidgetId, majorLabel, minorLabel);
Intent resultValue = new Intent();
resultValue.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, mAppWidgetId);
setResult(RESULT_OK, resultValue);
finish();
}
这是我的 appwidget 提供者
public class PhcaAppWidgetProvider extends AppWidgetProvider {
private static final String ACTION_CLICK = "WIDGET_CLICKED";
private final String widgetPageName = "_widget";
private static final String PREFS_NAME = "PHCA";
private static final String PREF_PREFIX_KEY = "prefix_";
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
int[] appWidgetIds) {
// Get all ids
ComponentName thisWidget = new ComponentName(context,
PhcaAppWidgetProvider.class);
//int[] allWidgetIds = appWidgetManager.getAppWidgetIds(thisWidget);
final int N = appWidgetIds.length;
for (int i=0; i<N; i++) {
int appWidgetId = appWidgetIds[i];
SharedPreferences myPrefs = context.getSharedPreferences(PREFS_NAME, context.MODE_WORLD_WRITEABLE);
Integer objNum = myPrefs.getInt(PREF_PREFIX_KEY + appWidgetId, -1);
if (objNum > -1) {
PageAction pa = (PageAction) CommonActivity.GetPageObjectAtIndex(context, widgetPageName, objNum);
String majorLabel = pa.GetUnparsedMajorLabel(context).toString();
String minorLabel = pa.GetUnparsedMinorLabel(context).toString();
updateAppWidget(context, appWidgetManager, appWidgetId, majorLabel, minorLabel);
}
}
}
@Override
public void onEnabled(Context context) {
Log.d("Widget", "onEnabled");
}
@Override
public void onReceive(Context context, Intent intent) {
String intentAction = intent.getAction();
updateWidgetState(context, intentAction);
if (intentAction.equals(ACTION_CLICK)) {
Bundle extras = intent.getExtras();
Integer appWidgetId = extras.getInt("appwidgetid");
SharedPreferences myPrefs = context.getSharedPreferences(PREFS_NAME, context.MODE_WORLD_WRITEABLE);
Integer objNum = myPrefs.getInt(PREF_PREFIX_KEY + appWidgetId, -1);
if (objNum > -1) {
PageAction pa = (PageAction) CommonActivity.GetPageObjectAtIndex(context, widgetPageName, objNum);
pa.ExecuteActionFromWidgetClick(context);
}
} else {
super.onReceive(context, intent);
}
}
public static void updateWidgetState(Context paramContext, String paramString)
{
RemoteViews localRemoteViews = buildUpdate(paramContext, paramString);
ComponentName localComponentName = new ComponentName(paramContext, PhcaAppWidgetProvider.class);
AppWidgetManager.getInstance(paramContext).updateAppWidget(localComponentName, localRemoteViews);
}
private static RemoteViews buildUpdate(Context ctx, String paramString)
{
RemoteViews views = new RemoteViews(ctx.getPackageName(), R.layout.phca_appwidget);
views.setTextViewText(R.id.majorlabel, "majorLabel");
views.setTextViewText(R.id.minorlabel, "minorLabel");
Intent intent = new Intent(ctx, PhcaAppWidgetProvider.class);
intent.setAction(ACTION_CLICK);
intent.putExtra("appwidgetid", 0);
PendingIntent pendingIntent = PendingIntent.getBroadcast(ctx, 0, intent , 0);
views.setOnClickPendingIntent(R.layout.phca_appwidget, pendingIntent);
if(paramString.equals(ACTION_CLICK))
{
Toast.makeText(ctx, "ACTION_CLICK", Toast.LENGTH_LONG).show();
}
return views;
}
}
当我添加小部件并将其删除时,不同的意图被传递到 onReceive 中,因此可以正常工作,但单击时什么也没有。
问题2:有人可以指出我做错了什么吗?我非常关注这里的教程:http: //developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/appwidget/ExampleAppWidgetProvider.html
问题 3:似乎我可以在小部件的布局 xml 中放置一个 android:onclick="WidgetClicked",但我不知道在哪里放置 WidgetClicked 方法。WidgetProvider 似乎合乎逻辑,但这对我也不起作用。有没有办法在xml中做到这一点?
免责声明:上面的代码是经过一天半的故障排除后的当前状态。它只是许多不同尝试的一次迭代。