在Widget Android上的按钮
我一直在试图设置我的主屏幕小部件上的按钮来重新打开ConfigActivity。 但是我无法做到。
我试过四处寻找stackoverflow,android开发指南以及其他非官方教程,并尝试所有建议的方式,但迄今没有运气。
这里是AppWidgetProvider()中的onUpdate()
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget);
Intent intent = new Intent(context, ConfigActivity.class);
intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetIds[i]);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
views.setOnClickPendingIntent(R.id.widget_refresh, pendingIntent);
我试过把Extra,Data,Flags,没有运气。
配置活动只是将小部件放在主屏幕上时首次启动的活动。 R.layout.widget是小部件的布局R.id.widget_refresh是按钮ID
还有什么我必须做的吗? 我错过了什么?
我的ConfigActivity.java
public class ConfigActivity extends Activity implements android.view.View.OnClickListener {
private int appWidgetId = AppWidgetManager.INVALID_APPWIDGET_ID;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.configactivity);
Intent intent = new Intent();
intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
setResult(Activity.RESULT_CANCELED, intent);
assignAppWidgetId();
findViewById(R.id.widgetactivity_button).setOnClickListener(this);
}
/**
* Widget configuration activity,always receives appwidget Id appWidget Id =
* unique id that identifies your widget analogy : same as setting view id
* via @+id/viewname on layout but appwidget id is assigned by the system
* itself
*/
private void assignAppWidgetId() {
Bundle extras = getIntent().getExtras();
if (extras != null)
appWidgetId = extras.getInt(AppWidgetManager.EXTRA_APPWIDGET_ID,
AppWidgetManager.INVALID_APPWIDGET_ID);
}
public void onClick(View v) {
if (v.getId() == R.id.widgetactivity_button)
startWidget();
}
/**
* This method right now displays the widget and starts a Service to fetch
* remote data from Server
*/
private void startWidget() {
// this intent is essential to show the widget
// if this intent is not included,you can't show
// widget on homescreen
Intent intent = new Intent();
intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
setResult(Activity.RESULT_OK, intent);
// start your service
// to fetch data from web
Intent serviceIntent = new Intent(this, RemoteFetchService.class);
serviceIntent
.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
startService(serviceIntent);
// finish this activity
this.finish();
}
}
编辑:我已经解决了问题,通过onReceive()而不是onUpdate()的意图,我可以让小部件再次接收像普通的点击。
链接地址: http://www.djcxy.com/p/50659.html下一篇: Android widget, how to show the next value from the database on a touch event?