我写了一个service,里面通过socket通信调用了stopSelf和system.exit(0),service就被终止了。
但是过了一会儿,我发现这个service自己又启动了。
请问各位大大是什么原因呢,又如何解决?service

解决方案 »

  1.   

    查看service启动的条件,估计是你的程序接到什么intent然后又启动了service
      

  2.   

    应该是返回 START_NOT_STICKY 非粘性的才会不自动重启
      

  3.   

    好久不做应用,你自己手动stop然后又重启了,应该是哪里又重新启动了!
      

  4.   

    好久不做应用,你自己手动stop然后又重启了,应该是哪里又重新启动了!
    呃,我是写在onCreate里了
      

  5.   

    public int onStartCommand(Intent intent, int flags, int startId) {
    // TODO Auto-generated method stub
    handler = new Handler() { // 新Handler对象
    @Override
    public void handleMessage(Message msg) { // 接受msg
    // TODO Auto-generated method stub
    if (msg.what == 12345) {
    tag = line;
    System.out.println("handle:" + tag);
    if (tag.equals("close")) {
    System.out.println("Service is stopped.");
    stopSelf(); // 关闭Service
    System.exit(0);
    }
    }
    }
    };
    return START_NOT_STICKY;
    }
    我想问下我写成这样了,后面的return还有用吗
      

  6.   

    这里加多一句试试 if (tag.equals("close"))
    {...
    stopService(intent);//关闭  Service  ;import android.content.Context; 
    ...
    }( ・ิω・ิ) 希望帮到您