0

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
  • Is that the complete logcat for the event? – Mark Jul 06 '16 at 10:08
  • Yes................................. –  Jul 06 '16 at 10:09
  • So where is the Error in the logcat? – Mark Jul 06 '16 at 10:14
  • I don't know. I guess the error happend as result of killing the serivce...But the main problem is that the service start agian –  Jul 06 '16 at 10:18
  • That's because you have `START_STICKY` - use : `START_NOT_STICKY` - http://stackoverflow.com/a/33969065/4252352 – Mark Jul 06 '16 at 10:22
  • But i want it run even if the app killied...So is't possible to kill it from my app and the service won't start agian? –  Jul 06 '16 at 10:32
  • Have you tried it? What you're describing has nothing to do with Start Sticky / Not Sticky – Mark Jul 06 '16 at 10:52
  • So how can I do it? –  Jul 06 '16 at 11:47
  • Just replace : `START_STICKY` with : `START_NOT_STICKY` – Mark Jul 06 '16 at 11:50
  • But I want my app working even if the app killed...So I ask :is it possible to shutdown START_STICKY service from the app? –  Jul 06 '16 at 12:07
  • Have you tried it? I gave you a link that explained what START_STICKY and START_NOT_STICKY mean, I suggest you read it - they are nothing to do with the life cycle of your application, they are how the service behaves if stopped/destroyed. Exiting your app does not affect the Service. Of course if the system, or you call stopService before completing, and try restart itself (START_STICKY) - which is what's happening. – Mark Jul 06 '16 at 12:21
  • I tried. When I close the app the service stop, and when I press the button the service stop and restart agian –  Jul 06 '16 at 12:41
  • But the service closed when I closed the app. What should I do? –  Jul 06 '16 at 12:55
  • Then use startForeground() from within your service - this will tell the system your app has some ongoing work that needs to continue running in the Service. Only downside is there will be a notification – Mark Jul 06 '16 at 13:37
  • OK. I found a way. I added startForeground(). and now from onDestory() I put stopSelf() and then I call killApp()-method I created. inside kilApp I close app with finish(); System.exit(0); –  Jul 06 '16 at 21:41

2 Answers2

0

Use attribute in the manifest file under the Service tag

android:stopWithTask="true"
Aman Jain
  • 2,975
  • 1
  • 20
  • 35
-1

Please replace this method

public void killServ()
{
  stopService(MainActivity.this,MyService.class);
} 
ketan
  • 19,129
  • 42
  • 60
  • 98
ViratBhavsar
  • 104
  • 4