如何获取我刚刚点击的AppWidget id?
How to get the AppWidget id that I just clicked?
我需要在单击它时获取 AppWidget
的 ID,因为我需要该值才能正确执行某些操作有某种方法可以以可扩展的方式执行此操作吗?但是获取我点击的appwidget的id已经是一个很大的帮助了
我使用代码获取所有 ID
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);
}
}
让我们看一下来自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
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);
//...
在 ExampleActivity
中,您可以这样获得 appWidgetId:
int widgetId = getIntent().getIntExtra("KEY_ID", -1);
(或者,如果您将启动模式设置为 "singleTop",或者如果您使用了 Intent.FLAG_ACTIVITY_SINGLE_TOP
标志,则带有 id 的 Intent
将被传送到 ExampleActivity
在 onNewIntent(Intent)
)
请注意,appWidgetId 不是以 0 开头(我测试了两个 App Widget,它们的编号分别为 5 和 6)但是此代码片段只是为了向您展示您可以使用 PendingIntent
这将帮助您识别您的 App Widget。(不仅 int
值,请参阅 Intent.putExtra())
我需要在单击它时获取 AppWidget
的 ID,因为我需要该值才能正确执行某些操作有某种方法可以以可扩展的方式执行此操作吗?但是获取我点击的appwidget的id已经是一个很大的帮助了
我使用代码获取所有 ID
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);
}
}
让我们看一下来自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
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);
//...
在 ExampleActivity
中,您可以这样获得 appWidgetId:
int widgetId = getIntent().getIntExtra("KEY_ID", -1);
(或者,如果您将启动模式设置为 "singleTop",或者如果您使用了 Intent.FLAG_ACTIVITY_SINGLE_TOP
标志,则带有 id 的 Intent
将被传送到 ExampleActivity
在 onNewIntent(Intent)
)
请注意,appWidgetId 不是以 0 开头(我测试了两个 App Widget,它们的编号分别为 5 和 6)但是此代码片段只是为了向您展示您可以使用 PendingIntent
这将帮助您识别您的 App Widget。(不仅 int
值,请参阅 Intent.putExtra())