如何在服务中引用小部件?

时间:2011-09-16 01:00:15

标签: android

如何在服务内部将TextView和ProgressBar设置为findViewById视图?>

 ProgressBar dialog;

TextView Titletext;


@Override
public IBinder onBind(Intent intent) {
    // TODO Auto-generated method stub
    return null;
}
@Override
public void onCreate(){
    super.onCreate();
    Log.e("StackWidgetService", "Inside of onCreate in Widget Service");

    getter fethcer = new getter();
    fethcer.execute();

}

1 个答案:

答案 0 :(得分:2)

只需获取AppWidgetManager的引用并更新布局。

AppWidgetManager awm = AppWidgetManager.getInstance(context);
int[] appWidgetIds = awm.getAppWidgetIds(yourProviderComponentName);
RemoteViews rv = new RemoteViews(context.getPackageName(), R.layout.thelayout);
// Construct the remote views here ...

awm.updateAppWidget(appWidgetIds[0], rv);

此示例仅更新第一个窗口小部件实例(appWidgetIds[0])的布局,如果没有窗口小部件,甚至可能崩溃。通常你必须遍历appWidgetIds - 数组并单独更新每个数组;如果没有小部件实例,则跳过循环。