いつもお世話になっております。 たくやと申します。
ただ今Androidの開発の自己学習を兼ねて、AppWidgetでクリック自に画像を変更するウィジェットを作成しようとしています。
AppWidgetProviderを継承したMainという名称のクラスで、onUpdateをオーバーライドし、その中でサービスを呼び出しています。
そのサービスの中でRemoteViewsを作成しimageViewのソースを変更しようとしていますが、全く動きません。
実機でデバックしているのですが、ウィジェットは正常にインストールされホーム画面に配置はできるのですが、クリックしても望んだ動作(画像変更)はしてくれません。
質問は3つです。
①ウィジェットはブレークポイントで止めて確認はできないのでしょうか?
できるのであれば方法を、できないのであれば何か代わりになるような方法があればお願いします。
私の知識不足なのでしょうが、中の動きが不明すぎてちゃんと意図した通りに流れているか確認できれば、と思っています。
②以下にソースを貼り付けますので、お時間がある方は是非どこが悪いのか指摘していただくと助かります。
③こういう場合は添付ファイルにしたほうがよいですか?
質問が長くなってしまし申し訳ありません。
AndroidManifest.xml(エラー・警告なし)
res/layout/main.xml(エラー・警告なし)
res/xml/widget.xml(エラー・警告なし)
Main.java(エラー・警告なし)
package com.appwidget.appwidget;
importは省略します。
public class Main extends AppWidgetProvider {
static int index = 0;
static int[] images = {
R.drawable.g0,
R.drawable.g1,
};
private static final String ACTION_MY_CLICK = "com.appwidget.appwidget.Main.ACTION_MY_CLICK";
@Override
public void onUpdate(Context context,AppWidgetManager appWidgetManager, int[] appWidgetIds){
super.onUpdate(context, appWidgetManager, appWidgetIds);
Intent serviceIntent = new Intent(context, MyService.class);
context.startService(serviceIntent);
}
public static class MyService extends Service{
@Override
public void onStart(Intent intent, int startId){
Intent onClick = new Intent();
onClick.setAction(ACTION_MY_CLICK);
PendingIntent pendingIntent = PendingIntent.getService(this, 0, onClick, 0);
RemoteViews remoteViews = new RemoteViews(getPackageName(),R.layout.main);
remoteViews.setOnClickPendingIntent(R.id.imageView1, pendingIntent);
if(ACTION_MY_CLICK.equals(intent.getAction())){
remoteViews.setImageViewResource(R.id.imageView1, images[index]);
index = ++index % images.length;
}
ComponentName thisWidget = new ComponentName(this,Main.class);
AppWidgetManager manager = AppWidgetManager.getInstance(this);
manager.updateAppWidget(thisWidget, remoteViews);
}
@Override
public IBinder onBind(Intent intent) {
// TODO 自動生成されたメソッド・スタブ
return null;
}
}
}
AndroidManifest.xml(エラー・警告なし)
<uses-sdk
android:minSdkVersion="15"
android:targetSdkVersion="15" />
<application
android:allowBackup="true"
android:icon="@drawable/btn_rating_star_off_pressed"
android:label="@string/app_name">
<receiver android:name=".Main" android:label="AppWidet">
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
</intent-filter>
<meta-data android:name="android.appwidget.provider" android:resource="@xml/widget" />
</receiver>
<service android:name="Main$MyService">
<intent-filter>
<action android:name="com.appwidget.appwidget.Main.ACTION_MY_CLICK" />
</intent-filter>
</service>
</application>
</manifest>
res/layout/main.xml(エラー・警告なし)
ImageViewにsrcを指定しているのは、指定しないとホームに設置したときアイコンなしで設置されてしまうためです。
この指定を外しても動作はしませんでした。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:id="@+id/LinearLayout01"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="
http://schemas.android.com/apk/res/android"
android:orientation="vertical">
<ImageView
android:id="@+id/imageView1"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:gravity="center"
android:contentDescription="@string/test"
android:src="@drawable/btn_rating_star_off_pressed"/>
</LinearLayout>
res/xml/widget.xml
<?xml version="1.0" encoding="utf-8"?>
<appwidget-provider
xmlns:android="
http://schemas.android.com/apk/res/android"
android:minWidth="72dip"
android:minHeight="72dip"
android:updatePeriodMillis="0"
android:initialLayout="@layout/main"
/>