0

Hello guys i am totally new to widgets i don't know how i can declare widgets in the manifest,whenever i try i get errors

the widget is communicating with a service which has already been defined in the manifest

package name 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);         
    }
}
Ankit Srivastava
  • 853
  • 1
  • 9
  • 28

2 Answers2

1

Your manifest should look like this to receive the 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>

And the Widget Provider resource xml should look something like this

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

Libin
  • 16,967
  • 7
  • 61
  • 83
0

The code below is from the official documentation.

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

For more information see the AppWidget Guide.

janzoner
  • 1,420
  • 1
  • 11
  • 19