永远不会调用Android AppWidgetProvider的onReceive方法

时间:2014-04-27 12:03:12

标签: android android-widget

我正在研究一个Android小部件。这是我的AppWidgetProvider类:

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",
            "***********************************************************************");

}
}

但onReceive方法永远不会被调用。问题出在哪里?

1 个答案:

答案 0 :(得分:1)

如果您在提供商中使用配置活动,如下所示,请记住在配置活动中setOnClickPendingIntent(),或者以后不会调用onReceive()方法,即使其他内容都是正确的。

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