Home » Android » android widget with multiple buttons

android widget with multiple buttons

Posted by: admin June 15, 2020 Leave a comment

Questions:

i have developed a android widget with mutiple buttons on it. it works fine and i add first time. but when i reboot the device buttons do not fire. here it is what i do

here is xml config

<receiver android:name="com.islem.widget.MyWidgetMain" android:label="@string/app_name">
<intent-filter>
    <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
    <!-- Broadcast Receiver that will also process our self created action -->
    <action android:name="com.islem.widget.KalanWidgetMain.ACTION_WIDGET_RECEIVER1"/>
    <action android:name="com.islem.widget.KalanWidgetMain.ACTION_WIDGET_RECEIVER2"/>
    <action android:name="com.islem.widget.KalanWidgetMain.ACTION_WIDGET_RECEIVER3"/>
    <action android:name="com.islem.widget.KalanWidgetMain.ACTION_WIDGET_RECEIVER4"/>
    <action android:name="com.islem.widget.KalanWidgetMain.ACTION_WIDGET_RECEIVER5"/>
    <action android:name="com.islem.widget.KalanWidgetMain.ACTION_WIDGET_RECEIVER6"/>
    <action android:name="com.islem.widget.KalanWidgetMain.ACTION_WIDGET_RELOAD_ALARM"/>

</intent-filter>
<meta-data android:name="android.appwidget.provider" android:resource="@xml/k_widget_provider" />

here is the onUpdate method

    @Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {      

    for (int appWidgetId : appWidgetIds) {

        RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.kalan_widget_main);

        Intent refreshButtonIntent = new Intent(context, KalanWidgetMain.class);
        refreshButtonIntent.setAction(ACTION_WIDGET_RECEIVER1);

        Intent voiceTabIntent = new Intent(context, KalanWidgetMain.class);
        voiceTabIntent.setAction(ACTION_WIDGET_RECEIVER2);

        Intent dataTabIntent = new Intent(context, KalanWidgetMain.class);
        dataTabIntent.setAction(ACTION_WIDGET_RECEIVER3);

        Intent smsTabIntent = new Intent(context, KalanWidgetMain.class);
        smsTabIntent.setAction(ACTION_WIDGET_RECEIVER4);

        Intent prevButtonIntent = new Intent(context, KalanWidgetMain.class);
        prevButtonIntent.setAction(ACTION_WIDGET_RECEIVER5);

        Intent nextButtonIntent = new Intent(context, KalanWidgetMain.class);
        nextButtonIntent.setAction(ACTION_WIDGET_RECEIVER6);

        refreshButtonIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
        voiceTabIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
        dataTabIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
        smsTabIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
        prevButtonIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
        nextButtonIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);

        PendingIntent refreshButtonPendingIntent = PendingIntent.getBroadcast(context, appWidgetId, refreshButtonIntent, PendingIntent.FLAG_CANCEL_CURRENT);
        PendingIntent voiceTabPendingIntent = PendingIntent.getBroadcast(context, appWidgetId, voiceTabIntent, PendingIntent.FLAG_UPDATE_CURRENT);
        PendingIntent dataTabPendingIntent = PendingIntent.getBroadcast(context, appWidgetId, dataTabIntent, PendingIntent.FLAG_UPDATE_CURRENT);
        PendingIntent smsTabPendingIntent = PendingIntent.getBroadcast(context, appWidgetId, smsTabIntent, PendingIntent.FLAG_CANCEL_CURRENT);
        PendingIntent prevButtonPendingIntent = PendingIntent.getBroadcast(context, appWidgetId, prevButtonIntent, 0);
        PendingIntent nextButtonPendingIntent = PendingIntent.getBroadcast(context, appWidgetId, nextButtonIntent, 0);

        remoteViews.setOnClickPendingIntent(R.id.kalanRefresh, refreshButtonPendingIntent);
        remoteViews.setOnClickPendingIntent(R.id.kalanTabVoiceBg, voiceTabPendingIntent);
        remoteViews.setOnClickPendingIntent(R.id.kalanTabDataBg, dataTabPendingIntent);
        remoteViews.setOnClickPendingIntent(R.id.kalanTabSMSBg, smsTabPendingIntent);
        remoteViews.setOnClickPendingIntent(R.id.kalanPrev, prevButtonPendingIntent);
        remoteViews.setOnClickPendingIntent(R.id.kalanNext, nextButtonPendingIntent);
        appWidgetManager.updateAppWidget(appWidgetIds, remoteViews);

        KalanUpdater.registerWidget(context, appWidgetManager, appWidgetId); 
        appWidgetManager.updateAppWidget(appWidgetId, remoteViews);
    }
}

here is the onReceive method

    @Override
public void onReceive(Context context, Intent intent) {

    System.out.println("on onReceive");     
    Toast.makeText(context, "onReceive", Toast.LENGTH_SHORT).show();

    // v1.5 fix that doesn't call onDelete Action
    final String action = intent.getAction();
    if (AppWidgetManager.ACTION_APPWIDGET_DELETED.equals(action)) {
        final int appWidgetId = intent.getExtras().getInt(
                AppWidgetManager.EXTRA_APPWIDGET_ID,
                AppWidgetManager.INVALID_APPWIDGET_ID);
        if (appWidgetId != AppWidgetManager.INVALID_APPWIDGET_ID) {
            this.onDeleted(context, new int[] { appWidgetId });
        }
    } else {

        if (intent.getAction().equals(ACTION_WIDGET_RECEIVER1) || intent.getAction().equals(ACTION_WIDGET_RECEIVER2) ||
                intent.getAction().equals(ACTION_WIDGET_RECEIVER3) || intent.getAction().equals(ACTION_WIDGET_RECEIVER4) ||
                intent.getAction().equals(ACTION_WIDGET_RECEIVER5) || intent.getAction().equals(ACTION_WIDGET_RECEIVER6) ||
                intent.getAction().equals(ACTION_WIDGET_RELOAD_ALARM) 
                ) {             
            int appWidgetId = intent.getExtras().getInt(AppWidgetManager.EXTRA_APPWIDGET_ID, AppWidgetManager.INVALID_APPWIDGET_ID);
            System.out.println("appWidgetId:" + appWidgetId + ", action:" + action);




        }


        super.onReceive(context, intent);
    }
}
How to&Answers:

When phone restart, it will call onEnable instead of onUpdate.

–update–

int[] allids = AppWidgetManager
.getInstance(Context)
.getAppWidgetIds(new ComponentName(Context, YourAppWidgetProvider.class);

This will get all ids that is under the control of YourAppWigetProvider. Read more on this