I want to bulid some app that run in background when I press "Start service" and continue in the background even if the app closed or killed. For this I use START_STICKY and it works, but the problem is when I want to kill the service-the app is closed(like I want) and then I get Error(unfortunately the process Service has stopped). and the service try to run again.
How I stop the service?
P.S-I searched solution over the web without success.
My code
MainActivty
public class MainActivity extends AppCompatActivity {
private TextView textView;
private Intent i;
private static Button killSerBut;
private static final String TAG="com.example.elicahi.service";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView=(TextView) findViewById(R.id.textView1);
killSerBut=(Button)findViewById(R.id.kill);
killSerBut.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.d(TAG,"onClick");
textView.setText("OnClick");
killServ();
}
});
//start the service
i= new Intent(this, MyService.class);
startService(i);
}
public void changeText(String msg)
{
textView.setText(msg);
}
public void killServ()
{
finish();
Log.d(TAG,"OnStop");
MyService myService=new MyService();
myService.stopService(i);
}
}
MyService
public class MyService extends Service {
private static final String TAG="com.example.elicahi.service";
private Intent intent;
public MyService( ) {
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.d(TAG, "onStartCommand method called");
Runnable r= new Runnable() {
@Override
public void run() {
for (int i=0; i<10;i++){
long waitFor= System.currentTimeMillis()+1000;
while (System.currentTimeMillis()<waitFor){
synchronized (this){
try{
wait(waitFor-System.currentTimeMillis());
Log.d(TAG, "the service is doing something");
}catch (Exception e){}
}
}
}
}
};
//start the thread that inside the run nethod
Thread elichaiThread = new Thread(r);
elichaiThread.start();
//if the service is destroy- then restart it
return START_STICKY;
}
@Override
public void onDestroy() {
Log.d(TAG,"onDestroy method called");
stopSelf();
}
/* public void killServ(Intent intGet)
{
Log.d(TAG,"Killed");
intent=intGet;
}*/
@Override
public IBinder onBind(Intent intent) {
// TODO: Return the communication channel to the service.
return null;
}
}
and thats my manifest
<service
android:name=".MyService"
android:enabled="true"
android:exported="true" >
</service>
Logcat
07-06 13:07:11.197 1230-1299/com.example.elicahi.service D/com.example.elicahi.service: the service is doing something
07-06 13:07:12.197 1230-1299/com.example.elicahi.service D/com.example.elicahi.service: the service is doing something
07-06 13:07:13.198 1230-1299/com.example.elicahi.service D/com.example.elicahi.service: the service is doing something
07-06 13:07:13.720 1230-1230/com.example.elicahi.service D/com.example.elicahi.service: onClick
07-06 13:07:13.737 1230-1230/com.example.elicahi.service D/com.example.elicahi.service: OnStop
07-06 13:07:14.198 1230-1299/com.example.elicahi.service D/com.example.elicahi.service: the service is doing something
07-06 13:07:15.198 1230-1299/com.example.elicahi.service D/com.example.elicahi.service: the service is doing something
07-06 13:07:16.946 1557-1557/com.example.elicahi.service D/com.example.elicahi.service: onStartCommand method called
07-06 13:07:17.954 1557-1589/com.example.elicahi.service D/com.example.elicahi.service: the service is doing something
07-06 13:07:18.990 1557-1589/com.example.elicahi.service D/com.example.elicahi.service: the service is doing something
07-06 13:07:20.031 1557-1589/com.example.elicahi.service D/com.example.elicahi.service: the service is doing something
07-06 13:07:21.071 1557-1589/com.example.elicahi.service D/com.example.elicahi.service: the service is doing something
07-06 13:07:22.111 1557-1589/com.example.elicahi.service D/com.example.elicahi.service: the service is doing something
07-06 13:07:23.149 1557-1589/com.example.elicahi.service D/com.example.elicahi.service: the service is doing something
07-06 13:07:24.189 1557-1589/com.example.elicahi.service D/com.example.elicahi.service: the service is doing something
07-06 13:07:25.207 1557-1589/com.example.elicahi.service D/com.example.elicahi.service: the service is doing something
07-06 13:07:26.208 1557-1589/com.example.elicahi.service D/com.example.elicahi.service: the service is doing something
07-06 13:07:27.223 1557-1589/com.example.elicahi.service D/com.example.elicahi.service: the service is doing something