【问题标题】:Android widget not showing on 4.3Android 小部件未在 4.3 上显示
【发布时间】:2015-02-12 11:30:03
【问题描述】:

Widget not showing in widget list 有一个类似的问题,还有很多其他问题的答案基本上是:重启、启动应用程序、等待等。

应用安装后,它会在 Android 2.3 设备上显示小部件。所以代码很好。 但它从未出现在 4.3 设备上。所以 4.3 正在寻找不存在的东西。

有没有人有任何额外的提示?

AndroidManifest.xml

    <receiver
        android:name=".WidgetProvider"
        android:label="@string/widget_name">
        <intent-filter>
           <action
               android:name="android.appwidget.action.APPWIDGET_UPDATE" />
        </intent-filter>
        <meta-data 
            android:name="android.appwidget.provider"
            android:resource="@xml/widget_info" />
    </receiver>

widget_info.xml

   <?xml version="1.0" encoding="utf-8"?>
   <appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android" >
    android:minWidth="208dp"
    android:minHeight="56dp"
    android:minResizeHeight="48dp"
    android:minResizeWidth="48dp"
    android:updatePeriodMillis="86400000"
    android:previewImage="@drawable/ic_launcher"
    android:resizeMode="horizontal|vertical"
    android:widgetCategory="home_screen"
    android:configure=""
    android:initialLayout="@layout/widget_layout"
  </appwidget-provider>

WidgetProvider.java

public class WidgetProviderextends AppWidgetProvider {
   DateFormat df = new SimpleDateFormat("hh:mm:ss");

   public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
     final int N = appWidgetIds.length;

     Log.i("ExampleWidget",  "Updating widgets " + Arrays.asList(appWidgetIds));

     // Perform this loop procedure for each App Widget that belongs to this
     // provider
     for (int i = 0; i < N; i++) {
       int appWidgetId = appWidgetIds[i];

       // Create an Intent to launch ExampleActivity
       Intent intent = new Intent(context, ExampleActivity.class);
       PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);

       // Get the layout for the App Widget and attach an on-click listener
       // to the button
       RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget_layout);
       views.setOnClickPendingIntent(R.id.new_post, pendingIntent);

       // To update a label
       views.setTextViewText(R.id.blogname, df.format(new Date()));

       // Tell the AppWidgetManager to perform an update on the current app
       // widget
       appWidgetManager.updateAppWidget(appWidgetId, views);
     }
   }

以上所有代码大多是用于测试的示例代码,它在 Android 2.3 上运行良好,但在 4.3 上运行良好......在 logcat 输出中运行调试构建时没有错误。

【问题讨论】:

    标签: android widget


    【解决方案1】:

    啊! 终于重做了整个事情,它现在可以工作了——原始代码中有一个字符拼写错误。 widget_info.xml 中的 > 字符放置错误。它应该放在所有属性之后,而不是之前。这在 eclipse、构建应用程序和运行应用程序时都没有报告为问题。在 Android 4.2 上,默认设置必须允许小部件安装继续进行,而在 4.3 上,默认设置可能会禁用安装。

    上面帖子中的一个小线索 - Stack Overflow 突出显示 AndroidManifest.xml 中的每个 XML 属性,而不是原始帖子中显示的 widget_info.xml 中的每个 XML 属性。修复 > 字符也修复了 eclipse 中的突出显示。

    widget_info.xml

       <?xml version="1.0" encoding="utf-8"?>
       <appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
        android:minWidth="208dp"
        android:minHeight="56dp"
        android:minResizeHeight="48dp"
        android:minResizeWidth="48dp"
        android:updatePeriodMillis="86400000"
        android:previewImage="@drawable/ic_launcher"
        android:resizeMode="horizontal|vertical"
        android:widgetCategory="home_screen"
        android:configure=""
        android:initialLayout="@layout/widget_layout" >
      </appwidget-provider>
    

    【讨论】:

    • 祝福你的心!!你拯救了我可怜的受折磨的灵魂!!!我想那里有一些带有该错误的示例,并且可怜的人在网上漫游以寻找为什么他们的小部件不起作用的答案,而这一切都是因为放错了“>”
    【解决方案2】:

    可能是你的App没有启动器Activity造成的由 Activity 启动。

    【讨论】:

    • 上面的句子没有意义。您是说“开始活动一次”吗?我已经澄清该应用程序已启动、卸载和安装、重新启动等。在 Android 4.3 上没有任何效果,而在 2.3 上运行良好。使用相同的代码。
    • 您确定在 Android 2.3 上使用上述代码吗?某些属性如 android:previewImage="@drawable/ic_launcher"android:resizeMode="horizontal|vertical" 在 Android 2.3 中不可用。
    【解决方案3】:

    “存在该错误的示例”在 Android Application Development for Dummies ISBN 978-1-118-38710-8 第 179 页中。

    【讨论】:

      猜你喜欢
      • 2021-10-30
      • 2013-10-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多