Android AppWidget開發實戰,androidappwidget

來源:互聯網
上載者:User

Android AppWidget開發實戰,androidappwidget

AppWidget 架構類1.AppWidgetProvider 

繼承自 BroadcastRecevier , 在AppWidget 應用 update、enable、disable 和 delete 時接收通知。其中,onUpdate、onReceive 是最常用到的方法,它們接收更新通知。
繼承自 AppWidgetProvider 可實現的方法為如下:
onDeleted(Context context, int[] appWidgetIds)
onDisabled(Context context)
onEnabled(Context context)
onReceive(Context context, Intent intent)
onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds)

2.AppWidgetProvderInfo

描述 AppWidget 的大小、更新頻率和初始介面等資訊,以XML 檔案形式存在於應用的 res/xml/目錄下。

3.AppWidgetManger 

負責管理 AppWidget ,向 AppwidgetProvider 發送通知。
bindAppWidgetId(int appWidgetId, ComponentName provider):通過給定的ComponentName 綁定appWidgetId
getAppWidgetIds(ComponentName provider):通過給定的ComponentName 擷取AppWidgetId
getAppWidgetInfo(int appWidgetId):通過AppWidgetId 擷取 AppWidget 資訊
getInstalledProviders():返回一個List<AppWidgetProviderInfo>的資訊
getInstance(Context context):擷取 AppWidgetManger 執行個體使用的內容物件
updateAppWidget(int[] appWidgetIds, RemoteViews views):通過appWidgetId 對傳進來的 RemoteView 進行修改,並重新重新整理AppWidget 組件
updateAppWidget(ComponentName provider, RemoteViews views):通過 ComponentName 對傳進來的 RemoeteView 進行修改,並重新重新整理AppWidget 組件
updateAppWidget(int appWidgetId, RemoteViews views):通過appWidgetId 對傳進來的 RemoteView 進行修改,並重新重新整理AppWidget 組件

4.RemoteViews 

一個可以在其他應用進程中啟動並執行類,向 AppWidgetProvider 發送通知。

Demo

一個簡單的AppWidget步驟

1、在res下建立xml檔案夾,然後建立appwidget_provider.xml

<?xml version="1.0" encoding="UTF-8"?><appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"android:minWidth="60dp"android:minHeight="30dp"android:updatePeriodMillis="86400000"    android:initialLayout="@layout/widget_main"></appwidget-provider>
2、在layout檔案夾下建立布局檔案widget_main.xml
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:id="@+id/layout"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    android:orientation="vertical" ><TextView        android:id="@+id/tv"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:clickable="true"        android:text="點點點我" ></TextView></LinearLayout>
3、定義類繼承自AppWidgetProvider
public class MyAppWidgetProvider extends AppWidgetProvider {    private RemoteViews remoteViews;    @Override    public void onReceive(Context context, Intent intent) {        super.onReceive(context, intent);        if (remoteViews == null) {            remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget_main);        }        if (intent.getAction().equals("com.example.widgettest")) {            if (CommonValiable.isChange) {                remoteViews.setTextViewText(R.id.tv, "點點點我");            } else {                remoteViews.setTextViewText(R.id.tv, "買買買我");            }            Toast.makeText(context, Boolean.toString(CommonValiable.isChange), Toast.LENGTH_LONG).show();            CommonValiable.isChange = !CommonValiable.isChange;        }        AppWidgetManager appWidgetManger = AppWidgetManager.getInstance(context);        int[] appIds = appWidgetManger.getAppWidgetIds(new ComponentName(context, MyAppWidgetProvider.class));        appWidgetManger.updateAppWidget(appIds, remoteViews);    }    @Override    public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {        final int N = appWidgetIds.length;        for (int i = 0; i < N; i++) {            int appWidgetId = appWidgetIds[i];            updateAppWidget(context, appWidgetManager, appWidgetId);        }    }    public void updateAppWidget(Context context, AppWidgetManager appWidgeManger, int appWidgetId) {        remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget_main);        Intent intent = new Intent("com.example.widgettest");        PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intent, 0);        remoteViews.setOnClickPendingIntent(R.id.tv, pendingIntent);        appWidgeManger.updateAppWidget(appWidgetId, remoteViews);    }}
4、在資訊清單檔的application節點下聲明如下

<receiver android:name=".MyAppWidgetProvider" >    <meta-data        android:name="android.appwidget.provider"        android:resource="@xml/appwidget_provider" >    </meta-data>    <intent-filter>        <action android:name="com.example.widgettest" />        <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />    </intent-filter></receiver>

下載Demo請猛戳



android開發中,怎使appWidget在被點擊的時產生對話方塊

不依賴可能是不行, 但是你可以把你的activity弄出dialog 在manifest裡面配置 整個activity就是以個dialog。
在activity下設定這個屬性就可以了
android:theme="@android:style/Theme.Dialog
 
瘋狂Android講義(第2版)與:Android開發實戰經典這兩本書哪個更適合初學者??

《瘋狂android講義》是《瘋狂java講義》的作者,內容比較多,也比較細。當然如果你有比較好的java基礎推薦你看這本。

如果是你是初學者,並且不瞭解java語言,那麼你就選擇《android開發實戰經典》會更加適合你。
 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.