2012-02-26 3 views
0

appWidgetProvider에서 특정 appWidgetId를 가져와야합니다. appWidgetIds []가 아니라 사용자가 클릭 한 특정 ID가 필요합니다.AppWidgetProvider에서 appWidgetId를 가져 오는 방법은 무엇입니까?

내가하지만의 AppWidgetProvider 너무 다른

(504)처럼 "intent.getExtra().의 getInt (AppWidgetManager.APP_WIDGET_ID가)"라고하여, 그 구성 활동에 그것을 얻을 수있다, 나는 그것을 감사 할 것이다 아무도 내게 손을주지 않으면.

public class WidgetBox extends AppWidgetProvider implements Widget { 

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

} 

답변

0

위젯 라이프 사이클 메소드가 호출 될 때 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 
    } 
} 

, 이것은 구성하는 동안 보류 의도 (또는 최신 업데이트)를 사용하여 수행됩니다 : 당신은 단순히 이런 제공된 ID를 반복 할 수

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); 
+0

무엇 이 appWidgetIDs입니까? 왜 시리즈인가? 왜 우리는 appWidgetIds의 모든 요소에 대해 동일한 프로세스를 수행합니까? – atasoyh

+1

사용자는 동일한 위젯을 여러 번 배포 할 수 있습니다 (예 : 다른 위치에 일몰과 같이 다른 구성으로 가능). API는 onUpdate()에 모든 위젯의 ID 배열을 제공합니다. – Stefan

관련 문제