1. 程式人生 > >android studio建立桌面外掛widget

android studio建立桌面外掛widget

之前看別人說用android studio ,而我還是用著ecilipse不以為然,試了一次,感覺還不如ecilipse好用,後來還是一直用的ecilipse,知道最近用起了android studio才發現真的很強大,支援各種功能,扯遠了,在ecilipse中製作桌面外掛,又要配置這個又要配置那個,而在android studio中真的是一鍵搞定,沒有比這個更好的了。下面來看看android studio生成的一些關於AppWidgets的一些檔案。

我們來按照一般的設計流程看下程式碼:

1.新建AppWidgetProvider繼承類:MyAppWidget

public class 
MyAppWidget extends AppWidgetProvider { //第一次載入時呼叫,是個靜態函式,很明顯 static void updateAppWidget(Context context, AppWidgetManager appWidgetManager, int appWidgetId) { CharSequence widgetText = context.getString(R.string.appwidget_text); // 載入佈局檔案生成RemoteViews,即要顯示的View
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.my_app_widget); views.setTextViewText(R.id.appwidget_text, widgetText); // Instruct the widget manager to update the widget appWidgetManager.updateAppWidget(appWidgetId, views); } //桌面外掛內容更新函式,生命週期內,每隔固定時間更新一次,呼叫一次
@Override public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) { // There may be multiple widgets active, so update all of them for (int appWidgetId : appWidgetIds) { updateAppWidget(context, appWidgetManager, appWidgetId); } } @Override public void onEnabled(Context context) { // Enter relevant functionality for when the first widget is created } @Override public void onDisabled(Context context) { // Enter relevant functionality for when the last widget is disabled } }
2.需要給 MyAppWidget弄個佈局用來顯示:my_app_widget,這個就不用說多少了

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#09C"
    android:padding="@dimen/widget_margin">

    <TextView
        android:id="@+id/appwidget_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:layout_margin="8dp"
        android:background="#09C"
        android:contentDescription="@string/appwidget_text"
        android:text="@string/appwidget_text"
        android:textColor="#ffffff"
        android:textSize="24sp"
        android:textStyle="bold|italic" />

</RelativeLayout>
3.搞個Widget配置檔案,描述widget的一些引數:my_app_widget_info
<?xml version="1.0" encoding="utf-8"?>
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
android:initialKeyguardLayout="@layout/my_app_widget"			//鎖屏外掛佈局位置
android:initialLayout="@layout/my_app_widget"				//載入佈局位置
android:minHeight="40dp"
android:minWidth="250dp"
android:previewImage="@drawable/example_appwidget_preview"
android:resizeMode="horizontal|vertical"
android:updatePeriodMillis="86400000"			//更新時間間隔
android:widgetCategory="home_screen|keyguard"></appwidget-provider>    //外掛模式:桌面和鎖屏
4.最後一步也是很重要的一步就是配置檔案的設定:AndroidManifest.xml
只加了以下程式碼:

 
<receiver android:name=".MyAppWidget">
    <intent-filter>
        <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
    </intent-filter>

    <meta-data
        android:name="android.appwidget.provider"
        android:resource="@xml/my_app_widget_info" />
</receiver>
可以看出,widget其實就是用了廣播元件,底層功能還要看底層原始碼學習,加油~