Android小部件未在4.3上显示

时间:2013-10-23 22:29:40

标签: android widget

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输出中运行调试版本时没有错误。

3 个答案:

答案 0 :(得分:2)

哎呀! 最后重做整个事情,它现在有效 - 原始代码中只有一个字符拼写错误。 &gt; widget_info.xml中的字符被错误地放置了。它应该放在之后所有属性,而不是之前。这不是eclipse中的问题,也不是构建应用程序,也不是运行应用程序。在Android 4.2上,默认值必须允许小部件安装继续进行,而在4.3上,默认值可能会禁用安装。

上面发布的一个小线索 - Stack Overflow突出显示AndroidManifest.xml中的每个XML属性,但不突出显示原始帖子中显示的widget_info.xml。修复&gt;角色也修复了日食中的突出显示。

<强> 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>

答案 1 :(得分:0)

如果您的应用没有任何启动器活动,可能会导致。 API 11 后,只有那些广播接收器(以及只有那些widgetProvider s)才能注册其应用可以通过Activity启动。

答案 2 :(得分:0)

&#34;那里的样本与错误&#34;是在Android应用程序开发傻瓜ISBN 978-1-118-38710-8第179页。