Android AppWidget does not show up in the menu in honeycomb until reboot

落花浮王杯 提交于 2020-01-21 00:30:07

问题


I have created an AppWidget for Honeycomb which is working well, except that when first installed, it does not show up in the Widgets menu so it can not be added to a home screen. Rebooting the device will allow it to show up, or during development, sending it twice from Eclipse will cause it to show up.

Any ideas?

Thanks!


回答1:


Appearently EboMike was right, setting android:installLocation="internalOnly" did fix the issue. Without specifying an install location, it should have defaulted to internalOnly, but did not seem to for me. Perhaps something changed in Honeycomb?

Also, even with internalOnly set, I was still seeing the issue when installing from Eclipse (widget would not show up in the selection menu until the second run) but when installing from the android market, it seems to be working fine which was my major concern.

Thanks!




回答2:


The reason for this is actually described here and it is by design as of Android 3.1. By default, the installLocation will already be set to "internalOnly" so that should not fix the problem and neither should a reboot.

To work around this, an activity needs to be triggered in your widget. This will activate it and it will then appear in the widget list.

To do this, you can add an activity that essentially does nothing like this:

1) In your AndroidManifest.xml, add this inside your "application" tag:

<activity
    android:name=".DummyActivity"
    android:label="@string/app_name">
    <intent-filter> 
        <action android:name="android.intent.action.MAIN" /> 
        <category android:name="android.intent.category.LAUNCHER" /> 
    </intent-filter> 
</activity>

2) Then create a "DummyActivity.java" class in your "src" like this:

package com.domain.app;
import android.app.Activity;
import android.os.Bundle;
public class DummyActivity extends Activity
{
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        finish();
    }
}

Now, when you deploy the widget to your device, that activity will be launched automatically (you'll see a message in your Eclipse console saying "starting activity ...") and it will immediately "finish" without showing anything visual on the device. And now your widget will be listed in the widget list!




回答3:


Spooky,

I did this installLocation fix and it worked first time.

But from then on I still have to reboot every time I update my widget on my Xoom with 3.01 to see it in the list.




回答4:


To solve this problem with my widget I send widget update broadcast, like this in my WidgetController.class:

Intent intent = new Intent();
intent.setClassName(context, context.getPackageName() + ".WidgetProvider");
intent.setAction(AppWidgetManager.ACTION_APPWIDGET_UPDATE);
intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS,
        getWidgetIds(context));
context.sendBroadcast(intent);

then to provide stable widget initialization in the system I send this broadcast from Application object when application or widget start:

public class MyApp extends Application {

    @Override
    public void onCreate() {

        WidgetController.getInstance().updateWidget(getApplicationContext());
    }
}

!!! Important: when AppWidget hosts in application it's enough, but when AppWidget separated from it you should use way proposed by @John above in this topic.



来源:https://stackoverflow.com/questions/6024025/android-appwidget-does-not-show-up-in-the-menu-in-honeycomb-until-reboot

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!