How to get the Appwidget id when clicking it?

Asked

Viewed 27 times

1

I need the ID of an Appwidget in specific to do certain action, example:

widget 1- sair
widget 2- entrar
widget 3- nao faz nada

Suppose the login was selected I will use the WIDGET LOGIN ID to do the action

So far I’ve got the code that catches everyone but doesn’t fit me:

Context context = getApplicationContext();
ComponentName name = new ComponentName(context, AppWidget.class);
int [] ids = AppWidgetManager.getInstance(context).getAppWidgetIds(name);
  • John, it is interesting that you put the code you are using, point out where the fault is, otherwise the staff will negatively your question even

1 answer

2


I got a way this way

 @Override
    public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
        System.out.println("onUpdate");
        final int N = appWidgetIds.length;

        RemoteViews views;
        for (int i=0; i<N; i++) {
            int appWidgetId = appWidgetIds[i];

            SharedPreferences dados = context.getSharedPreferences(MontWidget, 0);
            Integer posicao = dados.getInt(ID_CONFIG_WIDGET+appWidgetId, 0);

            Log.i("Posicao de selecao", String.valueOf(posicao));

            if (posicao == 0) {

                CharSequence widgetText = WidgetConfigActivity.loadTitlePref(context, appWidgetId);

                views = new RemoteViews(context.getPackageName(), R.layout.x1);

                Intent confirmar_acao = new Intent(context, Alert_x1_Activity.class);
                confirmar_acao.putExtra(Alert_x1_Activity.ARG_DIALOG_NUMBER, 1);
                confirmar_acao.putExtra("KEY_ID",appWidgetId);

                confirmar_acao.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
                PendingIntent confirmar_acao_u = PendingIntent.getActivity(context,
                        appWidgetId, confirmar_acao, PendingIntent.FLAG_UPDATE_CURRENT | 0);

                views.setOnClickPendingIntent(R.id.btn_x1, confirmar_acao_u);
                views.setOnClickPendingIntent(R.id.txt_x1, confirmar_acao_u);
                views.setTextViewText(R.id.txt_x1,widgetText);

                appWidgetManager.updateAppWidget(appWidgetId, views);

            }
            if(posicao == 1) {

                CharSequence widgetText = WidgetConfigActivity.loadTitlePref(context, appWidgetId);

                views = new RemoteViews(context.getPackageName(), R.layout.x2_widget);

                Intent confirmar_acao = new Intent(context, Alert_x2_Activity.class);
                confirmar_acao.putExtra(Alert_x2_Activity.ARG_DIALOG_NUMBER, 1);
                confirmar_acao.putExtra("KEY_ID",appWidgetId);
                confirmar_acao.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);

                PendingIntent confirmar_acao_u = PendingIntent.getActivity(context,
                        appWidgetId, confirmar_acao, PendingIntent.FLAG_UPDATE_CURRENT | 0);

                views.setOnClickPendingIntent(R.id.btn_x2, confirmar_acao_u);
                views.setOnClickPendingIntent(R.id.txt_painel_virtual, confirmar_acao_u);
                views.setTextViewText(R.id.txt_x2,widgetText);

                appWidgetManager.updateAppWidget(appWidgetId, views);
            }
        }

    }

It can be done that way

Note: Alert_x1_activity opens a confirmation Alertdialog

In my example I should call the Appwidget id in the Alertx1 or Alertx classes

 int widgetId = getIntent().getIntExtra("KEY_ID", -1);

Browser other questions tagged

You are not signed in. Login or sign up in order to post.