如何获取我刚刚点击的AppWidget ID?

时间:2017-10-20 17:55:00

标签: android android-appwidget appwidgetprovider

当我点击它时,我需要获取AppWidget的id,因为我需要正确执行某些操作的值,以某种方式以可扩展的方式执行它?但是获取我点击的appwidget的id已经是一个很大的帮助

我使用代码来获取所有ID

  

但我无法得到我所需要的东西,而是我点击了

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

    }

1 个答案:

答案 0 :(得分:0)

让我们来看看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);
        }
    }
}

在for循环中,您知道当前正在更新的AppWidget的 appWidgetId 。因此,您可以使用此 appWidgetId 并将其作为Intent额外内容传递:

// ...
// 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);
//...

ExampleActivity中,您可以像这样获得 appWidgetId

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

(或者,如果您将启动模式设置为&#34; singleTop&#34;或者如果您使用了Intent.FLAG_ACTIVITY_SINGLE_TOP标记,那么带有ID的Intent将被传递到{{1在ExampleActivity

请注意,appWidgetId不是以0开头(我使用两个应用小工具进行了测试,他们的数字为5和6)但是此代码段只是为了告诉您可以通过onNewIntent(Intent)的信息将帮助您识别您的应用小工具。(不仅PendingIntent值,请参阅Intent.putExtra()