我正在尝试运行一项服务,当我停止我的应用程序时,该服务仍在运行意味着它正在继续其任务。当我们停止我们的应用程序时,停止服务的方法是什么?请帮助我如何停止这项服务。如果我在这个类中使用意图从服务调用的地方移回那个类,那么还有一件事我移回了,但是当我按下模拟器上的后退按钮返回时,它也会显示该服务屏幕。
我正在显示服务类的代码
public class SetMovableBG_Inbuilt_SetImage extends Service
{
Timer mytimer;
int interval = 2000;
int interval1 = 4000;
Drawable drawable;
WallpaperManager wpm;
int prev = 0;
int numImage;
private ArrayList<Integer> imgId;
@Override
public void onCreate()
{
super.onCreate();
mytimer = new Timer();
wpm = WallpaperManager.getInstance(this);
}
// here i am geting intent from the class from where i am calling this class.
//everything works fine but service not stops even after stop the app
@Override
public int onStartCommand(Intent intent, int flags, int startId)
{
imgId = intent.getIntegerArrayListExtra("ImgId");
numImage = imgId.size();
mytimer.schedule(new TimerTask()
{
@Override
public void run()
{
// TODO Auto-generated method stub
if ( prev >= numImage )
{
prev = 0;
}
try
{
wpm.setResource(imgId.get(prev));
}
catch ( IOException e )
{
e.printStackTrace();
}
prev++;
}
}, interval, interval1);
return super.onStartCommand(intent, flags, startId);
}
@Override
public IBinder onBind(Intent arg0)
{
return null;
}
}