How to start a service in Oncreate without button Onclick ,but it should be stopped using button


How to start a service in Oncreate without button Onclick ,but it should be stopped using button



I am trying to play music continuously in background without any button click,it should play on launching activity,and again it can be stopped by stop button.But it is not stopping as it is placed in Oncreate() ,what can be the solution for this.
Please anyone tell me how to stopservice.Thankyou in advance.


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

start = findViewById(R.id.startbutton);
stop = findViewById(R.id.stopbutton);


Intent i = new Intent(this, Service.class);
this.startService(i);
stop.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent i = new Intent(view.getContext(), Service.class);
stopService(i);
Toast.makeText(MainActivity.this, "stop service called", Toast.LENGTH_SHORT).show();
}
});
start.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent i = new Intent(view.getContext(), Service.class);
startService(i);
}
});

}



Below is my Service code


private MediaPlayer player;
public Service() {
super();
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
player= MediaPlayer.create(this,
Settings.System.DEFAULT_RINGTONE_URI);
player.setLooping(true);
player.start();
return START_STICKY;
}

@Override
public void onDestroy() {
super.onDestroy();
player.stop();
}

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



}





can you update your question with ESS_Service where you start and stop the music?
– Sagar
Jun 29 at 6:53


ESS_Service





Post the Service code also... and try to use Activity context..
– SRB bans
Jun 29 at 6:54





Possible duplicate of Android background music service
– Gowthaman M
Jun 29 at 6:57





Can anyone please help me with this,I can play music when app is launched without clicking button,and can also stop it by clicklistener,but after destroying and reopening the app ,not able to stop service.
– user9396014
Jun 29 at 11:30




4 Answers
4



you can use these piece of code


startService(new Intent(Activity.this, Service.class));

stop.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent i = new Intent(Activity.this, Service.class);
stopService(i);

}
});





I tried this ,service is not stopping at all.Please tell some solution to stop it.
– user9396014
Jun 29 at 9:15





have you changed Activity.this with your activity
– Rahul Chaudhary
Jun 29 at 9:21





yes I have changed.
– user9396014
Jun 29 at 9:23





At first time stop service is working but when I close app and reopen ,stop service is not working
– user9396014
Jun 29 at 9:31





put log and check if service is running, on reopen
– Rahul Chaudhary
Jun 29 at 9:33




Please Add this override method and call what ever service you need.


@Override
public void onResume() {
super.onResume();
startService(new Intent(YourActivity.this, ServiceName.class));
}





Service is getting called,but it is not stopping by clicklistener when I close the app and reopen.
– user9396014
Jun 29 at 9:28






in onDestroy you can stopservice, and also in clicklistener of stop view using stopSelf().
– Harish Reddy
Jun 29 at 13:46




I have thought of two solutions :



1:Just don't use clicklistener . Immediately start the service when launching the activity



2: If you want to use start button , call start.performClick()


start.performClick()



To stop the player on home screen use this method


@Override
public void onPause() {
super.onPause();
player.stop();
}





Yeah I am starting service immediately without clicklistener,but how can I stop service on clicklistener,that is my question.
– user9396014
Jun 29 at 9:08






write the stop.setOnClickListener code outside onCreate and try
– Kevin Kurien
Jun 29 at 9:25



stop.setOnClickListener





If still not working try writing the code in onStart or onResume
– Kevin Kurien
Jun 29 at 9:31


onStart


onResume





I tried both ,still stop service is not working after closing and reopening app
– user9396014
Jun 29 at 9:43





Have you declared the service in AndroidManifest.xml. <service android:enabled="true" android:name=".Service" />
– Kevin Kurien
Jun 29 at 10:51



<service android:enabled="true" android:name=".Service" />



I am returning START_NOT_STICKY in onStartCommand method in Service class instead of START_STICKY.Because of that my service was restarting every time when I reopen the app.So now I am able to stop service and my problem got solved.






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

How to add background colour in existing image using Swift?

Moria Casán

How to make file upload 'Required' in Contact Form 7?