▼Androidメモ▼
ホームウィジェット

ホームウィジェットを作成する。


ハンドセットのホームウィジェットのサイズ
設定するサイズは「(セル数*74)-2」で単位はdp。
セル数は4x4の端末が多い。

実際に表示される時のピクセル数は以下の通り。
画面解像度 セル数 最小サイズ 縦実サイズ 横実サイズ
HVGA 1x1 72x72 80x100 106x74
2x2 146x146 160x200 212x146
3x3 220x220 240x300

318x222

4x4 294x294 320x400 424x296
WVGA 1x1 108x108 120x150 159x111
2x2 219x219 240x300 318x222
3x3 330x330 360x450 477x333
4x4 44xx441 480x600 636x444

タブレットのホームウィジェットのサイズ
設定するサイズは「(セル数*70)-30」で単位はdp。
セル数は8x7の端末が多い。

リソース
resフォルダの下にdrawableフォルダを作成し、以下の画像を追加。
dice1.png

dice2.png

dice3.png

dice4.png

dice5.png

dice6.png


resフォルダの下にxmlフォルダを作成し、以下のホームウィジェット設定xmlを追加。
appwidgetex.xml
<?xml version="1.0" encoding="utf-8"?>
<appwidget-provider
xmlns:android="http://schemas.android.com/apk/res/android"
android:initialLayout="@layout/appwidget"
android:minHeight="72dip"
android:minWidth="72dip"
android:updatePeriodMillis="0">
</appwidget-provider>

resフォルダの下のlayoutフォルダに、以下のレイアウトxmlを追加。
appwidget.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#33000000"
android:orientation="vertical">
<ImageView
android:id="@+id/imageview1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:src="@drawable/dice1"/>
<Button
android:id="@+id/button1"
android:text="振る"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="12sp"
/>
</LinearLayout>


ソースコード
AppWidgetEx.java
package net.npaka.appwidgetex;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.Context;
import android.content.Intent;

//ホームウィジェット
public class AppWidgetEx extends AppWidgetProvider {
//更新時に呼ばれる
@Override
public void onUpdate(Context context,
AppWidgetManager appWidgetManager,int[] appWidgetIds) {
//ホームウィジェットを処理するサービスの実行
Intent intent=new Intent(context,AppWidgetService.class);
context.startService(intent);
}
}

AppWidgetService.java
package net.npaka.appwidgetex;
import java.util.*;
import android.app.*;
import android.appwidget.AppWidgetManager;
import android.content.*;
import android.os.IBinder;
import android.widget.RemoteViews;

//ホームウィジェットを制御するサービス
public class AppWidgetService extends Service {
private static final String ACTION_BTNCLICK =
"net.npaka.AppWidgetService.ACTION_BTNCLICK";

//サービス開始時に呼ばれる
@Override
public void onStart(Intent intent,int startId) {
super.onStart(intent, startId);

//リモートビューの取得
AppWidgetManager manager=AppWidgetManager.getInstance(this);
RemoteViews view=new RemoteViews(getPackageName(),R.layout.appwidget);
if (ACTION_BTNCLICK.equals(intent.getAction())) {
btnClicked(view);
}

//button1とボタンクリックイベントの関連付け
Intent newintent=new Intent();
newintent.setAction(ACTION_BTNCLICK);
PendingIntent pending=PendingIntent.getService(this,0,newintent,0);
view.setOnClickPendingIntent(R.id.button1,pending);

//ホームウィジェットの更新
ComponentName widget=new ComponentName(this,AppWidgetEx.class);
manager.updateAppWidget(widget,view);
}

//バインダーを返す
@Override
public IBinder onBind(Intent intent) {
return null;
}

//ボタンクリック時に呼ばれる
public void btnClicked(RemoteViews view){
int[] ids={
R.drawable.dice1,R.drawable.dice2,R.drawable.dice3,
R.drawable.dice4,R.drawable.dice5,R.drawable.dice6};
int idx=rand(6);
view.setImageViewResource(R.id.imageview1,ids[idx]);
}

//乱数の取得
private static Random rand=new Random();
public static int rand(int num) {
return (rand.nextInt()>>>1)%num;
}
}


AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="net.npaka.appwidgetex"
android:versionCode="1"
android:versionName="1.0">
<application android:icon="@drawable/icon" android:label="@string/app_name">

<!-- ホームウィジェット -->
<receiver android:name="AppWidgetEx" android:label="AppWidgetEx">
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
</intent-filter>
<meta-data android:name="android.appwidget.provider"
android:resource="@xml/appwidgetex" />
</receiver>

<!-- ホームウィジェットを制御するサービス -->
<service android:name="AppWidgetService">
<intent-filter>
<action android:name="net.npaka.AppWidgetService.ACTION_BTNCLICK" />
</intent-filter>
</service>

</application>
<uses-sdk android:minSdkVersion="3" />
</manifest>




−戻る−