Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/221.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 为什么基类onReceive()函数没有';t调用onUpdate()?_Android_Logcat - Fatal编程技术网

Android 为什么基类onReceive()函数没有';t调用onUpdate()?

Android 为什么基类onReceive()函数没有';t调用onUpdate()?,android,logcat,Android,Logcat,我有一个相当简单的主屏幕小部件应用程序,它通过点击按钮来显示祝酒词。幸运的是,它仅在通电时(或者在模拟器中更新apk之后)才显示Toast。按钮单击有一个挂起的意图,该意图发送操作\u APPWIDGET\u更新,但基类的onReceive()函数不处理它 这是我的密码: public class WordWidget extends AppWidgetProvider { @Override public void onReceive(Context ctxt, Intent

我有一个相当简单的主屏幕小部件应用程序,它通过点击按钮来显示祝酒词。幸运的是,它仅在通电时(或者在模拟器中更新apk之后)才显示Toast。按钮单击有一个挂起的意图,该意图发送操作\u APPWIDGET\u更新,但基类的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
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
现在,一旦启动,如果我点击按钮,下面是日志:

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
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
正如您所看到的,onReceive()基类函数没有调用onUpdate()

你能帮我做这个吗


谢谢。

我建议您不要试图强制调用
onUpdate
,而是设置
pendingent
启动服务:

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

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

我建议您不要试图强制调用
onUpdate
,而是将
挂起内容设置为启动服务:

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

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

我终于找到了super.onReceive()在单击按钮时没有执行我自己的onUpdate()的原因

将操作设置为action_APPWIDGET_UPDATE创建意图不足以通过super.onReceive()调用触发对my override onUpdate()函数的调用。根据AppWidgetProvider类的onReceive()函数的代码,intent必须在extras字段中包含一些数据。这就是为什么onUpdate()第一次被正确调用的原因(当它是来自系统的实际操作\u APPWIDGET\u更新意图时),而不是当我单击按钮时

以下是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);
 }
}

谢谢。

我终于找到了super.onReceive()不执行我自己的onUpdate()的原因

将操作设置为action_APPWIDGET_UPDATE创建意图不足以通过super.onReceive()调用触发对my override onUpdate()函数的调用。根据AppWidgetProvider类的onReceive()函数的代码,intent必须在extras字段中包含一些数据。这就是为什么onUpdate()第一次被正确调用的原因(当它是来自系统的实际操作\u APPWIDGET\u更新意图时),而不是当我单击按钮时

以下是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);
 }
}
谢谢