[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>

日々のITエンジニアの日常作業を発信しています。
また、様々なレビュー記事も書いていますので、ぜひご活用ください。

また投稿は基本的に毎週月曜日です。
2025年2月中旬より入院の為投稿できませんが、
早く復帰できるように頑張ります!
その間、過去記事をご参照頂ければ幸いです。

では今週も頑張っていきましょう!
ガンバー!!

AndroidJAVA

コメント