【Android源码 栏目提醒】:网学会员为需要Android源码 的朋友们搜集整理了[Android项目-Gallery3D][media-PhotoAppWidgetProvider java]源码 - 讲义教程相关资料,希望对各位网友有所帮助!
package com.cooliris.media / Copyright C 2009 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. / import android.appwidget.AppWidgetManager import android.appwidget.AppWidgetProvider import android.content.ContentValues import android.content.Context import android.database.Cursor import android.database.sqlite.SQLiteDatabase import android.database.sqlite.SQLiteException import android.database.sqlite.SQLiteOpenHelper import android.graphics.Bitmap import android.graphics.BitmapFactory import android.util.Log import android.widget.RemoteViews import java.io.ByteArrayOutputStream import java.io.IOException / Simple widget to show a user-selected picture. / public class PhotoAppWidgetProvider extends AppWidgetProvider private static final String TAG PhotoAppWidgetProvider private static final boolean LOGD true Override public void onUpdateContext context AppWidgetManager appWidgetManager int appWidgetIds // Update each requested appWidgetId with its unique photo PhotoDatabaseHelper helper new PhotoDatabaseHelpercontext for int appWidgetId : appWidgetIds int specificAppWidget new int appWidgetId RemoteViews views buildUpdatecontext appWidgetId helper if LOGD Log.dTAG sending out views views for id appWidgetId appWidgetManager.updateAppWidgetspecificAppWidget views helper.close Override public void onDeletedContext context int appWidgetIds // Clean deleted photos out of our database PhotoDatabaseHelper helper new PhotoDatabaseHelpercontext for int appWidgetId : appWidgetIds helper.deletePhotoappWidgetId helper.close / Load photo for given widget and build link RemoteViews for it. / static RemoteViews buildUpdateContext context int appWidgetId PhotoDatabaseHelper helper RemoteViews views null Bitmap bitmap helper.getPhotoappWidgetId if bitmap null views new RemoteViewscontext.getPackageName R.layout.photo_frame views.setImageViewBitmapR.id.photo bitmap return views static class PhotoDatabaseHelper extends SQLiteOpenHelper private static final String DATABASE_NAME launcher.db private static final int DATABASE_VERSION 2 static final String TABLE_PHOTOS photos static final String FIELD_APPWIDGET_ID appWidgetId static final String FIELD_PHOTO_BLOB photoBlob PhotoDatabaseHelperContext context supercontext DATABASE_NAME null DATABASE_VERSION Override public void onCreateSQLiteDatabase db db.execSQLCREATE TABLE TABLE_PHOTOS FIELD_APPWIDGET_ID INTEGER PRIMARY KEY FIELD_PHOTO_BLOB BLOB Override public void onUpgradeSQLiteDatabase db int oldVersion int newVersion int version oldVersion if version DATABASE_VERSION Log.wTAG Destroying all old data. db.execSQLDROP TABLE IF EXISTS TABLE_PHOTOS onCreatedb / Store the given bitmap in this database for the given appWidgetId. / public boolean setPhotoint appWidgetId Bitmap bitmap boolean success false try // Try go guesstimate how much space the icon will take when // serialized to avoid unnecessary allocations/copies during // the write. int size bitmap.getWidth bitmap.getHeight 4 ByteArrayOutputStream out new ByteArrayOutputStreamsize bitmap.compressBitmap.CompressFormat.PNG 100 out out.flush out.close ContentValues values new ContentValues values.putPhotoDatabaseHelper.FIELD_APPWIDGET_ID appWidgetId values.putPhotoDatabaseHelper.FIELD_PHOTO_BLOB out.toByteArray SQLiteDatabase db getWritableDatabase db.insertOrThrowPhotoDatabaseHelper.TABLE_PHOTOS null values success true catch SQLiteException e Log.eTAG Could not open database e catch IOException e Log.eTAG Could not serialize photo e if LOGD Log.dTAG setPhoto success success return success static final String PHOTOS_PROJECTION FIELD_PHOTO_BLOB static final int INDEX_PHOTO_BLOB 0 / Inflate and return a bitmap for the given appWidgetId. / public Bitmap getPhotoint appWidgetId Cursor c null Bitmap bitmap null try SQLiteDatabase db getReadableDatabase String selection String.formatsd FIELD_APPWIDGET_ID appWidgetId c db.queryTABLE_PHOTOS PHOTOS_PROJECTION selection null null null null null if c null LOGD Log.dTAG getPhoto query count c.getCount if c null c.moveToFirst byte data c.getBlobINDEX_PHOTO_BLOB if data null bitmap BitmapFactory.decodeByteArraydata 0 data.length catch SQLiteException e Log.eTAG Could not load photo from database e finally if c null c.close return bitmap / Remove any bitmap associated with the given appWidgetId. / public void deletePhotoint appWidgetId try SQLiteDatabase db getWritableDatabase String whereClause String.formatsd FIELD_APPWIDGET_ID appWidgetId db.deleteTABLE_PHOTOS whereClause null catch SQLiteException e Log.eTAG Could not delete photo from database e