我创建了一个android小部件,当我添加一个configure活动时,小部件启动了一种活动并关闭它,但小部件没有显示,它的配置类中的代码显然有问题:
package com.rb.widget;
import android.app.Activity;
import android.os.Bundle;
public class WidgetConfig extends Activity{
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
}
}
配置类应该如何?
答案 0 :(得分:0)
这是因为Configure Activity必须返回一个值。
答案 1 :(得分:0)
在onCreate
函数中,您应该调用
/**
* In onCreate() we have to ensure that if the user presses BACK or cancelled the activity,
* then we should not add app widget.
*/
setResult(RESULT_CANCELED);
确保如果关闭配置活动,窗口小部件将不会添加到主页。以下代码显示了如何配置小部件。
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
/**
* In onCreate() we have to ensure that if the user presses BACK or cancelled the activity,
* then we should not add app widget.
*/
setResult(RESULT_CANCELED);
setContentView(R.layout.activity_widget_settings);
Intent intent = getIntent();
Bundle extras = intent.getExtras();
if (extras != null){
appWidgetId = extras.getInt(AppWidgetManager.EXTRA_APPWIDGET_ID,
AppWidgetManager.INVALID_APPWIDGET_ID);
}
if (appWidgetId == AppWidgetManager.INVALID_APPWIDGET_ID){
finish();
return;
}
appWidgetManager = AppWidgetManager.getInstance(this);
views = new RemoteViews(this.getPackageName(), R.layout.my_app_widget);
}
配置完成后,请不要忘记致电:
Intent widgetIntent = new Intent();
widgetIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
setResult(RESULT_OK,widgetIntent);
appWidgetManager.updateAppWidget(appWidgetId, views);