我想创建一个带有配置活动的app小部件,问题是当我将小部件添加到home时显示配置活动但是当我点击按钮配置时主要活动没有启动....当我点击在Widget上没有任何反应。我的主要活动中没有错误。因为如果我启动应用程序,它的工作没有任何错误... 我不知道我做错了什么:(。非常感谢你的帮助......
这是代码:
public class WidgetConfiguration extends Activity{
final Context context = this;
EditText url;
EditText password;
int mAppWidgetId;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setResult(RESULT_CANCELED);
initializeAppWidget();
showConfigDlg();
final Context context = WidgetConfiguration.this;
AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
RemoteViews views = new RemoteViews(context.getPackageName(),
R.layout.widget_layout);
}
private void initializeAppWidget(){
Intent intent = getIntent();
Bundle extras = intent.getExtras();
if (extras != null) {
mAppWidgetId = extras.getInt(
AppWidgetManager.EXTRA_APPWIDGET_ID,
AppWidgetManager.INVALID_APPWIDGET_ID);
}
if (mAppWidgetId == AppWidgetManager.INVALID_APPWIDGET_ID) {
finish();
}
}
private void showConfigDlg(){
setContentView(R.layout.widget_config);
((Button) findViewById(R.id.saveBtn)).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
Log.d("blablabla", "setting the onClick1");
AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
RemoteViews views = new RemoteViews(context.getPackageName(),
R.layout.widget_layout);
appWidgetManager.updateAppWidget(mAppWidgetId, views);
Intent resultValue = new Intent();
resultValue.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, mAppWidgetId);
setResult(RESULT_OK, resultValue);
finish();
}
});
}
}
public class WidgetProvider extends AppWidgetProvider
{
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
int[] appWidgetIds) {
final int N = appWidgetIds.length;
Log.d("blablabla", "setting the onClick2");
// Perform this loop procedure for each App Widget that belongs to this provider
for (int i=0; i<N; i++) {
int appWidgetId = appWidgetIds[i];
Log.d("blablabla", "setting the onClick3");
// Create an Intent to launch ExampleActivity
Intent intent = new Intent(context, MainActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(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.widget_layout);
views.setOnClickPendingIntent(R.id.button, pendingIntent);
// Tell the AppWidgetManager to perform an update on the current app widget
appWidgetManager.updateAppWidget(appWidgetId, views);
}
}
}