0

嗨,我正在开发一个程序,在 4 小部件显示和正常工作之前的 android 版本上开发一个小部件,但在 4.xx 版本中,它甚至没有在小部件 neighter 锁屏或主屏幕中列出。

这是我的代码:

@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;

    }
}

这是我的小部件 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>

正如我所说的小部件可以在旧设备上完美运行,但在新设备中它没有显示。抱歉,我想显示所有内容。当然,我还有另一种键盘保护布局,但我认为没有必要把它放在这里。

4

1 回答 1

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

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

于 2013-05-27T08:27:42.533 回答