Android home screen widget no gets appWidgetId in Configuration Activity
I have a widget and I would like to add more than one of them to my screen, so I must control wich widget I'm configuring when I click on it in home screen with appWidgetId.
I'm following the documention in android.com, but I'm not getting the appWidgetId when I click on it in home screen.
Pieces of my code:
//MyWidgetClass
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
int[] appWidgetIds) {
Log.i(TAG, CLASS_NAME + " onUpdate");
for (int appWidgetId : appWidgetIds) {
updateAppWidget(context, appWidgetManager, appWidgetId);
}
}
public static void updateAppWidget(Context context,
AppWidgetManager appWidgetManager, int appWidgetId) {
Log.d(TAG, CLASS_NAME + " updateAppWidget --- " + appWidgetId);
if (appWidgetId != AppWidgetManager.INVALID_APPWIDGET_ID) {
Intent settingsIntent = new Intent(context,
Widget1x1SettingsActivity.class);
settingsIntent
.setAction(android.appwidget.AppWidgetManager.ACTION_APPWIDGET_CONFIGURE);
//settingsIntent.putExtra("WIDGET_ID", appWidgetId);
PendingIntent settingsPendingIntent = PendingIntent.getActivity(
context, 0, settingsIntent, 0);
RemoteViews remoteViews = null;
int layout = R.layout.widget_1x1;
remoteViews = new RemoteViews(context.getPackageName(), layout);
// Do something ...
remoteViews.setOnClickPendingIntent(R.id.widget,
settingsPendingIntent);
ComponentName thisWidget = new ComponentName(context,
MyWidgetClass.class);
appWidgetManager.updateAppWidget(thisWidget, remoteViews);
}
}
//Settings class
@Override
protected void onCreate(Bundle savedInstanceState) {
Log.d(TAG, CLASS_NAME + " :: onCreate >>");
super.onCreate(savedInstanceState);
mContext = getApplicationContext();
Intent intent = getIntent();
Bundle extras = intent.getExtras();
if (extras != null) {
appWidgetId = extras.getInt(AppWidgetManager.EXTRA_APPWIDGET_ID,
AppWidgetManager.INVALID_APPWIDGET_ID);
//**//Here appWidgetId is 0 when I click on home screen widget**
}
....
}
And when I click on ok button to finish the configuration activity:
AppWidgetManager appWidgetManager = AppWidgetManager
.getInstance(getApplicationContext());
MyWidgetClass.updateAppWidget(getApplicationContext(),
appWidgetManager, appWidgetId);
Intent resultValue = new Intent();
resultValue.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID,
appWidgetId);
Log.d(TA开发者_如何学JAVAG, CLASS_NAME + " Putting in RESULT resultValue extras: " + resultValue.getExtras() + " getInt(): " + resultValue.getExtras().getInt(AppWidgetManager.EXTRA_APPWIDGET_ID)); // it prints correct
setResult(RESULT_OK, resultValue);
finish();
I don't paste any xml file because I think they are ok.
Thanks in advance, SergiBC
精彩评论