3

I am building a music player and i need to add a seekbar for the songs. I have checked online but most of the results are for a media player that is in an activity as opposed to a service. I am new to using the media player so any detailed results would be greatly appreciated.

Community
  • 1
  • 1
Alex Kombo
  • 3,256
  • 8
  • 34
  • 67

1 Answers1

1

You can use Broadcast Receiver to send data from service to activity.

Add Below Method in Service and call this method whenever you want to update SeekBar.

public static void publishResult(Context context, int percentage){
    Intent intent = new Intent("Broadcast");
    intent.putExtra("INTENT_TYPE", "SEEKBAR_RESULT");
    intent.putExtra("PERCENTAGE", 10);
    LocalBroadcastManager.getInstance(context).sendBroadcast(intent);
}

In Activity Add Following Code To Receive the broadcast messages.

 @Override
    public void onResume() {
        super.onResume();
        //Register Broadcast receiver 
        LocalBroadcastManager.getInstance(getActivity()).registerReceiver(mBroadcastReceiver, new IntentFilter("Broadcast"));
    }

    @Override
    public void onPause() {
        super.onPause();
        //Unregister Broadcast receiver
        LocalBroadcastManager.getInstance(getActivity()).unregisterReceiver(mBroadcastReceiver);
    }

    private BroadcastReceiver mBroadcastReceiver= new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            String intentType = intent.getStringExtra("INTENT_TYPE");
            if(intentType.equalsIgnoreCase("SEEKBAR_RESULT")){
                int percentage = intent.getIntExtra("PERCENTAGE");
            }
        }
    };

For more detail on LocalBroadcastManager Refer: http://developer.android.com/reference/android/support/v4/content/LocalBroadcastManager.html

Dhaval Patel
  • 10,119
  • 5
  • 43
  • 46
  • What about when the activity isnt on the foreground? How will the seekbar update to the correct position when the activity it resumed? – Alex Kombo Oct 18 '15 at 19:16
  • @AlexKombo that's a valid question. You can store the result in Application Global variable or in Shared-preference and on Resume method you can update the seekbar. And for foreground activity you can use Broadcast receiver to update the Seekbar. – Dhaval Patel Oct 18 '15 at 19:26
  • @AlexKombo Happy to help you. :-) – Dhaval Patel Oct 18 '15 at 20:16
  • `"What about when the activity isnt on the foreground?"` that is why i said to use a "bound" service, in this case when no activity is bound to your service, noone is notified about the change, once an activity binds to your service it it directly notified – pskink Oct 19 '15 at 06:43
  • @DhavalPatel yes i read it and this is not a way anybody should follow, just use a "bound service", really – pskink Oct 19 '15 at 08:18
  • @pskink I think it depend upon the scenario. For music player like app "startService" fits more then "bound service". because bound service can't run independently from activity. please correct me if i am wrong. – Dhaval Patel Oct 19 '15 at 08:51
  • @DhavalPatel yes you are wrong, you can have both started and bound service, see [here](http://developer.android.com/intl/es/guide/components/bound-services.html) ^F `Binding to a Started Service` – pskink Oct 19 '15 at 08:55
  • @pskink I think you should write an answer to this question. – Dhaval Patel Oct 19 '15 at 09:14