初めてのウィジェット アプリケーションを作成しようとしましたが、問題が発生しました。だから、私は2つの矢印を持っています:左に切り替え、右に切り替えます。それらの両方について、矢印の方向に応じてホームスクリーンウィジェットのビューを変更する単一のサービスを使用したいと考えています。矢印の方向を区別するために、使用する各インテントに追加のデータを追加します。これは私のサンプルコードです:
RemoteViews remoteViews =
new RemoteViews(context.getPackageName(),
R.layout.presentation_layout);
Intent updateServiceIntent1 = new Intent(context, UpdateService.class);
updateServiceIntent1.putExtra("com.mycompany.direction", 1);
PendingIntent updateServicePendingIntent1 =
PendingIntent.getService(context, 0, updateServiceIntent1,
PendingIntent.FLAG_UPDATE_CURRENT);
//Action when we touch left arrow
remoteViews.setOnClickPendingIntent(R.id.left,
updateServicePendingIntent1);
Intent updateServiceIntent2 = new Intent(context, UpdateService.class);
updateServiceIntent2.putExtra("com.mycompany.direction", 0);
PendingIntent updateServicePendingIntent2 =
PendingIntent.getService(context, 0, updateServiceIntent2,
PendingIntent.FLAG_UPDATE_CURRENT);
//Action when we touch right arrow
remoteViews.setOnClickPendingIntent(R.id.right,
updateServicePendingIntent2);
ComponentName thisWidget =
new ComponentName(context, HelperWidget.class);
AppWidgetManager manager = AppWidgetManager.getInstance(context);
manager.updateAppWidget(thisWidget, remoteViews);
サービスでは、次のことを行います。
@Override
public void onStart(Intent intent, int startId) {
Log.i(TAG, "direction " + intent.getIntExtra("com.mycompany.direction", -1) + "");
}
したがって、デバッグでは常に方向 0を取得しました。しかし、左矢印をタッチすると方向1を取得し、右矢印をタッチすると方向0を取得したいと考えています。この問題で私を助けてもらえますか? ありがとう。