【问题标题】:Why the base class onReceive() function doesn't call the onUpdate()?为什么基类 onReceive() 函数不调用 onUpdate()?
【发布时间】:2012-01-17 13:15:14
【问题描述】:

我有一个相当简单的主屏幕小部件应用程序,它在单击按钮时显示 Toast。幸运的是,它仅在上电时显示 Toast(或者在我更新模拟器中的 apk 之后)。按钮单击具有发送 ACTION_APPWIDGET_UPDATE 的待处理意图,但基类的 onReceive() 函数不处理它。

这是我的代码:

public class WordWidget extends AppWidgetProvider {

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

        Log.d("WordWidget.onReceive", "onReceive");
        Log.d("WordWidget.onReceive", intent.getAction());

        if (intent.getAction()==null) {
            ctxt.startService(new Intent(ctxt, UpdateService.class));
        } else {
            super.onReceive(ctxt, intent);
        }
    }

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

        // To prevent any ANR timeouts, we perform the update in a service
        Log.d("WordWidget.onUpdate", "onUpdate");

        context.startService(new Intent(context, UpdateService.class));
    }

    public static class UpdateService extends Service {
        @Override
        public void onStart(Intent intent, int startId) {
            Log.d("UpdateService.onStart", "onStart");

            // Build the widget update for today
            RemoteViews updateViews = buildUpdate(this);

            // Push update for this widget to the home screen
            ComponentName thisWidget = new ComponentName(this, WordWidget.class);
            AppWidgetManager manager = AppWidgetManager.getInstance(this);
            manager.updateAppWidget(thisWidget, updateViews);
        }

        public RemoteViews buildUpdate(Context context) {

            Log.d("UpdateService.buildUpdate", "buildUpdate");

            RemoteViews updateViews = new RemoteViews(context.getPackageName(), R.layout.widget_word);

            Intent defineIntent = new Intent(context, WordWidget.class);
            defineIntent.setAction(AppWidgetManager.ACTION_APPWIDGET_UPDATE);

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

            updateViews.setOnClickPendingIntent(R.id.widget, pendingIntent);

            showToast(context);

            return updateViews;
        }

        private void showToast(Context context){
            Log.d("UpdateService.showToast", "showToast");
            Toast.makeText(context, "It is working...", Toast.LENGTH_LONG).show();          
        }

        @Override
        public IBinder onBind(Intent intent) {
            // We don't need to bind to this service
            return null;
        }
    }
}

在启动时(或刚重新安装后),我的小部件会显示 Toast,这是 LogCat:

01-17 13:03:10.855: I/ActivityManager(72): Start proc com.example.android.simplewiktionary for broadcast com.example.android.simplewiktionary/.WordWidget: pid=564 uid=10036 gids={3003}
01-17 13:03:11.045: D/WordWidget.onReceive(564): onReceive
01-17 13:03:11.045: D/WordWidget.onReceive(564): android.appwidget.action.APPWIDGET_UPDATE
01-17 13:03:11.054: D/WordWidget.onUpdate(564): onUpdate
01-17 13:03:11.075: D/UpdateService.onStart(564): onStart
01-17 13:03:11.075: D/UpdateService.buildUpdate(564): buildUpdate
01-17 13:03:11.094: D/UpdateService.showToast(564): showToast
01-17 13:03:12.655: D/dalvikvm(72): GC_EXPLICIT freed 971K, 47% free 13550K/25159K, paused 7ms+52ms

现在一旦启动,如果我点击按钮,这里是 LogCat:

01-17 13:04:16.095: D/dalvikvm(126): GC_EXPLICIT freed 72K, 14% free 14016K/16135K, paused 3ms+3ms
01-17 13:04:16.277: D/WordWidget.onReceive(564): onReceive
01-17 13:04:16.277: D/WordWidget.onReceive(564): android.appwidget.action.APPWIDGET_UPDATE
01-17 13:04:21.365: D/dalvikvm(564): GC_EXPLICIT freed 71K, 5% free 6307K/6595K, paused 3ms+2ms

你可以看到 onUpdate() 没有被 onReceive() 基类函数调用...

你能帮我解决这个问题吗?

谢谢。

【问题讨论】:

    标签: android logcat


    【解决方案1】:

    我终于找到了为什么我自己的 onUpdate() 没有在按钮点击时被 super.onReceive() 执行的原因。

    创建一个将操作设置为 ACTION_APPWIDGET_UPDATE 的意图不足以触发 super.onReceive() 调用对我的覆盖 onUpdate() 函数的调用。根据 AppWidgetProvider 类的 onReceive() 函数的代码,intent 的 extras 字段中必须有一些数据。这就是为什么第一次正确调用 onUpdate() 的原因(当它是来自系统的真正 ACTION_APPWIDGET_UPDATE 意图时)而不是当我单击按钮时......

    这是来自 AppWidgetProvider.java 文件的代码:

    // BEGIN_INCLUDE(onReceive)
    public void  [More ...] onReceive(Context context, Intent intent) {
     // Protect against rogue update broadcasts (not really a security issue,
     // just filter bad broacasts out so subclasses are less likely to crash).
     String action = intent.getAction();
     if (AppWidgetManager.ACTION_APPWIDGET_UPDATE.equals(action)) {
         Bundle extras = intent.getExtras();
         if (extras != null) {
             int[] appWidgetIds = extras.getIntArray(AppWidgetManager.EXTRA_APPWIDGET_IDS);
             if (appWidgetIds != null && appWidgetIds.length > 0) {
                 this.onUpdate(context, AppWidgetManager.getInstance(context), appWidgetIds);
             }
         }
     }
     else if (AppWidgetManager.ACTION_APPWIDGET_DELETED.equals(action)) {
         Bundle extras = intent.getExtras();
         if (extras != null && extras.containsKey(AppWidgetManager.EXTRA_APPWIDGET_ID)) {
             final int appWidgetId = extras.getInt(AppWidgetManager.EXTRA_APPWIDGET_ID);
             this.onDeleted(context, new int[] { appWidgetId });
         }
     }
     else if (AppWidgetManager.ACTION_APPWIDGET_ENABLED.equals(action)) {
         this.onEnabled(context);
     }
     else if (AppWidgetManager.ACTION_APPWIDGET_DISABLED.equals(action)) {
         this.onDisabled(context);
     }
    }
    

    谢谢。

    【讨论】:

      【解决方案2】:

      我建议不要尝试强制调用onUpdate,而是设置PendingIntent 来启动服务:

      Intent intent = new Intent(context, UpdateService.class);    
      PendingIntent pi = PendingIntent.getService(context, 0, intent, 0);
      

      如果这不起作用或者对您来说不是可行的解决方案,请告诉我。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2011-09-16
        • 2013-08-06
        • 1970-01-01
        • 1970-01-01
        • 2022-05-18
        • 1970-01-01
        • 2013-01-28
        相关资源
        最近更新 更多