I have tried to stop my service, even my destroy method also executed but my service is not stopping. I have done my best. Please help me.
my code is mentioned below:
MainActivity
Intent intent=new Intent(this,Myservice.class);
startService(intent); //in start button.Intent intent=new Intent(this,Myservice.class);
stopService(intent); //in stop button.
Myservice
public class Myservice extends Service {
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onCreate() {
super.onCreate();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Toast.makeText(this,"runing",Toast.LENGTH_LONG).show();
{
myTherad th=new myTherad();
Thread thread=new Thread(th);
thread.start();
}
return super.onStartCommand(intent, flags, startId);
}
@Override
public void onDestroy() {
Toast.makeText(this,"stop",Toast.LENGTH_LONG).show();
super.onDestroy();
}
class myTherad implements Runnable {
@Override
public void run() {
synchronized (this)
{
try {
for(int i=0;i<10;i++)
{
wait(1000);
Log.d("mfnhsdgjkfn","===="+String.valueOf(i));
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
stopSelf();
}
}
}