Question

I'm working on an android widget. This is my AppWidgetProvider class:

public class MyWidgetProvider extends AppWidgetProvider {

public static String ACTION_CHARGE = "Charge";
public static String ACTION_BALANCE = "Balance";
public static String ACTION_SEND_BALANCE = "SendBalance";
public static String ACTION_CALL_ME = "CallMe";

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

    // Get all ids
    ComponentName thisWidget = new ComponentName(context,
            MyWidgetProvider.class);
    int[] allWidgetIds = appWidgetManager.getAppWidgetIds(thisWidget);
    for (int widgetId : allWidgetIds) {

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

        // Register onClickListeners
        Intent intent = new Intent(context, MyWidgetProvider.class);
        intent.setAction(ACTION_CHARGE);
        PendingIntent pendingIntent = PendingIntent.getBroadcast(context,
                0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
        remoteViews.setOnClickPendingIntent(R.id.ib_charge, pendingIntent);

        intent = new Intent(context, MyWidgetProvider.class);
        intent.setAction(ACTION_BALANCE);
        pendingIntent = PendingIntent.getBroadcast(context, 1, intent,
                PendingIntent.FLAG_UPDATE_CURRENT);
        remoteViews.setOnClickPendingIntent(R.id.ib_balance, pendingIntent);

        intent = new Intent(context, MyWidgetProvider.class);
        intent.setAction(ACTION_SEND_BALANCE);
        pendingIntent = PendingIntent.getBroadcast(context, 2, intent,
                PendingIntent.FLAG_UPDATE_CURRENT);
        remoteViews.setOnClickPendingIntent(R.id.ib_charge_send,
                pendingIntent);

        intent = new Intent(context, MyWidgetProvider.class);
        intent.setAction(ACTION_CALL_ME);
        pendingIntent = PendingIntent.getBroadcast(context, 3, intent,
                PendingIntent.FLAG_UPDATE_CURRENT);
        remoteViews.setOnClickPendingIntent(R.id.ib_call_me, pendingIntent);

        appWidgetManager.updateAppWidget(widgetId, remoteViews);
    }
}

@Override
public void onReceive(Context context, Intent intent) {
    if (intent.getAction().equals(ACTION_CHARGE)) {
        Log.d("onReceive", ACTION_CHARGE);
        context.startActivity(new Intent("android.intent.action.CALL", Uri
                .parse("tel:*789*1415" + Uri.encode("#"))));
    } else if (intent.getAction().equals(ACTION_BALANCE)) {
        Log.d("onReceive", ACTION_BALANCE);
    } else if (intent.getAction().equals(ACTION_SEND_BALANCE)) {
        Log.d("onReceive", ACTION_SEND_BALANCE);
    } else if (intent.getAction().equals(ACTION_CALL_ME)) {
        Log.d("onReceive", ACTION_CALL_ME);
    }

    super.onReceive(context, intent);
    Log.d("onReceive",
            "***********************************************************************");

}
}

ٌٌBut the onReceive method never gets called. Where's the problem?

Was it helpful?

Solution

If you are using a configure activity in you provider as below, please remember to setOnClickPendingIntent() in you configure activity, or onReceive() method won't be called later, even if the other things are all correct.

<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
                    android:minWidth="40dp"
                    android:minHeight="40dp"
                    android:updatePeriodMillis="86400000"
                    android:previewImage="@drawable/icon"
                    android:initialLayout="@layout/appwidget"
                    android:configure="AppWidgetConfigure"
                    android:resizeMode="horizontal|vertical"
                    android:widgetCategory="home_screen">
</appwidget-provider>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top