今回は簡単なAndroidウィジットを作成する。EasyProjectGenerator for Android Ver1.04以降を実行する。そして言語「Java」、プロジェクト種類「SimpleWidget」としてプロジェクトを自動生成する。
これでウィジット用のプロジェクトができた。主なファイルは、javaソースコード「MainWidgetProvider.java」、ウィジットのレイアウトxml「widget.xml」、ウィジットの挙動xml「widget_info.xml」と「AndroidManifest.xml」内でのウィジット登録になる。
そのままウィジットをAndroidエミュレーターに送り、実際に配置してみる。Eclipseでパッケージをエミュレーターにインストールする。
そして一覧からインストールした「Test115」ウィジットを選ぶ。
不要になったらウィジットを長押ししてからゴミ箱へドラッグすればいい。
実際のウィジットのソースコードを見る。まずはjavaで書かれたメイン処理。
package com.Test115; import android.appwidget.AppWidgetManager; import android.appwidget.AppWidgetProvider; import android.content.Context; import android.content.Intent; import android.widget.RemoteViews; public class MainWidgetProvider extends AppWidgetProvider { @Override public void onEnabled(Context context) { super.onEnabled(context); } @Override public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) { //super.onUpdate(context,appWidgetManager,appWidgetIds); for(int nWidgetId : appWidgetIds) { RemoteViews remoteView = new RemoteViews(context.getPackageName(),R.layout.widget); remoteView.setImageViewResource(R.id.MainImageView,R.drawable.icon); appWidgetManager.updateAppWidget(nWidgetId,remoteView); } } @Override public void onDeleted(Context context, int[] appWidgetIds) { super.onDeleted(context,appWidgetIds); } @Override public void onReceive(Context context, Intent intent) { super.onReceive(context,intent); } }

<?xml version="1.0" encoding="utf-8"?> <FrameLayout xmlns:android="https://schemas.android.com/apk/res/android" android:id="@+id/MainFrameLayout" android:layout_width="fill_parent" android:layout_height="fill_parent"> <LinearLayout android:id="@+id/MainLinearLayout" android:layout_width="fill_parent" android:layout_height="fill_parent"> <ImageView android:id="@+id/MainImageView" android:layout_width="wrap_content" android:layout_height="wrap_content"/> <TextView android:id="@+id/main_text" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/app_name" /> </LinearLayout> </FrameLayout>
利用できるウィジットサイズは72/146/220/294/368dip...になる。 更新間隔は今回はゼロ=更新しないとした。この値の最小値は「1800000」(=30x60x1000msec=30分)となる。つまり時計の秒針や分針などはこの更新指定を使って描画できない。
<?xml version="1.0" encoding="utf-8"?> <appwidget-provider xmlns:android="https://schemas.android.com/apk/res/android" android:initialLayout="@layout/widget" android:minWidth="146dip" android:minHeight="72dip" android:updatePeriodMillis="0"> <!-- width/height set to ((nCells x 74) - 2)dip --> <!-- 72dip, 146dip, 220dip, 294dip, 368dip... --> </appwidget-provider>

<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="https://schemas.android.com/apk/res/android" package="com.Test115" android:versionCode="1" android:versionName="1.0"> <application android:icon="@drawable/icon" android:label="@string/app_name"> <receiver android:name="MainWidgetProvider"> <meta-data android:name="android.appwidget.provider" android:resource="@xml/widget_info" /> <intent-filter> <action android:name="android.appwidget.action.APPWIDGET_UPDATE" /> </intent-filter> </receiver> </application> </manifest>
プロジェクトファイルをダウンロード