您好,登錄后才能下訂單哦!
本文實例講述了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_"; // 小部件 對應ID int mAppWidgetId = AppWidgetManager.INVALID_APPWIDGET_ID; // 輸入框 EditText mAppWidgetPrefix; public ExampleAppWidgetConfigure() { super(); } @Override public void onCreate(Bundle icicle) { super.onCreate(icicle); setResult(RESULT_CANCELED); // 設置布局 setContentView(R.layout.appwidget_configure); mAppWidgetPrefix = (EditText) findViewById(R.id.appwidget_prefix); // 設置監聽 findViewById(R.id.save_button).setOnClickListener(mOnClickListener); // 獲取intent傳遞過來的數據 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, 實現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文件中描述布局屬性的節點名稱必須為“appwidget-provider”。 * * @description: * @author ldm * @date 2016-5-16 下午1:43:31 */ public class ExampleAppWidgetProvider extends AppWidgetProvider { // Log打印日志標簽 private static final String TAG = "ExampleAppWidgetProvider"; /** * onUpdate() 處理AppWidgetManager.ACTION_APPWIDGET_UPDATE廣播。 * 該廣播在需要AppWidgetProvider提供RemoteViews數據時 * ,由AppWidgetService.sendUpdateIntentLocked()發出。 */ @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]; //設置標題 String titlePrefix = ExampleAppWidgetConfigure.loadTitlePref( context, appWidgetId); //更新AppWidget updateAppWidget(context, appWidgetManager, appWidgetId, titlePrefix); } } /** * onDeleted() 處理AppWidgetManager.ACTION_APPWIDGET_DELETED廣播。 * 該廣播在有該AppWidgetProvider的實例被刪除時 * ,由AppWidgetService.deleteAppWidgetLocked()發出。 */ @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()發出。 */ @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()發出。 */ @Override public void onDisabled(Context context) { Log.d(TAG, "onDisabled"); PackageManager pm = context.getPackageManager(); //設置組件可用 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())); // 創建RemoteViews 對象 RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.appwidget_provider); // 設置RemoteViews 對象的文本 views.setTextViewText(R.id.appwidget_text, text); // 告訴AppWidgetManager 顯示 views對象給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(); //對ACTION進行判斷 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中添加相應組件:
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
更多關于Android相關內容感興趣的讀者可查看本站專題:《Android基本組件用法總結》、《Android開發入門與進階教程》、《Android布局layout技巧總結》、《Android視圖View技巧總結》、《Android編程之activity操作技巧總結》、《Android資源操作技巧匯總》及《Android控件用法總結》
希望本文所述對大家Android程序設計有所幫助。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。