簡體   English   中英

Android小部件未在4.3上顯示

[英]Android widget not showing on 4.3

窗口小部件列表中未顯示的窗口小部件有類似的問題,還有許多其他問題的答案基本上是:重新啟動,啟動應用程序,等待等。

安裝應用后,它會在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輸出中運行調試版本時沒有錯誤。

哎呀! 最后重做整個事情,它現在有效 - 原始代碼中只有一個字符拼寫錯誤。 widget_info.xml中的>字符被錯誤地放置了。 它應該放在所有的屬性之后 ,而不是之前。 這不是eclipse中的問題,也不是構建應用程序,也不是運行應用程序。 在Android 4.2上,默認值必須允許小部件安裝繼續進行,而在4.3上,默認值可能會禁用安裝。

上面發布的一個小線索 - Stack Overflow突出顯示AndroidManifest.xml中的每個XML屬性,但不突出顯示原始帖子中顯示的widget_info.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>

如果你的應用程序沒有任何啟動器活動,可能會導致這種情況。在API 11之后 ,只有那些廣播接收器(以及那些只有那些widgetProvider )可以注冊,他們的應用程序可以由Activity啟動。

“使用那個錯誤的樣本”是在Android應用程序開發傻瓜ISBN 978-1-118-38710-8第179頁。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM