[JAVA,Android]ウィジェットで複数ボタンを制御する方法

ウィジェットで複数ボタンを制御したい

ウィジェットはシンプルに作成したい。
でも機能的にどうしても複数のボタンをつける必要が出てきちゃいました。

サンプルソース

単純に、インテントを2種類作成すれば実現できます。


private static final String ACTION_BTNCLICK = “*****Service.ACTION_BTNCLICK”;
private static final String ACTION_BTNCLICK2 = “*****Service.ACTION_BTNCLICK2”;

Intent newintent = new Intent();
newintent.setAction(ACTION_BTNCLICK);
PendingIntent pending = PendingIntent.getService(this,0,newintent,0);
view.setOnClickPendingIntent(R.id.button1,pending);
if (ACTION_BTNCLICK.equals(intent.getAction())) {
 btnClicked(view);
}

Intent newintent2 = new Intent();
newintent2.setAction(ACTION_BTNCLICK2);
PendingIntent pending2 = PendingIntent.getService(this,0,newintent2,0);
view.setOnClickPendingIntent(R.id.button2,pending2);
if (ACTION_BTNCLICK2.equals(intent.getAction())) {
 btnClicked2(view);
}


<service android:name=”*****Service”>
<intent-filter>
 <action android:name=”*****Service.ACTION_BTNCLICK”/>
 <action android:name=”*****Service.ACTION_BTNCLICK2″/>
</intent-filter>
</service>

コメント