Search code examples
androidandroid-appwidgetappwidgetprovider

How to get the AppWidget id that I just clicked?


I need to get the id of the AppWidget when I click on it because I need the value to correctly perform certain action have some way to do it in a scalable way? but getting the id of the appwidget that I clicked is already a big help

I use a code to fetch all ids

But I can not get what I need that's what I clicked on

public class AppWidget extends AppWidgetProvider {

    private static final String MontWidget = "WidgetPreferences";
    private static String ID_CONFIG_WIDGET = "teste_config";

    static void updateAppWidget(Context context, AppWidgetManager appWidgetManager,
                                int appWidgetId) {

        RemoteViews views;

        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 = Widget_Config.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.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);

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

            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 = Widget_Config.loadTitlePref(context, appWidgetId);

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

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

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

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

            appWidgetManager.updateAppWidget(appWidgetId, views);
        }
    }

    @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];

            Intent intent = new Intent(context, Alert_x1_Activity.class);

            intent.putExtra("KEY_ID",appWidgetId);

            PendingIntent pendingIntent = PendingIntent.getActivity(context, appWidgetId, intent, 0);
            // Get the layout for the App Widget and attach an on-click listener
            // to the button
            views = new RemoteViews(context.getPackageName(), R.layout.x1_widget);
            views.setOnClickPendingIntent(R.id.btn_x1, pendingIntent);

            appWidgetManager.updateAppWidget(appWidgetId, views);
        }

    }

Solution

  • Let's take a look at the example from documentation:

    public class ExampleAppWidgetProvider extends AppWidgetProvider {
    
    public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
        final int N = appWidgetIds.length;
    
        // Perform this loop procedure for each App Widget that belongs to this provider
        for (int i=0; i<N; i++) {
                int appWidgetId = appWidgetIds[i];
    
                // Create an Intent to launch ExampleActivity
                Intent intent = new Intent(context, ExampleActivity.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.appwidget_provider_layout);
                views.setOnClickPendingIntent(R.id.button, pendingIntent);
    
                // Tell the AppWidgetManager to perform an update on the current app widget
                appWidgetManager.updateAppWidget(appWidgetId, views);
            }
        }
    }
    

    Inside the for loop, you know the appWidgetId for the AppWidget you are currently updating. So you can take this appWidgetId and pass it as an Intent extra:

    // ...
    // Create an Intent to launch ExampleActivity
    Intent intent = new Intent(context, ExampleActivity.class);
    // Put the current appWidgetId as Intent extra:
    intent.putExtra("KEY_ID", appWidgetId);
    // Use the appWidgetId (or any unique int value) as request code for the PendingIntent. 
    // Note: two PendingIntents with the same "content intent" 
    // and the same request code *are the same PendingIntent* !
    PendingIntent pendingIntent = PendingIntent.getActivity(context, appWidgetId, intent, 0);
    //...
    

    In ExampleActivity, you get the appWidgetId like this:

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

    (Or if you set the launch mode to to "singleTop" or if you used the Intent.FLAG_ACTIVITY_SINGLE_TOP flag, then the Intent with the id will be delivered to ExampleActivity in onNewIntent(Intent) )

    Please note that the appWidgetId's don't start with 0 (I tested with two App Widgets, and they had numbers 5 and 6) But this code snippet is only meant to show you that you can pass information with the PendingIntent which will help you to identify your App Widget.(Not only int values, please see Intent.putExtra())