sigrlami
sigrlami

Reputation: 1832

Android App Widget broadcast doesn't work

I have a widget with 1 image view and 1 text view.

onoff

I want to change image on click. Consider following code doesn't catch touch event

package com.sigrlami.rixvpn.widget;

import java.util.Random;

import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.widget.RemoteViews;

import com.sigrlami.rixvpn.R;

public class VpnWidgetProvider extends AppWidgetProvider {

private boolean currentStatus = false;
// log tag
private static final String LOG = "com.sigrlami.rixvpn.widget.VpnWidgetProvider";

//public static final String CLICK = "com.sigrlami.rixvpn.CLICK";

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

    // Get all ids
    ComponentName thisWidget = new ComponentName(context, VpnWidgetProvider.class);
    int[] allWidgetIds = appWidgetManager.getAppWidgetIds(thisWidget);

    for (int widgetId : allWidgetIds) {

        // Create some random data
        int number = (new Random().nextInt(100));

        RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.vpn_widget);
        Log.w("WidgetExample", String.valueOf(number));

        // Set the text
        remoteViews.setTextViewText(R.id.layout_vpn_widget_tv_Check, String.valueOf(number));

        // Register an onClickListener
        Intent intent = new Intent(context, VpnWidgetProvider.class);

        intent.setAction(appWidgetManager.ACTION_APPWIDGET_UPDATE);

        PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intent, 0);

        remoteViews.setOnClickPendingIntent(R.id.layout_vpn_widget_iv_Check, pendingIntent);

        if (currentStatus = false) {
            remoteViews.setImageViewResource(R.id.layout_vpn_widget_iv_Check, R.drawable.on);

        } else {
            remoteViews.setImageViewResource(R.id.layout_vpn_widget_iv_Check, R.drawable.off);
        }

        appWidgetManager.updateAppWidget(widgetId, remoteViews);
    }

}

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

    final String action = intent.getAction();
    if (AppWidgetManager.ACTION_APPWIDGET_UPDATE.equals(action)) {
        if (currentStatus == false) {

            currentStatus = true;

        } else {
            currentStatus = false;
        }

    }
    super.onReceive(context, intent);
}

}

Looking to this part through debugger shows nothing strange, but when I change to getActivity() it shows that I'm trying to start a activity that does not exist. I see that something is going on, but this is useless to me.

// Register an onClickListener
Intent intent = new Intent(context, VpnWidgetProvider.class);

intent.setAction(appWidgetManager.ACTION_APPWIDGET_UPDATE);

PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intent, 0);

remoteViews.setOnClickPendingIntent(R.id.layout_vpn_widget_iv_Check, pendingIntent);

But it works 1 time when app deployed to device. Any suggestions what I have done wrong?

Upvotes: 3

Views: 1357

Answers (1)

ρяσѕρєя K
ρяσѕρєя K

Reputation: 132972

For handling click in AppWidgetProvider you need to use custom broadcast reciver as:

<receiver android:name="VpnWidgetProvider"
            android:label="@string/widget_title" android:exported="false" android:icon="@drawable/volume">
            <intent-filter>
                <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
                <action android:name="com.xxxx.xxxx.VpnWidgetProvider.ACTION_WIDGET_REFRESH"/>
                <action android:name="android.appwidget.action.APPWIDGET_DELETED"/>
                <action android:name="android.media.RINGER_MODE_CHANGED"/>
            </intent-filter>
            <meta-data android:name="android.appwidget.provider"
                android:resource="@xml/widget_info" />

and in AppWidgetProvider class:

public class VpnWidgetProvider extends AppWidgetProvider {
public static String ACTION_WIDGET_REFRESH = "ActionReceiverRefresh";
//your code here...
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
//you code here...

Intent intent = new Intent(context, VpnWidgetProvider.class);
intent.setAction(appWidgetManager.ACTION_WIDGET_REFRESH);//get action here
//you code here...

see this full working example for handling onclick on home Screen AppWidget:

Silenttoggle Home Screen Widget

Upvotes: 5

Related Questions