2013-05-30
導語:學習一下AppWidget吧。
本文:本文只是在案頭上顯示一個簡單的外掛程式。
Manifest.xml配置:
放置一個receiver, <meta-data>配置
[html]
<receiver
android:name=".Test3_AppWidget"
android:label="Test3"
android:icon="@drawable/run_boy_2"
>
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE"/>
</intent-filter>
<meta-data
android:name="android.appwidget.provider"
android:resource="@xml/test3_appwidget"/>
</receiver>
<receiver
android:name=".Test3_AppWidget"
android:label="Test3"
android:icon="@drawable/run_boy_2"
>
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE"/>
</intent-filter>
<meta-data
android:name="android.appwidget.provider"
android:resource="@xml/test3_appwidget"/>
</receiver>
外掛程式顯示配置:
地區大小、布局、重新整理等等
[html]
<?xml version="1.0" encoding="UTF-8"?>
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
android:minWidth="72dp"
android:minHeight="72dp"
android:updatePeriodMillis="2000"
android:initialLayout="@layout/test3_layout"
>
</appwidget-provider>
<?xml version="1.0" encoding="UTF-8"?>
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
android:minWidth="72dp"
android:minHeight="72dp"
android:updatePeriodMillis="2000"
android:initialLayout="@layout/test3_layout"
>
</appwidget-provider>
外掛程式布局:
可使用FrameLayout/LinearLayout/RelativeLayout, 控制項使用AnalogClock/Button/Chronometer/ImageButton/ProgressBar/TextView...
[html
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TEXT_1"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TEXT_VIEW"
/>
</LinearLayout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TEXT_1"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TEXT_VIEW"
/>
</LinearLayout>
AppWidgetProvider即Receiver:
代碼中什麼都沒寫,需要時擴充吧
[java]
public class Test3_AppWidget extends AppWidgetProvider{
@Override
public void onDeleted(Context context, int[] appWidgetIds) {
super.onDeleted(context, appWidgetIds);
System.out.println("Test3_AppWidget onDeleted");
}
@Override
public void onDisabled(Context context) {
super.onDisabled(context);
System.out.println("Test3_AppWidget onDisabled");
}
@Override
public void onEnabled(Context context) {
super.onEnabled(context);
System.out.println("Test3_AppWidget onEnabled");
}
@Override
public void onReceive(Context context, Intent intent) {
super.onReceive(context, intent);
System.out.println("Test3_AppWidget onReceive");
}
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
int[] appWidgetIds) {
super.onUpdate(context, appWidgetManager, appWidgetIds);
System.out.println("Test3_AppWidget onUpdate");
}
}
public class Test3_AppWidget extends AppWidgetProvider{
@Override
public void onDeleted(Context context, int[] appWidgetIds) {
super.onDeleted(context, appWidgetIds);
System.out.println("Test3_AppWidget onDeleted");
}
@Override
public void onDisabled(Context context) {
super.onDisabled(context);
System.out.println("Test3_AppWidget onDisabled");
}
@Override
public void onEnabled(Context context) {
super.onEnabled(context);
System.out.println("Test3_AppWidget onEnabled");
}
@Override
public void onReceive(Context context, Intent intent) {
super.onReceive(context, intent);
System.out.println("Test3_AppWidget onReceive");
}
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
int[] appWidgetIds) {
super.onUpdate(context, appWidgetManager, appWidgetIds);
System.out.println("Test3_AppWidget onUpdate");
}
}