小部件未显示在android 4.XX jellybean上

时间:2013-05-26 09:15:33

标签: android android-widget android-4.2-jelly-bean

我正在开发一个程序,在Android版本上显示一个小部件,然后显示4个小部件并正常工作,但在4.xx版本中甚至没有列在小部件更新的锁屏或主屏幕中。

这是我的代码:

@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
public class widget extends AppWidgetProvider {
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[]     appWidgetIds) {
    final int N = appWidgetIds.length;

    // 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, MainActivity.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);
        views.setOnClickPendingIntent(R.id.wdbtn, pendingIntent);

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

        int widgetId = 0;
        Bundle myOptions = appWidgetManager.getAppWidgetOptions (widgetId);

        // Get the value of OPTION_APPWIDGET_HOST_CATEGORY
        int category = myOptions.getInt(AppWidgetManager.OPTION_APPWIDGET_HOST_CATEGORY, -1);

        // If the value is WIDGET_CATEGORY_KEYGUARD, it's a lockscreen widget
        boolean isKeyguard = category == AppWidgetProviderInfo.WIDGET_CATEGORY_KEYGUARD;
        int baseLayout = isKeyguard ? R.layout.keyguardwidget : R.layout.widget;

    }
}

这是我的widget xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<ImageButton
    android:id="@+id/wdbtn"
    android:layout_width="61dp"
    android:layout_height="66dp"
    android:background="@null"
    android:contentDescription="@null"
    android:maxHeight="48dp"
    android:maxWidth="48dp"
    android:minHeight="48dp"
    android:minWidth="48dp"
    android:padding="0dp"
    android:scaleType="fitXY"
    android:src="@drawable/ic_launcher" />

和小部件xml信息:

<?xml version="1.0" encoding="utf-8"?>
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android" 

android:initialLayout="@layout/widget"
android:updatePeriodMillis="3000"
android:previewImage="@drawable/ic_launcher"
android:resizeMode="horizontal|vertical"
android:widgetCategory="home_screen|keyguard"
android:initialKeyguardLayout="@layout/keyguardwidget">

和清单:

<receiver android:name="widget" >
<intent-filter>
    <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
</intent-filter>
<meta-data android:name="android.appwidget.provider"
           android:resource="@xml/widgetinfo" />
</receiver>

正如我所说的小部件完美地在旧设备上工作但在新设备中它没有显示。长期以来我想展示一切。 当然我还有另一种键盘布局,但我认为没有必要把它放在这里。

1 个答案:

答案 0 :(得分:1)

我修好了 在清单中你应该添加.widget 并在provider.xml中:

xmlns:android="http://schemas.android.com/apk/res/android"  
android:minWidth="60dp"
android:minHeight="30dp"
android:initialLayout="@layout/widget"
android:updatePeriodMillis="300000"
android:previewImage="@drawable/ic_launcher"
android:resizeMode="horizontal|vertical"
android:widgetCategory="keyguard|home_screen"
android:configure=""
android:initialKeyguardLayout="@layout/keyguardwidget">

现在新错误whern我添加了一个小部件来屏幕它说应用程序没有安装在AVD上我还没有在真实设备上测试过。