【问题标题】:android homescreen widget AppWidgetProvider onreceived function intent action and extras are gone/nullandroid 主屏幕小部件 AppWidgetProvider onreceived 功能意图操作和附加功能消失/空
【发布时间】:2016-11-02 10:23:32
【问题描述】:

代码看起来不错,但不确定 AppWidgetProvider 的 onreceived 函数的意图操作为 null 并且应用程序没有放置额外数据的原因。

AppWidgetProvider 代码:

public class Widget_2_6 extends AppWidgetProvider{

    private AlarmManager alarm;

    @Override
    public void onReceive(Context context, Intent intent)
    {
        Log.e("onReceive -- Widget_2_6", "onReceive -- Widget_2_6: action: " + intent.getAction());
        boolean from_boot = intent.getBooleanExtra(BootCompleteBroadcastReceiver.FROM_BOOT, false);
        if(from_boot)
        {
            Log.e("onReceive -- Widget_2_6", "onReceive -- Widget_2_6: onupdate start");
            Log.e("onReceive -- Widget_2_6", "onReceive -- Widget_2_6: onupdate end");
        }
        else
        {
            Log.e("onReceive -- Widget_2_6", "onReceive -- Widget_2_6 not from boot: onupdate start");
            Log.e("onReceive -- Widget_2_6", "onReceive -- Widget_2_6 not from boot: onupdate end");
        }
        super.onReceive(context, intent);
    }

BootCompleteBroadcastReceiver:

public final static String FROM_BOOT = "FROM_BOOT";
    cn = new ComponentName(context, Widget_2_6.class);
                Intent intent_widget_2_6 = new Intent(context, Widget_2_6.class);
                intent.setAction(INTENT_FROM_BOOT);
                intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, manager.getAppWidgetIds(cn));
                intent.putExtra(FROM_BOOT, true);
                context.sendBroadcast(intent_widget_2_6);

androidmanifest.xml:

<receiver android:name="com.example.linktogoogleplay.Widget_2_6">
            <intent-filter>
                <action android:name="android.appwidget.action.APPWIDGET_UPDATE"></action>
                <action android:name="com.example.linktogoogleplay.FROM_BOOT"></action>
            </intent-filter>

            <meta-data android:name="android.appwidget.provider"
                android:resource="@xml/my_widget_provider_2_6" />
        </receiver>

我尝试使用 android.appwidget.action.APPWIDGET_UPDATE 作为 Intent 操作,但仍然收到 null 操作并且应用程序没有放置额外的数据。

(注意:)需要在启动时将小部件更新为最新数据,而不是使用旧数据。当时间改变时,同样的 bootboradcast 接收器也会运行(因为它需要从服务器获取最新信息)

bootBroadcastreceiver 正在工作,因为它可以获取 timechange 操作,只是 appwidgetprovider 部分不工作。

如果有什么遗漏,请告诉我。

【问题讨论】:

    标签: android broadcastreceiver android-widget


    【解决方案1】:

    好的,开始工作了。错误的部分如下:

    cn = new ComponentName(context, Widget_2_6.class);
                    Intent intent_widget_2_6 = new Intent(context, Widget_2_6.class);
                    intent.setAction(INTENT_FROM_BOOT);
                    intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, manager.getAppWidgetIds(cn));
                    intent.putExtra(FROM_BOOT, true);
                    context.sendBroadcast(intent_widget_2_6);
    

    它必须是以下代码(不知道为什么会这样,但仍然没有发送额外的内容,至少这次动作也被发送到接收者而不是 null)。

    Intent intent_widget = new Intent(INTENT_FROM_BOOT);
                intent.putExtra(FROM_BOOT, true);
                context.sendBroadcast(intent_widget);
    

    当然,appWidgetIds 的获取将在 appWidgerProvider 类的 onreceive 上。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多