How to get the AppWidget id by clicking it?

0

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

Assume that the enter was selected I will use the ID of the WIDGET ENTER to do the action

So far I have the code that catches all but it does not suit me:

Context context = getApplicationContext();
ComponentName name = new ComponentName(context, AppWidget.class);
int [] ids = AppWidgetManager.getInstance(context).getAppWidgetIds(name);
    
asked by anonymous 23.10.2017 / 18:55

1 answer

1

I got a shape as follows

 @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);
            }
        }

    }

Can be done this 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);
    
23.10.2017 / 23:44