本文實例講述了Android編程之桌面小部件AppWidgetProvider用法。分享給大家供大家參考,具體如下:
成都創(chuàng)新互聯(lián)專注于廣德網(wǎng)站建設(shè)服務(wù)及定制,我們擁有豐富的企業(yè)做網(wǎng)站經(jīng)驗。 熱誠為您提供廣德營銷型網(wǎng)站建設(shè),廣德網(wǎng)站制作、廣德網(wǎng)頁設(shè)計、廣德網(wǎng)站官網(wǎng)定制、小程序開發(fā)服務(wù),打造廣德網(wǎng)絡(luò)公司原創(chuàng)品牌,更為您提供廣德網(wǎng)站排名全網(wǎng)營銷落地服務(wù)。
/** * 桌面小部件 AppWidget配置 * * @description: * @author ldm * @date 2016-5-16 下午1:57:16 */ public class ExampleAppWidgetConfigure extends Activity { static final String TAG = "ExampleAppWidgetConfigure"; // 保存的文件名 private static final String PREFS_NAME = "com.example.android.apis.appwidget.ExampleAppWidgetProvider"; // 保存的字段KEY private static final String PREF_PREFIX_KEY = "prefix_"; // 小部件 對應(yīng)ID int mAppWidgetId = AppWidgetManager.INVALID_APPWIDGET_ID; // 輸入框 EditText mAppWidgetPrefix; public ExampleAppWidgetConfigure() { super(); } @Override public void onCreate(Bundle icicle) { super.onCreate(icicle); setResult(RESULT_CANCELED); // 設(shè)置布局 setContentView(R.layout.appwidget_configure); mAppWidgetPrefix = (EditText) findViewById(R.id.appwidget_prefix); // 設(shè)置監(jiān)聽 findViewById(R.id.save_button).setOnClickListener(mOnClickListener); // 獲取intent傳遞過來的數(shù)據(jù) Intent intent = getIntent(); Bundle extras = intent.getExtras(); if (extras != null) { mAppWidgetId = extras.getInt(AppWidgetManager.EXTRA_APPWIDGET_ID, AppWidgetManager.INVALID_APPWIDGET_ID); } if (mAppWidgetId == AppWidgetManager.INVALID_APPWIDGET_ID) { finish(); } mAppWidgetPrefix.setText(loadTitlePref(ExampleAppWidgetConfigure.this, mAppWidgetId)); } View.OnClickListener mOnClickListener = new View.OnClickListener() { public void onClick(View v) { final Context context = ExampleAppWidgetConfigure.this; String titlePrefix = mAppWidgetPrefix.getText().toString(); //保存到SharedPreferences文件 saveTitlePref(context, mAppWidgetId, titlePrefix); AppWidgetManager appWidgetManager = AppWidgetManager .getInstance(context); //更新小部件 ExampleAppWidgetProvider.updateAppWidget(context, appWidgetManager, mAppWidgetId, titlePrefix); Intent resultValue = new Intent(); resultValue.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, mAppWidgetId); setResult(RESULT_OK, resultValue); finish(); } }; static void saveTitlePref(Context context, int appWidgetId, String text) { SharedPreferences.Editor prefs = context.getSharedPreferences( PREFS_NAME, 0).edit(); prefs.putString(PREF_PREFIX_KEY + appWidgetId, text); prefs.commit(); } static String loadTitlePref(Context context, int appWidgetId) { SharedPreferences prefs = context.getSharedPreferences(PREFS_NAME, 0); String prefix = prefs.getString(PREF_PREFIX_KEY + appWidgetId, null); if (prefix != null) { return prefix; } else { return context.getString(R.string.appwidget_prefix_default); } } static void deleteTitlePref(Context context, int appWidgetId) { } static void loadAllTitlePrefs(Context context, ArrayListappWidgetIds, ArrayList texts) { } }
/** * AppWidgetProvider小部件廣播組件使用: * 1, 實現(xiàn)AppWidgetProvider的子類,并至少override onUpdate()方法 * 2,在AndroidManifest.xml中,聲明上述的AppWidgetProvider的子類是一個Receiver,并且: * 該Receiver的intent-filter的Action必須包含“android.appwidget.action.APPWIDGET_UPDATE”; * 該Receiver的meta-data為“android.appwidget.provider”,并用一個xml文件來描述布局屬性。 * 3, 在第2點中的xml文件中描述布局屬性的節(jié)點名稱必須為“appwidget-provider”。 * * @description: * @author ldm * @date 2016-5-16 下午1:43:31 */ public class ExampleAppWidgetProvider extends AppWidgetProvider { // Log打印日志標(biāo)簽 private static final String TAG = "ExampleAppWidgetProvider"; /** * onUpdate() 處理AppWidgetManager.ACTION_APPWIDGET_UPDATE廣播。 * 該廣播在需要AppWidgetProvider提供RemoteViews數(shù)據(jù)時 * ,由AppWidgetService.sendUpdateIntentLocked()發(fā)出。 */ @Override public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) { Log.d(TAG, "onUpdate"); final int N = appWidgetIds.length; for (int i = 0; i < N; i++) { //獲取到id int appWidgetId = appWidgetIds[i]; //設(shè)置標(biāo)題 String titlePrefix = ExampleAppWidgetConfigure.loadTitlePref( context, appWidgetId); //更新AppWidget updateAppWidget(context, appWidgetManager, appWidgetId, titlePrefix); } } /** * onDeleted() 處理AppWidgetManager.ACTION_APPWIDGET_DELETED廣播。 * 該廣播在有該AppWidgetProvider的實例被刪除時 * ,由AppWidgetService.deleteAppWidgetLocked()發(fā)出。 */ @Override public void onDeleted(Context context, int[] appWidgetIds) { Log.d(TAG, "onDeleted"); final int N = appWidgetIds.length; for (int i = 0; i < N; i++) { ExampleAppWidgetConfigure.deleteTitlePref(context, appWidgetIds[i]); } } /** * onEnabled() 處理AppWidgetManager.ACTION_APPWIDGET_ENABLED廣播。 * 該廣播在該AppWidgetProvider被實例化時,由AppWidgetService.sendEnableIntentLocked()發(fā)出。 */ @Override public void onEnabled(Context context) { Log.d(TAG, "onEnabled"); PackageManager pm = context.getPackageManager(); pm.setComponentEnabledSetting(new ComponentName( "com.example.android.apis", ".appwidget.ExampleBroadcastReceiver"), PackageManager.COMPONENT_ENABLED_STATE_ENABLED, PackageManager.DONT_KILL_APP); } /** * onDisabled() 處理AppWidgetManager.ACTION_APPWIDGET_DISABLED廣播。 * 該廣播在該AppWidgetProvider的所有實例中的最后一個實例被刪除時 * ,由AppWidgetService.deleteAppWidgetLocked()發(fā)出。 */ @Override public void onDisabled(Context context) { Log.d(TAG, "onDisabled"); PackageManager pm = context.getPackageManager(); //設(shè)置組件可用 pm.setComponentEnabledSetting(new ComponentName( "com.example.android.apis", ".appwidget.ExampleBroadcastReceiver"), PackageManager.COMPONENT_ENABLED_STATE_ENABLED, PackageManager.DONT_KILL_APP); } static void updateAppWidget(Context context, AppWidgetManager appWidgetManager, int appWidgetId, String titlePrefix) { Log.d(TAG, "updateAppWidget appWidgetId=" + appWidgetId + " titlePrefix=" + titlePrefix); CharSequence text = context.getString(R.string.appwidget_text_format, ExampleAppWidgetConfigure.loadTitlePref(context, appWidgetId), "0x" + Long.toHexString(SystemClock.elapsedRealtime())); // 創(chuàng)建RemoteViews 對象 RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.appwidget_provider); // 設(shè)置RemoteViews 對象的文本 views.setTextViewText(R.id.appwidget_text, text); // 告訴AppWidgetManager 顯示 views對象給widget. appWidgetManager.updateAppWidget(appWidgetId, views); } }
小部件布局
<?xml version="1.0" encoding="utf-8"?>
public class ExampleBroadcastReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { Log.d("ExmampleBroadcastReceiver", "intent=" + intent); //獲取廣播的ACTION String action = intent.getAction(); //對ACTION進(jìn)行判斷 if (action.equals(Intent.ACTION_TIMEZONE_CHANGED) || action.equals(Intent.ACTION_TIME_CHANGED)) { AppWidgetManager gm = AppWidgetManager.getInstance(context); ArrayListappWidgetIds = new ArrayList (); ArrayList texts = new ArrayList (); ExampleAppWidgetConfigure.loadAllTitlePrefs(context, appWidgetIds, texts); final int N = appWidgetIds.size(); for (int i=0; i
在AndroidManifest.xml中添加相應(yīng)組件:
ExampleAppWidgetConfigure
ExampleAppWidgetProvider
ExampleBroadcastReceiver
開源代碼:https://github.com/ldm520/ANDROID_API_DEMOS
更多關(guān)于Android相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Android基本組件用法總結(jié)》、《Android開發(fā)入門與進(jìn)階教程》、《Android布局layout技巧總結(jié)》、《Android視圖View技巧總結(jié)》、《Android編程之a(chǎn)ctivity操作技巧總結(jié)》、《Android資源操作技巧匯總》及《Android控件用法總結(jié)》
希望本文所述對大家Android程序設(shè)計有所幫助。