1

I got a problem which I can't understand. I'm trying to solve it for 8 days and still stuck. I asked about that more experienced developers and they can't answer. So please, I desperately ask for help

the service is simple - it has one method yet - it should give me Log - getLog()

public class AudioService extends Service{

     MyBinder binder = new MyBinder();

public void getLog(){Log.d("MyLog","I reached getLog!");}

    @Override
    public IBinder onBind(Intent arg0) {
        // TODO Auto-generated method stub
        return binder;
    }
     class MyBinder extends Binder {
            AudioService getService() {
              return AudioService.this;
            }
          }
        }

MainAcivity crashes when i try to reach audioService.getLog(); It doesn't crash,however, if i insert line AudioService audioService= new AudioService(); but that's what I don't want - I need to set a Service which will play audio so I can start mp3 from one activity and stop it from another. here's MainActivity :

public class MainActivity extends Activity {
    ServiceConnection sConn;
    Intent intservice;
    AudioService audioService ;

    boolean bound=false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        intservice=new Intent(this,AudioService.class);
        sConn=new ServiceConnection(){

            @Override
            public void onServiceConnected(ComponentName arg0, IBinder binder) {
                // TODO Auto-generated method stub
                audioService = ((AudioService.MyBinder) binder).getService(); 
                bound=true;
            }

            @Override
            public void onServiceDisconnected(ComponentName arg0) {
                // TODO Auto-generated method stub
                bound=false;

            }

        };
            startService(intservice);

            audioService.getLog();

    }
    @Override
    protected void onStart() {
      super.onStart();
      bindService(intservice, sConn, Context.BIND_AUTO_CREATE);
    }

    @Override
    protected void onStop() {
      super.onStop();
      if (!bound) return;
      unbindService(sConn);
      bound = false;
    }
}

in case I messed something in Manifest I shall include part with service here :

  </activity>
        <service android:enabled="true" android:name="AudioService"></service>
    </application>

</manifest>

I can't understand what's wrong here so please give me advice where I did wrong

1 Answer 1

0

Well, I've found why it was. Binding is a procedure that needs time and every example existing waits for onClick which means Activity has enough time between onStart() and onCreate(). In my case it goes immediately. So the best way is to move all Activity under public void onServiceConnected(ComponentName arg0, IBinder binder). Which should mean that after binding happened - Activity goes on well.

Hope that will help to many people )

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.