編輯:關於 Android 開發
AppWidget 是創建在桌面上的窗口小控件,我們可以在這個小控件上進行一些操作(具體視需求而定)。作為新手,我在這裡介紹一下 AppWidget 的簡單用法。
1、在正式介紹 AppWidget 之前,先了解兩個關鍵概念:PendingIntent 和 RemoteViews。
PendingIntent:A description of an Intent and target action to perform with it. Instances of this class are created with getActivity, getActivities, getBroadcast, and getService; the returned object can be handed to other applications so that they can perform the action you described on your behalf at a later time.
翻譯:通過 PendingIntent 來描述一個 Intent 和目標動作,該類實例與 getActivity、getActivities、getBroadcast、getService 一起創建;返回的對象能夠傳給其他程序,以便它們能在稍後執行你想讓它們執行的動作。
結合 Mars 老師的講解可以這樣理解:PendingIntent 能夠包含一個 Intent 對象,並把它傳遞給其他應用,當滿足一定條件時,就會執行這個 Intent 中包含的動作。getActivity 和 getActivities 用於啟動活動,getBroadcast 用於發送廣播,getService 用於開啟服務。
RemoteViews:A class that describes a view hierarchy that can be displayed in another process. The hierarchy is inflated from a layout resource file, and this class provides some basic operations for modifying the content of the inflated hierarchy.
翻譯:RemoteViews 是一個描述視圖層次結構的類,該層次結構可以顯示在另一個進程中;它從布局資源文件加載而來,並提供一些基本操作來修改加載後層次結構的內容。簡言之,RemoteViews 代表的就是顯示在桌面上的 appWidget 整個布局,包括設置的按鈕、圖片等。
2、了解上述概念後,就能更好地理解 AppWidget 了。實現步驟如下:
步驟一:在 res 下新建一個文件夾,在其中新建一個類型為 appwidget_provider 的 xml 文件。它代表顯示在桌面上的整體布局框架(不含細節,具體內容需引用另一個布局文件),示例中名為 example_appwidget_provider.xml:
<?xml version="1.0" encoding="utf-8"?> <appwidget-provider xmlns:android="https://schemas.android.com/apk/res/android" android:minWidth = "280dp" android:minHeight = "100dp" android:updatePeriodMillis = "864000000" android:initialLayout="@layout/example_appwidget"> </appwidget-provider> <!-- 上面設置了布局的寬、高、更新時間周期(毫秒)、包含的布局 -->
步驟二:在 layout 中新建一個 xml 文件作為 appwidget 的實際布局顯示,即上面的 example_appwidget:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="https://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <!-- 這裡是顯示在桌面的appWidget的布局 --> <ImageView android:id="@+id/imageId" android:layout_width="fill_parent" android:layout_height="60dp" android:src="@drawable/cancer"/> <Button android:id="@+id/changeButton" android:layout_width="match_parent" android:layout_height="40dp" android:textColor="#00ff00" android:text="開啟另一個活動"/> </LinearLayout>
步驟三:新建一個繼承 AppWidgetProvider 的 Activity,復寫其中的方法。值得注意的是,AppWidget 的操作並不與當前 MainActivity 處於同一線程,因此不能像主線程那樣直接操作布局內容,需要通過 RemoteViews 來更新。
package com.example.appwight; import android.app.PendingIntent; import android.appwidget.AppWidgetManager; import android.appwidget.AppWidgetProvider; import android.content.ComponentName; import android.content.Context; import android.content.Intent; import android.widget.RemoteViews; public class ExampleAppWidgetProvider extends AppWidgetProvider{ //廣播名稱,需要在AppWidget Manifest.xml文件中聲明 private static final String UPDATE_INTENT = "com.example.appwidget"; @Override public void onReceive(Context context, Intent intent) { //接受廣播的時候調用 String action = intent.getAction(); if (UPDATE_INTENT.equals(action)) { System.out.println("action=======>"+UPDATE_INTENT); RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.example_appwidget); //第一個參數:ImagView的Id,第二個參數:用來替換的圖片 remoteViews.setImageViewResource(R.id.imageId, R.drawable.chilli); AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context); //ComponentName也代表AppWidget控件,與RemoteViews不同的是。前者只是代表著整個控件,後者包含Button,ImageView等内容 ComponentName componentName = new ComponentName(context, ExampleAppWidgetProvider.class); appWidgetManager.updateAppWidget(componentName, remoteViews); }else { //調用父類的onReceive方法 super.onReceive(context, intent); } } @Override public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) { super.onUpdate(context, appWidgetManager, appWidgetIds); System.out.println("onUpdate"); for (int i = 0; i < appWidgetIds.length; i++) { //Intent intent = new Intent(context, SecondActivity.class); Intent intent = new Intent(); intent.setAction(UPDATE_INTENT); /* * pendingIntent可以包含一個intent * 第一個參數是當前上下文,第二個參數是包含的intent * getAcitivity表明這個PendingIntent的目的是為了啟動一個activity,此外還有getService(),和getBroadcast() *PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0); */ PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intent, 0); //remoteView代表的是AppWidget的整個布局,第一個參數是包名,第二個參數是AppWidget的布局 RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.example_appwidget); //為remoteVIew中的按鈕設置點擊事件,第一個為按鈕的id,第二個參數為要啟動的PendingIntent。 remoteViews.setOnClickPendingIntent(R.id.changeButton, pendingIntent); //更新AppWidget,第一個參數為表明是當前哪個AppWidget(因為可以在桌面上創建多個AppWidget) //第二個參數為要更新的RemoteViews的對象 appWidgetManager.updateAppWidget(appWidgetIds[i], remoteViews); } } @Override public void onDeleted(Context context, int[] appWidgetIds) { //刪除AppWidget的時候調用 super.onDeleted(context, appWidgetIds); System.out.println("onDeleted"); } @Override public void onEnabled(Context context) { //當AppWidget被創建的時候調用 super.onEnabled(context); System.out.println("onEnabled"); } @Override public void onDisabled(Context context) { //當最後一個AppWidget被刪除的時候調用 super.onDisabled(context); System.out.println("onDisable"); } }
步驟四:在 AndroidManifest.xml 的 <application> 中聲明:
<receiver android:name="com.example.appwight.ExampleAppWidgetProvider"> //過濾廣播 <intent-filter > <action android:name="android.appwidget.action.APPWIDGET_UPDATE"/> <action android:name="com.example.appwidget"/> </intent-filter> //元數據,將布局與類綁定 <meta-data android:name="android.appwidget.provider" android:resource="@xml/example_appwidget_provider"/> </receiver>
效果圖:


Android Service 全面剖析:生命週期、Local Service、IntentService 與 AIDL 遠程服務
Service 是 Android 四大元件中最容易被誤解的一個。很多人以為它是「比較長命的 Activity」,也以為它天生就能跑背景耗時任務。事實上,Service
Android 離線緩存快速實現:Volley 與圖片快取
離線緩存的目標離線緩存的目標是讓 App 在無網路時仍能顯示上次取得的資料。Volley 內建快取,可與 Universal-ImageLoader 搭配,實現圖文快取。
如何寫一個簡易的文件系統(四):實現 umount
哈哈,時隔幾年,又從磁盤深處翻出了當年的原始代碼 myfs.zip。 -----------------------------------------無敵分割線---
Android ActionBar 完全使用指南:從啟用到自定義樣式與 Action Provider
本文涵蓋內容 ActionBar 的基本概念與版本差異 啟用 ActionBar 的兩種路線 在 ActionBar 上添加按鈕與點擊事件 自定義樣式、背景與 Tab