今天就寫寫 Android 視窗小部件的用法,即App Widget.我們知道,App Widget是應用程式視窗小部件(Widget)是微型的應用程式視圖,它可以被嵌入到其它應用程式中(比如桌面)并接收周期性的更新。你可以通過一個App Widget Provider來釋出一個Widget。
實作App Widget主要用到的類有AppWidgetProvider 它繼承自BroadcastReceiver,它可以接受widget相關的廣播,例如 widget 的更新、删除、開啟和禁用等。接下來,給大家說說實作步驟:
1、在資源檔案夾res/layout中建立widget的布局檔案,我建立的是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"
android:text="時間顯示"
/>
</LinearLayout>
2、在res檔案夾中建立xml檔案夾,并且在xml檔案夾中建立widgetconfig.xml檔案,為widget的配置檔案,如下:
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
android:initialLayout="@layout/widget"
android:minHeight="30dp"
android:minWidth="60dp"
android:updatePeriodMillis="864000" >
</appwidget-provider>
3、建立TimerService繼承自Service,其中實作onCreate(),onDestroy()方法。在onCreate方法中設定定時任務,使每隔一秒widget進行更新。
代碼如下:
public class TimerService extends Service {
private Timer timer;
private SimpleDateFormat dateFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
@Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
return null;
}
@Override
public void onCreate() {
// TODO Auto-generated method stub
super.onCreate();
timer=new Timer();
timer.schedule(new TimerTask() {
@Override
public void run() {
updateViews();
}
}, , );//設定為每隔1秒更新
}
/**
* 更新
*/
private void updateViews(){
String time=dateFormat.format(new Date());
RemoteViews rv=new RemoteViews(getPackageName(), R.layout.widget);
rv.setTextViewText(R.id.tv, time);
AppWidgetManager manager=AppWidgetManager.getInstance(getApplicationContext());
ComponentName cn=new ComponentName(getApplicationContext(), WidgetProvider.class);
manager.updateAppWidget(cn, rv);
}
@Override
public void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
timer=null;
}
4、建立WidgetProvider繼承自AppWidgetProvider,重寫方法,其中onDeleted是将widget從螢幕删除是調用,onDisabled是在最後一個widget從螢幕删除是調用,onEnabled是在第一個widget添加到螢幕是調用,onUpdate是在重新整理的時候調用。我們需要在執行onEnabled方法時開啟服務,在onDisabled方法執行時,停止服務。代碼如下:
public class WidgetProvider extends AppWidgetProvider {
@Override
public void onDeleted(Context context, int[] appWidgetIds) {
// TODO Auto-generated method stub
super.onDeleted(context, appWidgetIds);
//将widget從桌面删除
}
/**
* 最後一個widget被從桌面删除執行
*/
@Override
public void onDisabled(Context context) {
// TODO Auto-generated method stub
super.onDisabled(context);
context.stopService(new Intent(context,TimerService.class));
}
/**
* 表示第一個widget添加到桌面上執行
*/
@Override
public void onEnabled(Context context) {
// TODO Auto-generated method stub
super.onEnabled(context);
context.startService(new Intent(context,TimerService.class));
}
/**
*
*/
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
super.onReceive(context, intent);
}
/**
* 重新整理的時候執行
*/
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
int[] appWidgetIds) {
// TODO Auto-generated method stub
super.onUpdate(context, appWidgetManager, appWidgetIds);
//remoteView和AppWidgetManager
}
}
5、我們需要在定義檔案中定義我們的WidgetProvider和TimerService,代碼如下:
<receiver android:name="com.example.mywidght.WidgetProvider" >
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
</intent-filter>
<meta-data
android:name="android.appwidget.provider"
android:resource="@xml/widghtconfig" />
</receiver>
<service android:name="com.example.mywidght.TimerService" >
</service>
當你做了以上步驟,就可以運作你的程式了,我貼的代碼是我做的一個桌面的時間插件。如果覺得有用請點贊,謝謝。如果有疑問,可加群137149792讨論,共同進步。