1

I have service in which I want to have a localbroadcast receiver. I want to recieve a value from the activity to my services onCreate.

Note: I can get a value from an activity to my service in onStartCommand() using intent. But here I want a value from activity in onCreate of the service.

The following is my service file:

    public class MediaServiceSimha extends Service {

        private MediaPlayer player;
        String musicpath;
        private ResponseReceiver receiver;

        public MediaServiceSimha() {}

        @Override
        public IBinder onBind(Intent intent) {
            return null;
        }

        @Override
        public void onDestroy() {
            LocalBroadcastManager localBroadcastManager = LocalBroadcastManager.getInstance(this);
            localBroadcastManager.unregisterReceiver(receiver);
            super.onDestroy();
        }

        @Override
        public boolean onUnbind(Intent intent) {
            return super.onUnbind(intent);
        }

        @
        Override
        public int onStartCommand(Intent intent, int flags, int startId) {
            player.start();
            return super.onStartCommand(intent, flags, startId);
        }

        @Override
        public void onCreate() {
            super.onCreate();
            IntentFilter broadcastFilter = new IntentFilter("com.example.myintentserviceapp.intent_service.ALL_DONE");
            receiver = new ResponseReceiver();
            LocalBroadcastManager localBroadcastManager = LocalBroadcastManager.getInstance(this);
            localBroadcastManager.registerReceiver(receiver, broadcastFilter);
        }

        public class ResponseReceiver extends BroadcastReceiver {

            @Override
            public void onReceive(Context context, Intent intent) {
                musicpath = intent.getStringExtra("musicpath");
            }
        }
    }

The following is my activity file where I want to pass a value called musicpath

public class ServiceTest extends AppCompatActivity {
    Intent intent;

    public static final String TEXT_INPUT = "inText";
    //MediaServiceSimha mediaServiceSimha;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        L.m("activity_oncreate_starting");
        setContentView(R.layout.activity_service_test);
        intent= new Intent(this,MediaServiceSimha.class);

        startService(intent);

        Intent broadcastIntent = new Intent();
        broadcastIntent.setAction("com.example.myintentserviceapp.intent_service.ALL_DONE");
        broadcastIntent.putExtra("musicpath", "/storage/emulated/0/Download/1.mp3");
        LocalBroadcastManager localBroadcastManager = LocalBroadcastManager.getInstance(this);
        localBroadcastManager.sendBroadcast(broadcastIntent)
    }


}

After running, it says musicpath value is null.

How to do it?

manfcas
  • 1,933
  • 7
  • 28
  • 47
Santhosh
  • 9,965
  • 20
  • 103
  • 243
  • you dont need it at all: pass "musicpath" in extras when calling `startService` – pskink Jun 18 '16 at 05:22
  • i'm very confused by your question: the fact you are registering the broadcast receiver in onCreate of your service doesn't mean you'll receive the intent there. simply pass whatever information you need in the intent each time you need to using startService... – Creos Jun 18 '16 at 05:22
  • But why are you doing this? If you really want to pass PATH to service then put it on `Extra(..) `and receive in service – M D Jun 18 '16 at 05:23
  • when you are calling start service it doesn't mean it will call immediately,so wait for oncreate of service,whenever you get callback from service at that time you can send broadcast – skyshine Jun 18 '16 at 05:30

2 Answers2

0

My goal was to pass a variable value directly to oncreate in the service class.

I tried LocalBroadcast's reciver and also onbindservices - onServiceConnected

Both of them i found that they will only get executed untill oncreate -> onbind/onstartcommand get executed in the services.

So only option left is onstartcommand()

So when we pass values using intent then they can be used immediately inside onstartcommand.

Santhosh
  • 9,965
  • 20
  • 103
  • 243
-1

when you are calling start service it doesn't mean it will call immediately,so wait for oncreate of service,whenever your service getstarted you can send a callback to your activity then you can broadcast values to your service

       public service callback()  // callback from service started
        {
        Intent broadcastIntent = new Intent();
                broadcastIntent.setAction("com.example.myintentserviceapp.intent_service.ALL_DONE");
                broadcastIntent.putExtra("musicpath", "/storage/emulated/0/Download/1.mp3");
                LocalBroadcastManager localBroadcastManager = LocalBroadcastManager.getInstance(this);
                localBroadcastManager.sendBroadcast(broadcastIntent)
        } 


      public class MediaServiceSimha extends Service {
      @Override
            public void onCreate() {
                super.onCreate();
                IntentFilter broadcastFilter = new IntentFilter("com.example.myintentserviceapp.intent_service.ALL_DONE");
                receiver = new ResponseReceiver();
                LocalBroadcastManager localBroadcastManager = LocalBroadcastManager.getInstance(this);
                localBroadcastManager.registerReceiver(receiver, broadcastFilter);
              sendCallback(); // using interface or broadcast receiver to send callback to activity
            }

}
skyshine
  • 2,767
  • 7
  • 44
  • 84
  • what does the comment `//using interface or broadcast receiver to send callback to activity` mean. First time i am coming across `public service callback()`. I am completely new to this. can you help me more about these – Santhosh Jun 18 '16 at 09:23
  • @SanthoshYedidi you dont need `LocalBroadcastManager` at all: just pass "musicpath" in extras when calling `startService` – pskink Jun 18 '16 at 10:00
  • But i want to use the musicpath in the oncreate and not in the onstartcommand(). onCreate starts first and then onstartcommand – Santhosh Jun 18 '16 at 10:31
  • @SanthoshYedidi so you need to change your design, whats wrong with passing it in the `Intent`'s extras? – pskink Jun 18 '16 at 10:40
  • becaue i want to start the mediaplayer inside oncreate – Santhosh Jun 18 '16 at 12:03
  • I tried the above solution, i used localbroadcast as a callback, the problem i am facing is, after executing code in onCreate, it goes to onStartcommand, after that only it goes to onRecieve. When i called sendbroadcast() inside onCreate, i was expecting it will go to onRecieve, rather it goes to onStartcommand and only after that it goes to onRecieve – Santhosh Jun 18 '16 at 12:07
  • I have seen many media players. where they have started the mediaplayer inside onCreate. But they used bindingservice, they passed the musicpath from the activity. Instead of that i want to achieve it through localbroadcast – Santhosh Jun 18 '16 at 12:10
  • in oncreate of service you will get service instance,just make service as singleton,create some methods in service,whenever you want call methods you can call using service instance – skyshine Jun 18 '16 at 13:04
  • @SanthoshYedidi whats the difference where you `start()`ing the `MediaPlayer`? and of course the service has to be "local bound service" to easily talk to/from the client activity – pskink Jun 18 '16 at 19:22
  • I decided to use bindservice() to pass variables to oncreate from activity – Santhosh Jun 19 '16 at 04:00
  • @SanthoshYedidi actually `bindService` should be used for binding only, for passing any data you should use Service's `IBinder` object – pskink Jun 19 '16 at 04:03