why my service starts after ondestroy of activity

2019-06-13 17:59发布

问题:

Today I found the strange problem.In my application I am starting a activity from service

Intent in=new Intent(AlarmService.this, GuardActivity.class);
in.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
  startActivity(in);

Now after sometime i call ondestroy of activity. here I observe that my service is started again. This behavior is very strange.Do any body faced same problem

It is simple services with just above line...So I am not posting complete code

Activity

       Timer t = new Timer();
       t.schedule(new TimerTask() {

              @Override
              public void run() {

                     try{
                         finish();//Activity destroy itself after 10 sec
                     }
                     catch(Exception e)
                     {

                     }
                   }
       }, 10*1000);// 10 sec

回答1:

I am posting on answer because of I can't comment, had you stoped service by calling stopself(); after

Intent in=new Intent(AlarmService.this, GuardActivity.class);
in.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(in);