一、widget桌面的小掛件,外面可以用app widget制作自己的小掛件。制作掛件的過程:、
1、android studio下在res目錄下點擊右鍵,選擇new->widget->app widget 創建一個app wiget后會讓你創建app wiget類
<1> 自動的在src文件下創建xml文件夾,該文件夾下會有你剛創建的widget的xml文件
<2> 自動在layout目錄下創建相應的布局文件,該布局文件提供給widget使用,它是桌面上顯示的小掛件的布局文件
<3> 在包名下面自動創建相應的widget類,NewAppWidget 類是一個廣播接收者
<4> 在mainfest文件下會自動創建widgetUpdate的一個廣播接收者。
下面是創建了newAppWidget后的目錄:

new_app_widget_info.xml是創建的wiget的配置:
<?xml version="1.0" encoding="utf-8"?>
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
android:initialKeyguardLayout="@layout/new_app_widget"
android:initialLayout="@layout/new_app_widget"
android:minHeight="30dp"
android:minWidth="180dp"
android:previewImage="@drawable/example_appwidget_preview"
android:resizeMode="horizontal|vertical"
android:updatePeriodMillis="1000"
android:widgetCategory="home_screen"></appwidget-provider>
Notice:updateperiodMillis是掛件更新的時間,系統為了節約資源規定設置大於30分鍾才有效,小於30分鍾還是30分鍾更新一次。
new_app_widget.xml是掛件的布局文件:就是說小掛件只是個TextView的控件
<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="ca"
android:textColor="#ffffff"
android:textSize="20dp"
android:textStyle="bold|italic" />
</RelativeLayout>
NewAppWidget.class就是Mainfest文件中靜態注冊的監聽器的實現,里面如下:其中方法調用的時間見代碼中備注
package com.example.user.appwidget;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.Context;
import android.widget.RemoteViews;
import android.widget.TextView;
import java.util.Calendar;
/**
* Implementation of App Widget functionality.
*/
public class NewAppWidget extends AppWidgetProvider {
static void updateAppWidget(Context context, AppWidgetManager appWidgetManager,
int appWidgetId) {
Calendar c = Calendar.getInstance();
int hour = c.get(Calendar.HOUR_OF_DAY);
int minute = c.get(Calendar.MINUTE);
int seconds = c.get(Calendar.SECOND);
CharSequence widgetText = "Time:"+hour+":"+minute+":"+seconds;//context.getString(R.string.appwidget_text);
// Construct the RemoteViews object
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.new_app_widget);
views.setTextViewText(R.id.appwidget_text, widgetText);
// Instruct the widget manager to update the widget
appWidgetManager.updateAppWidget(appWidgetId, views);
}
// 到達指定更新時間(new_app_widget_info.xml中指定的,大於30分鍾才有效,小於30分鍾會30分鍾執行一次)
// 或者向桌面添加Widget時候執行
@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);
}
}
//當第一個widget創建時候執行
@Override
public void onEnabled(Context context) {
// Enter relevant functionality for when the first widget is created
}
//當最后一個widget實例被刪除時執行
@Override
public void onDisabled(Context context) {
// Enter relevant functionality for when the last widget is disabled
}
}
Mainfest中靜態注冊廣播接受者:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.user.appwidget">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name=".NewAppWidget">
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
</intent-filter>
<meta-data
android:name="android.appwidget.provider"
android:resource="@xml/new_app_widget_info" />
</receiver>
</application>
</manifest>
Notice:如果用的是Eclipse可能就要自己手動在相應的地方創建文件,這也是android studio優於eclipse的地方。
