Android 应用停止后,服务正在运行

Android 应用停止后,服务正在运行,android,android-service,Android,Android Service,我正在尝试运行一项服务,当我停止我的应用程序时,该服务仍在运行意味着它正在继续它的任务。当我们停止应用程序时,停止服务的方法是什么?请帮助我如何停止这项服务。还有一件事,如果我在这个类中使用intent返回到服务调用的那个类,我会返回,但当我按下emulator上的后退按钮返回时,它也会显示那个服务屏幕 我正在显示服务等级代码 public class SetMovableBG_Inbuilt_SetImage extends Service { Timer mytimer; i

我正在尝试运行一项服务,当我停止我的应用程序时,该服务仍在运行意味着它正在继续它的任务。当我们停止应用程序时,停止服务的方法是什么?请帮助我如何停止这项服务。还有一件事,如果我在这个类中使用intent返回到服务调用的那个类,我会返回,但当我按下emulator上的后退按钮返回时,它也会显示那个服务屏幕

我正在显示服务等级代码

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;
    }
}
公共类SetMovableBG\u内置\u SetImage扩展服务
{
计时器mytimer;
整数区间=2000;
整数区间1=4000;
可拉伸;
墙纸经理;
int prev=0;
整数图像;
私人ArrayList imgId;
@凌驾
public void onCreate()
{
super.onCreate();
mytimer=新计时器();
wpm=wallperManager.getInstance(this);
}
//在这里,我从我调用这个类的类中获取意图。
//一切正常,但即使停止应用程序,服务也不会停止
@凌驾
公共int onStartCommand(Intent Intent、int标志、int startId)
{
imgId=intent.getIntegerArrayListExtra(“imgId”);
numImage=imgId.size();
mytimer.schedule(新TimerTask()
{
@凌驾
公开募捐
{
//TODO自动生成的方法存根
如果(上一个>=numImage)
{
prev=0;
}
尝试
{
setResource(imgId.get(prev));
}
捕获(IOE异常)
{
e、 printStackTrace();
}
prev++;
}
},间隔,间隔1);
返回super.onStartCommand(intent、flags、startId);
}
@凌驾
公共IBinder onBind(意图arg0)
{
返回null;
}
}
您可以使用方法从其他类似组件的活动中停止服务。您可以使用方法从服务本身停止服务。据博士说

已启动的服务必须管理自己的生命周期。就是系统 除非必须恢复系统,否则不会停止或销毁服务 onStartCommand()之后,内存和服务将继续运行 返回。因此,服务必须通过调用stopSelf()或 另一个组件可以通过调用stopService()来停止它


要了解更多有关服务的信息,请参见

您是从活动开始服务的吗?请帮助我,我是新来的Android请参见我的答案。。在编写服务代码之前,请阅读并理解我提供的链接,然后我应该在调用服务类的活动类中或仅在服务类中写入service.stopService&我应该在哪个方法中写入此内容如果您使用startservice(intent)启动服务,然后使用stopService(intent)停止服务。使用相同的意图停止服务我尝试在“开始服务”下面写stopService(),但当我在emulator&check running应用程序中运行时,它不会显示我的ruuning应用程序..然后我在oncreate of service中写stopself(),然后qalso它不会在seting/apps/running@stinePike中显示我的应用程序ruuning我尝试了它。实际上,我制作了一个墙纸在一段时间后改变的应用程序。当我启动服务时,它开始改变&当我停止服务时,它停止服务,但墙纸在服务停止后改变。我怎样才能停止更换壁纸