永远不会调用Android AppWidgetProvider的onReceive方法 [英] Android AppWidgetProvider's onReceive method never gets called

查看:513
本文介绍了永远不会调用Android AppWidgetProvider的onReceive方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个Android小部件.这是我的AppWidgetProvider类:

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

}
}

ٌٌ但是onReceive方法永远不会被调用.哪里出问题了?

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

推荐答案

如果您在提供程序中使用如下配置活动,请记住在配置活动中使用setOnClickPendingIntent(),否则将不会使用onReceive()方法以后再打电话,即使其他一切都正确.

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>

这篇关于永远不会调用Android AppWidgetProvider的onReceive方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆