widget就是可以在桌面上添加、刪除的view.
這是一個顯示時間的簡單組件,主要靠service更新時間,發送給widget再更新界面。
佈局
首先是Widget的佈局,就是我們在桌面上看到的View的佈局layout_widget.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</LinearLayout>
然後在res下創建個xml文件夾新建一個widgetconfig,作爲widget的配置文件:
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
android:minWidth="100dp"
android:minHeight="40dp"
android:initialLayout="@layout/layout_widget"
android:updatePeriodMillis="300000"
>
</appwidget-provider>
minWidth、minHeight:最小寬高。
initialLayout:加載的佈局
updatePeriodMillis:更新時間,最小30分鐘
previewImage:3.0以後引入,widget的圖標,沒有的話默認應用的圖標
Widget類
自定義一個WidgetProvider類繼承自AppWidgetProvider,這個類是BroadcastReceiver的子類,這樣你的WidgetProvider類就可以接收來自AppWidgetProvider發送的廣播了。
public class WidgetProvider extends AppWidgetProvider
{
// 組件被刪除
@Override
public void onDeleted(Context context, int[] appWidgetIds)
{
super.onDeleted(context, appWidgetIds);
}
@Override
public void onDisabled(Context context)
{
// 最後一個組件被移除
super.onDisabled(context);
Intent intent = new Intent(context,TimeService.class);
context.stopService(intent);
}
@Override
public void onEnabled(Context context)
{
//第一個組件被添加
super.onEnabled(context);
Intent intent = new Intent(context,TimeService.class);
context.startService(intent);
}
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
int[] appWidgetIds)
{
// 組件更新
super.onUpdate(context, appWidgetManager, appWidgetIds);
}
}
因爲可能一個組件同時存在好幾個,所以,onEnabled是在第一個組件被添加的時候調用,我們在這裏開服務更新時間,onDisabled在最後一個組件被刪除,我們在這裏關服務。onUpdate會在每次更新的時候被調用,所以最好不要進行別的操作,如果provider中設置了android:configure屬性,第一次添加組件的時候onUpdate不會被調用。
Service
後臺Service服務負責更新時間
public class TimeService extends Service
{
Timer timer;
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
@Override
public IBinder onBind(Intent arg0)
{
return null;
}
@Override
public void onCreate()
{
super.onCreate();
timer = new Timer();
timer.schedule(new TimerTask()
{
@Override
public void run()
{
updateViews();
}
}, 0, 1000);
}
public void updateViews()
{
String time = sdf.format(new Date());
// 刷新widget需要remoteviews 和 appwidgetmanager
RemoteViews rViews = new RemoteViews(getPackageName(), R.layout.layout_widget);
rViews.setTextViewText(R.id.tv, time);
AppWidgetManager manager = AppWidgetManager.getInstance(getApplicationContext());
ComponentName provider = new ComponentName(getApplicationContext(), WidgetProvider.class);
manager.updateAppWidget(provider, rViews);
}
@Override
public void onDestroy()
{
super.onDestroy();
}
}
在Service中通知WidgetProvider更新界面。
AndroidManifest.xml
最後不要忘記修改清單文件:
添加Service,添加receiver(WidgetProvider實際上是一個receiver)
<receiver android:name="com.jzd.demo2.WidgetProvider">
<intent-filter >
<action android:name="android.appwidget.action.APPWIDGET_UPDATE"/>
</intent-filter>
<meta-data android:name="android.appwidget.provider"
android:resource="@xml/widgetconfig"/>
</receiver>
最後~~~
這樣我們就可以在桌面上添加自定義的小組件了,不過我們還是可以看到這個應用。這個應該只要我們安裝了在組件頁面就能添加組件,但是這個應用是無用的,所以用下面的方式屏蔽掉這個應用:
把MainActivity中的<category android:name="android.intent.category.LAUNCHER" />
改成<category android:name="android.intent.category.DEFAULT" />
這個這個程序也就沒有了入口,自然沒有圖標了,但是我們依然可以使用自定義的組件
最後是效果圖: