你需要在onRecieve方法的末尾添加几行….它应该像这样..
package com.android.FirstWidget;import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageButton;
import android.widget.RemoteViews;
public class ExampleAppWidgetProvider extends AppWidgetProvider {
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
Log.e("ds",intent.getAction());
Log.e("f","f");
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.wid);
if(intent.getAction().contains("arrow_left")){
views.setTextViewText(R.id.textView1,"left");
Log.e("fssss","sssf");
}
else if(intent.getAction().contains("arrow_right")){
views.setTextViewText(R.id.textView1,"right");
Log.e("fssss","sssf");
}
else {
super.onReceive(context, intent);
}
ComponentName componentName = new ComponentName(context, ExampleAppWidgetProvider.class);
AppWidgetManager.getInstance(context).updateAppWidget(componentName, views);
}
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
final int N = appWidgetIds.length;
// Perform this loop procedure for each App Widget that belongs to this provider
for (int i=0; i
int appWidgetId = appWidgetIds[i];
// Create an Intent to launch ExampleActivity
Intent intent = new Intent(context, ExampleAppWidgetProvider.class);
intent.setAction("arrow_left");
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intent, 0);
// Get the layout for the App Widget and attach an on-click listener
// to the button
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.wid);
views.setOnClickPendingIntent(R.id.arrowLeft, pendingIntent);
intent = new Intent(context, ExampleAppWidgetProvider.class);
intent.setAction("arrow_right");
pendingIntent = PendingIntent.getBroadcast(context, 0, intent,
views.setOnClickPendingIntent(R.id.arrowRight, pendingIntent);
//views.set
// Tell the AppWidgetManager to perform an update on the current app widget
appWidgetManager.updateAppWidget(appWidgetId, views);
}
}
}
此外,虽然我相信你编码的内容应该可以工作,但如果没有,你可以尝试在清单文件中设置按钮的intent操作.它应该是这样的
我想现在应该可以了.我自己偶然发现了这个问题,并在此找到了解决方案:Clickable widgets in android