Android home screen widget mysteriously fails to receive touch events

I've got a somewhat graphically complex Android homescreen (via AppWidgetProvider) widget that I simply can't get to respond to touch events. The general idea is that tapping the widget should make it change modes for the next 4-5 seconds to display different information, but for the life of me it's showing no sign of ever receiving the Touch event.

The steps I've taken are as follows:

  • I've implemented an intent-filter within the Manifest.xml like so...

    <action android:name="foo.kung.fancywidget.TOUCHED" />;
    
  • ... and it's inside the <receiver /> container for the widget, right next to the expected APPWIDGET_UPDATE entry that Android Studio helpfully adds.

  • I've ensured that the Layout being used has the clickable attribute set to true on every single element (just to be thorough) including the top-level RelativeLayout itself.

  • I've defined the static string for the thing at the top of the ExtraFancyWidget.class, like so...

    public static final String TOUCHED = "foo.kung.fancywidget.TOUCHED";
    
  • ...and according to what I've been reading it should come through as the broadcast via the onRecieve handler when done like this...

    if (TOUCHED.equals(intent.getAction())) {
        Log.i("onReceive", "Touch event received");
    }
    
  • ...but with a Log.d entry at the top of the onReceive handler I can tell I'm not getting any sort of signals through there at all aside from the heartbeat coming from the system service every ten seconds.

  • Lastly, I'm assigning the intent just like I've been reading about

    private PendingIntent createOnClickIntent(Context context) {
        Intent intent = new Intent(context, ExtraFancyWidget.class);
        intent.setAction(TOUCHED); // WHY DOESN'T THIS WORK?!?!?
        return PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
    }
    
  • ...and it's literally the last thing happening in onUpdate before the view is updated

    views.setOnClickPendingIntent(R.id.TheWholeWidget, createOnClickIntent(context));
    appWidgetManager.updateAppWidget(appWidgetId, views);
    
  • What could I possibly be overlooking or not seeing at this point? I don't know of anything else I'm supposed to be doing or changing to make this work... it just mysteriously ignores the user. ADB never shows me any of the log entries (although it does show a number of other, silly log messages so I know that's working) that would indicate the widget ever sees me tapping it.


    The problem is in step 5, where you're creating the intent. Do NOT set a specific class to the intent, rather create it with only an action, like this:

    Intent intent = new Intent();
    intent.setAction(TOUCHED); // this should work now
    

    or the shorthand

    Intent intent = new Intent(TOUCHED);
    

    If it still doesn't work, consider putting a requestCode other than 0 when building the PendingIntent. Depending on the Android version you're building from, there were some bugs when using just 0.

    return PendingIntent.getBroadcast(context, 1000, intent, PendingIntent.FLAG_UPDATE_CURRENT);
    

    Lastly, if this broadcast will be used only within your app (which is highly probable), consider using a LocalBroadcastManager.

    链接地址: http://www.djcxy.com/p/8942.html

    上一篇: 如何通过我们的CI平台(Hudson)自动增加C#程序集版本?

    下一篇: Android主屏幕小部件神秘失败,无法接收触摸事件