Android編程之桌面小部件AppWidgetProvider用法示例
本文實(shí)例講述了Android編程之桌面小部件AppWidgetProvider用法。分享給大家供大家參考,具體如下:
/** * 桌面小部件 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_"; // 小部件 對(duì)應(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, ArrayList<Integer> appWidgetIds, ArrayList<String> texts) { } }
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" > <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="This text will be shown before the date in our example widget." /> <EditText android:id="@+id/appwidget_prefix" android:layout_width="match_parent" android:layout_height="wrap_content" /> <Button android:id="@+id/save_button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@android:string/ok" /> </LinearLayout>
/** * AppWidgetProvider小部件廣播組件使用: * 1, 實(shí)現(xiàn)AppWidgetProvider的子類,并至少override onUpdate()方法 * 2,在AndroidManifest.xml中,聲明上述的AppWidgetProvider的子類是一個(gè)Receiver,并且: * 該Receiver的intent-filter的Action必須包含“android.appwidget.action.APPWIDGET_UPDATE”; * 該Receiver的meta-data為“android.appwidget.provider”,并用一個(gè)xml文件來描述布局屬性。 * 3, 在第2點(diǎn)中的xml文件中描述布局屬性的節(jié)點(diǎn)名稱必須為“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ù)時(shí) * ,由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的實(shí)例被刪除時(shí) * ,由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被實(shí)例化時(shí),由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的所有實(shí)例中的最后一個(gè)實(shí)例被刪除時(shí) * ,由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 對(duì)象 RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.appwidget_provider); // 設(shè)置RemoteViews 對(duì)象的文本 views.setTextViewText(R.id.appwidget_text, text); // 告訴AppWidgetManager 顯示 views對(duì)象給widget. appWidgetManager.updateAppWidget(appWidgetId, views); } }
小部件布局
<?xml version="1.0" encoding="utf-8"?> <TextView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/appwidget_text" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="#ffff00ff" android:textColor="#ff000000" />
public class ExampleBroadcastReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { Log.d("ExmampleBroadcastReceiver", "intent=" + intent); //獲取廣播的ACTION String action = intent.getAction(); //對(duì)ACTION進(jìn)行判斷 if (action.equals(Intent.ACTION_TIMEZONE_CHANGED) || action.equals(Intent.ACTION_TIME_CHANGED)) { AppWidgetManager gm = AppWidgetManager.getInstance(context); ArrayList<Integer> appWidgetIds = new ArrayList<Integer>(); ArrayList<String> texts = new ArrayList<String>(); ExampleAppWidgetConfigure.loadAllTitlePrefs(context, appWidgetIds, texts); final int N = appWidgetIds.size(); for (int i=0; i<N; i++) { ExampleAppWidgetProvider.updateAppWidget(context, gm, appWidgetIds.get(i), texts.get(i)); } } } }
在AndroidManifest.xml中添加相應(yīng)組件:
ExampleAppWidgetConfigure
<activity android:name=".appwidget.ExampleAppWidgetConfigure" > <intent-filter> <action android:name="android.appwidget.action.APPWIDGET_CONFIGURE" /> </intent-filter> </activity>
ExampleAppWidgetProvider
<receiver android:name=".appwidget.ExampleAppWidgetProvider" > <meta-data android:name="android.appwidget.provider" android:resource="@xml/appwidget_provider" /> <intent-filter> <action android:name="android.appwidget.action.APPWIDGET_UPDATE" /> </intent-filter> </receiver>
ExampleBroadcastReceiver
<receiver android:name=".appwidget.ExampleBroadcastReceiver" android:enabled="false" > <intent-filter> <action android:name="android.intent.ACTION_TIMEZONE_CHANGED" /> <action android:name="android.intent.ACTION_TIME" /> </intent-filter> </receiver>
開源代碼: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é)》
希望本文所述對(duì)大家Android程序設(shè)計(jì)有所幫助。
- Android使用ContentProvider初始化SDK庫方案小結(jié)
- 基于Android FileProvider 屬性配置詳解及FileProvider多節(jié)點(diǎn)問題
- Android ContentProvider實(shí)現(xiàn)手機(jī)聯(lián)系人讀取和插入
- Android利用ContentProvider獲取本地?cái)?shù)據(jù)的方法
- Android7.0行為變更之適配File Provider的方法
- Android7.0中關(guān)于ContentProvider組件詳解
- Android 中自定義ContentProvider與ContentObserver的使用簡單實(shí)例
- Android 中ContentProvider的實(shí)例詳解
- Android控件AppWidgetProvider使用方法詳解
- Android實(shí)現(xiàn)花瓣飄落效果的步驟
相關(guān)文章
Android—基于微信開放平臺(tái)v3SDK開發(fā)(微信支付填坑)
這篇文章主要介紹了Android—基于微信開放平臺(tái)v3SDK開發(fā)(微信支付填坑),具有一定的參考價(jià)值,有需要的可以了解一下。2016-11-11關(guān)于WebView 重定向行為導(dǎo)致的多次加載的問題
這篇文章主要介紹了關(guān)于WebView 重定向行為導(dǎo)致的多次加載的問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-03-03利用Kotlin的協(xié)程實(shí)現(xiàn)簡單的異步加載詳解
這篇文章主要給大家介紹了關(guān)于利用Kotlin的協(xié)程實(shí)現(xiàn)簡單的異步加載的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧。2018-03-03Flutter通過Container實(shí)現(xiàn)時(shí)間軸效果
時(shí)間軸是前端UI經(jīng)常用到的效果,本文講解下Flutter如何通過Container實(shí)現(xiàn),感興趣的朋友可以了解下2021-05-05Android自定義view實(shí)現(xiàn)仿抖音點(diǎn)贊效果
這篇文章主要介紹了Android自定義view實(shí)現(xiàn)仿抖音點(diǎn)贊效果,代碼簡單易懂非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2018-05-05RecyclerView+SnapHelper實(shí)現(xiàn)無限循環(huán)篩選控件
這篇文章主要為大家詳細(xì)介紹了RecyclerView+SnapHelper實(shí)現(xiàn)無限循環(huán)篩選控件,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-10-10Android Studio 3.0 Gradle 配置變更
這篇文章主要介紹了Android Studio 3.0 Gradle 配置變更的相關(guān)知識(shí),即多渠道打包變更和更改打包命名及路徑的代碼,感興趣的朋友跟隨腳本之家小編一起看看吧2018-03-03