【Android源码 栏目提醒】:网学会员Android源码 为您提供WeatherWidgetService java android源码 - 开发文档参考,解决您在WeatherWidgetService java android源码 - 开发文档学习中工作中的难题,参考学习。
/ Copyright C 2011 The Android Open Source Project Licensed under the Apache License Version 2.0 the License you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing software distributed under the License is distributed on an AS IS BASIS WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND either express or implied. See the License for the specific language governing permissions and limitations under the License. / package com.example.android.weatherlistwidget import java.util.ArrayList import java.util.List import android.appwidget.AppWidgetManager import android.content.Context import android.content.Intent import android.content.ContentUris import android.database.Cursor import android.net.Uri import android.os.Bundle import android.widget.RemoteViews import android.widget.RemoteViewsService / This is the service that provides the factory to be bound to the collection service. / public class WeatherWidgetService extends RemoteViewsService Override public RemoteViewsFactory onGetViewFactoryIntent intent return new StackRemoteViewsFactorythis.getApplicationContext intent / This is the factory that will provide data to the collection widget. / class StackRemoteViewsFactory implements RemoteViewsService.RemoteViewsFactory private Context mContext private Cursor mCursor private int mAppWidgetId public StackRemoteViewsFactoryContext context Intent intent mContext context mAppWidgetId intent.getIntExtraAppWidgetManager.EXTRA_APPWIDGET_ID AppWidgetManager.INVALID_APPWIDGET_ID public void onCreate // Since we reload the cursor in onDataSetChanged which gets called immediately after // onCreate we do nothing here. public void onDestroy if mCursor null mCursor.close public int getCount return mCursor.getCount public RemoteViews getViewAtint position // Get the data for this position from the content provider String city Unknown City int temp 0 if mCursor.moveToPositionposition final int cityColIndex mCursor.getColumnIndexWeatherDataProvider.Columns.CITY final int tempColIndex mCursor.getColumnIndex WeatherDataProvider.Columns.TEMPERATURE city mCursor.getStringcityColIndex temp mCursor.getInttempColIndex // Return a proper item with the proper city and temperature. Just for fun we alternate // the items to make the list easier to read. final String formatStr mContext.getResources.getStringR.string.item_format_string final int itemId position 2 0 R.layout.light_widget_item : R.layout.dark_widget_item RemoteViews rv new RemoteViewsmContext.getPackageName itemId rv.setTextViewTextR.id.widget_item String.formatformatStr temp city // Set the click intent so that we can handle it and show a toast message final Intent fillInIntent new Intent final Bundle extras new Bundle extras.putStringWeatherWidgetProvider.EXTRA_CITY_ID city fillInIntent.putExtrasextras rv.setOnClickFillInIntentR.id.widget_item fillInIntent return rv public RemoteViews getLoadingView //