0

I have a music player which plays music using a service,all the broadcast receivers within that service are defined inside the service and not externally.

I am totally new to widgets so i had been seeing a few tutorials.But they didn't help me much I am totally new to pending intents soo.I am so confused right now please help me out... All i want to do is just trigger the broadcast inside the service using the button of the widget ...

Here is the copy pasted code which i had been trying to understand

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

        Intent playIntent = new Intent(context, Music_service.class);



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

        controlButtons.setOnClickPendingIntent(
                R.id.bPlay, playPendingIntent);
        appWidgetManager.updateAppWidget(appWidgetIds, controlButtons);         

And here is my app :D enter image description here

Ankit Srivastava
  • 853
  • 1
  • 9
  • 28

1 Answers1

0

Create a custom Intent Action and set it as PendingIntent to the widget item ( button in your case)

 Intent intent = new Intent("com.example.app.ACTION_PLAY");
 PendingIntent pendingIntent = PendingIntent.getService(context.getApplicationContext(), 100,
            intent, PendingIntent.FLAG_UPDATE_CURRENT);
 RemoteViews remoteViews = new RemoteViews(context.getPackageName(),R.layout.widget_layout);
 remoteViews.setOnClickPendingIntent(R.id.bPlay, pendingIntent);

Then, change your manifest to handle the Action passed in PendingIntent

   <service android:name="com.example.app.services.CustomService" >
        <intent-filter>
            <action android:name="com.mediabook.app.ACTION_PLAY" />
            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </service>

Finally , when the play button is clicked , the Service will receive the Action and started. Check for the Action in onStartCommand

@Override
public int onStartCommand(Intent intent, int flag, int startId) {

    if(intent.getAction().equalsIgnoreCase("com.example.app.ACTION_PLAY")){
        // do your stuff here
    }

You can set similar custom Actions for all the required views in the Widget.

See the compile code here https://gist.github.com/androidbensin/4a9f044ac0b3110c049e

Hope your are good now. Let me know if any issues

Libin
  • 16,967
  • 7
  • 61
  • 83
  • please tell me if i can use broadcasts instead ?? – Ankit Srivastava May 09 '14 at 13:30
  • can i somehow trigger a broadcast inside the service – Ankit Srivastava May 09 '14 at 13:37
  • Yes sure you can use. See my answer here http://stackoverflow.com/questions/23443946/music-player-control-in-notification/23445191#23445191 – Libin May 09 '14 at 14:09
  • Yes. It doesn't matter widget or notification. Its about pending intent to remote views – Libin May 09 '14 at 14:12
  • hi,but this broadcast has been defined in it's own class right?I have this broadcast inside the service...when broadcasts are inside another class they are not registered in the manifest but they are registered like this – Ankit Srivastava May 09 '14 at 14:12
  • registerReceiver(broadcastReceiver, new IntentFilter( Player.BROADCAST_SEEKBAR)); thank you for your patience though – Ankit Srivastava May 09 '14 at 14:13
  • Move the broadcast as separate class. Sorry I have a meeting now. Try it and let me know , will answer after some time – Libin May 09 '14 at 14:14
  • well there are many broadcasts i really don't know how much code i will have to rewrite for it – Ankit Srivastava May 09 '14 at 14:15
  • seems like you are out of the meeting now :P :D – Ankit Srivastava May 09 '14 at 15:12
  • Was busy with work all day , sorry :) You can also set the `custom action` through `intent filter` from code. See my updated answer here http://stackoverflow.com/questions/23443946/music-player-control-in-notification/23445191#23445191 – Libin May 09 '14 at 22:19
  • nevermind and thanx anyways i got it working ...i just was messing one thing up...everything was right with my code ... :) I am accepting your answer – Ankit Srivastava May 10 '14 at 22:55