FileDocCategorySizeDatePackage
AppWidgetHost.javaAPI DocAndroid 1.5 API7944Wed May 06 22:41:54 BST 2009android.appwidget

AppWidgetHost

public class AppWidgetHost extends Object
AppWidgetHost provides the interaction with the AppWidget service for apps, like the home screen, that want to embed AppWidgets in their UI.

Fields Summary
static final int
HANDLE_UPDATE
static final int
HANDLE_PROVIDER_CHANGED
static Object
sServiceLock
static com.android.internal.appwidget.IAppWidgetService
sService
android.content.Context
mContext
String
mPackageName
android.os.Handler
mHandler
int
mHostId
Callbacks
mCallbacks
HashMap
mViews
Constructors Summary
public AppWidgetHost(android.content.Context context, int hostId)


         
        mContext = context;
        mHostId = hostId;
        mHandler = new UpdateHandler(context.getMainLooper());
        synchronized (sServiceLock) {
            if (sService == null) {
                IBinder b = ServiceManager.getService(Context.APPWIDGET_SERVICE);
                sService = IAppWidgetService.Stub.asInterface(b);
            }
        }
    
Methods Summary
public intallocateAppWidgetId()
Get a appWidgetId for a host in the calling process.

return
a appWidgetId

        try {
            if (mPackageName == null) {
                mPackageName = mContext.getPackageName();
            }
            return sService.allocateAppWidgetId(mPackageName, mHostId);
        }
        catch (RemoteException e) {
            throw new RuntimeException("system server dead?", e);
        }
    
public final AppWidgetHostViewcreateView(android.content.Context context, int appWidgetId, AppWidgetProviderInfo appWidget)

        AppWidgetHostView view = onCreateView(context, appWidgetId, appWidget);
        view.setAppWidget(appWidgetId, appWidget);
        synchronized (mViews) {
            mViews.put(appWidgetId, view);
        }
        RemoteViews views = null;
        try {
            views = sService.getAppWidgetViews(appWidgetId);
        } catch (RemoteException e) {
            throw new RuntimeException("system server dead?", e);
        }
        view.updateAppWidget(views);
        return view;
    
public static voiddeleteAllHosts()
Remove all records about all hosts for your package.
  • Call this when initializing your database, as it might be because of a data wipe.
  • Call this to have the AppWidget manager release all resources associated with your host. Any future calls about this host will cause the records to be re-allocated.

        try {
            sService.deleteAllHosts();
        }
        catch (RemoteException e) {
            throw new RuntimeException("system server dead?", e);
        }
    
public voiddeleteAppWidgetId(int appWidgetId)
Stop listening to changes for this AppWidget.

        synchronized (mViews) {
            mViews.remove(appWidgetId);
            try {
                sService.deleteAppWidgetId(appWidgetId);
            }
            catch (RemoteException e) {
                throw new RuntimeException("system server dead?", e);
            }
        }
    
public voiddeleteHost()
Remove all records about this host from the AppWidget manager.
  • Call this when initializing your database, as it might be because of a data wipe.
  • Call this to have the AppWidget manager release all resources associated with your host. Any future calls about this host will cause the records to be re-allocated.

        try {
            sService.deleteHost(mHostId);
        }
        catch (RemoteException e) {
            throw new RuntimeException("system server dead?", e);
        }
    
protected AppWidgetHostViewonCreateView(android.content.Context context, int appWidgetId, AppWidgetProviderInfo appWidget)
Called to create the AppWidgetHostView. Override to return a custom subclass if you need it. {@more}

        return new AppWidgetHostView(context);
    
protected voidonProviderChanged(int appWidgetId, AppWidgetProviderInfo appWidget)
Called when the AppWidget provider for a AppWidget has been upgraded to a new apk.

    
public voidstartListening()
Start receiving onAppWidgetChanged calls for your AppWidgets. Call this when your activity becomes visible, i.e. from onStart() in your Activity.

        int[] updatedIds = null;
        ArrayList<RemoteViews> updatedViews = new ArrayList();
        
        try {
            if (mPackageName == null) {
                mPackageName = mContext.getPackageName();
            }
            updatedIds = sService.startListening(mCallbacks, mPackageName, mHostId, updatedViews);
        }
        catch (RemoteException e) {
            throw new RuntimeException("system server dead?", e);
        }

        final int N = updatedIds.length;
        for (int i=0; i<N; i++) {
            updateAppWidgetView(updatedIds[i], updatedViews.get(i));
        }
    
public voidstopListening()
Stop receiving onAppWidgetChanged calls for your AppWidgets. Call this when your activity is no longer visible, i.e. from onStop() in your Activity.

        try {
            sService.stopListening(mHostId);
        }
        catch (RemoteException e) {
            throw new RuntimeException("system server dead?", e);
        }
    
voidupdateAppWidgetView(int appWidgetId, android.widget.RemoteViews views)

        AppWidgetHostView v;
        synchronized (mViews) {
            v = mViews.get(appWidgetId);
        }
        if (v != null) {
            v.updateAppWidget(views);
        }