如何在清单中正确声明小部件?

时间:2014-05-08 23:44:30

标签: android android-widget

大家好我是小部件全新的我不知道如何在清单中声明小部件,每当我尝试收到错误时

小部件正在与已在清单

中定义的服务进行通信

包名     package source.justanothermusicplayer.service;

public void onUpdate(Context context, AppWidgetManager appWidgetManager,
            int[] appWidgetIds) {

        RemoteViews controlButtons = new RemoteViews(context.getPackageName(),
                R.layout.widget);

        Intent playIntent = new Intent(Player.BROADCAST_PLAYPAUSE);//player is a class which starts the service 


        PendingIntent playPendingIntent = PendingIntent.getService(
                context, REQUEST_CODE, playIntent, INTENT_FLAGS);

        controlButtons.setOnClickPendingIntent(
                R.id.bPlay, playPendingIntent);

        appWidgetManager.updateAppWidget(appWidgetIds, controlButtons);         
    }
}

2 个答案:

答案 0 :(得分:1)

您的清单应如此receive Widget Update

    <receiver android:name="com.example.app.provider.CustomAppWidgetProvider" >
        <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 Provider资源xml应该看起来像这样

<appwidget-provider
   xmlns:android="http://schemas.android.com/apk/res/android"
   android:minWidth="200dp"
   android:updatePeriodMillis="0"
   android:minHeight="100dp"
   android:initialLayout="@layout/initial_layout">

答案 1 :(得分:0)

以下代码来自官方文档。

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

有关详细信息,请参阅AppWidget Guide