简体   繁体   English

如何在AppWidgetProvider中获取appWidgetId?

[英]How to get appWidgetId in AppWidgetProvider?

I need to get the specific appWidgetId in AppWidgetProvider, not a bundle of appWidgetIds[] but the specific id which user clicked. 我需要获取AppWidgetProvider中的特定appWidgetId,而不是appWidgetIds []的捆绑包,而是用户单击的特定ID。

I could get it in its configure activity, using the "intent.getExtra().getInt(AppWidgetManager.APP_WIDGET_ID)" says like 504. 我可以使用“ intent.getExtra()。getInt(AppWidgetManager.APP_WIDGET_ID)”在其配置活动中获得它,就像504。

But it is so different in AppWidgetProvider, I will be appreciate it if anyone give me a hand.thanks in advance. 但这在AppWidgetProvider中是如此不同,如果有人事先帮我,我将不胜感激。

public class WidgetBox extends AppWidgetProvider implements Widget {

private int _widgetId = ?; // not appWidgetIds[]

}

You will get an array of Ids when the widget life cycle methods are called. 调用小部件生命周期方法时,您将获得一个ID数组。 You can simply iterate through the provided Ids like this: 您可以像这样简单地遍历提供的ID:

@Override
public void onUpdate(final Context context, final AppWidgetManager appWidgetManager, final int[] appWidgetIds) {
    final int n = appWidgetIds.length;
    for( int i = 0; i < n; i++ ) {
        final int appWidgetId = appWidgetIds[i];
        // some logic here
    }
}

if you need to invoke some action, this is done using a pending intent during configuration (or a later update): 如果您需要调用某些操作,可以在配置(或以后的更新)过程中使用挂起的意图来完成:

final RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget);
final Intent defineIntent = new Intent(context, YourTargetActivity.class);
defineIntent.addCategory(Intent.CATEGORY_LAUNCHER);
final PendingIntent pendingIntent = PendingIntent.getActivity(context, 0 /* no request code */, defineIntent, 0);
views.setOnClickPendingIntent(R.id.view_id, pendingIntent);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM