2 pipi0 PIPI0 于 2016.01.24 07:16 提问

请问在服务中是否无法关闭服务呢???
public class MyService extends Service {

    private MyReceiver receiver;


    private class MyReceiver extends BroadcastReceiver {
        public void onReceive(Context context, Intent intent) {
            onDestroy();
        }
    }  

    public void onCreate() {
        super.onCreate();
        Toast.makeText(MyService.this, "服务已启动", Toast.LENGTH_SHORT).show();
    }

    public int onStartCommand(Intent intent, int flags, int startId) {
        receiver=new MyReceiver();
        IntentFilter filter=new IntentFilter();
        filter.addAction("net.s5c.Changelink.wifiOff");
        registerReceiver(receiver, filter);
        flags=START_STICKY;
        return super.onStartCommand(intent, flags, startId);
    }



 public void onDestroy() {
        Toast.makeText(this, "服务已关闭", Toast.LENGTH_LONG).show();//可以打印出这句,但看后台还是无法关闭服务!
        super.onDestroy();
    }

    public IBinder onBind(Intent intent) {
        return null;
    }

}

收到广播后能看到输出的onDestroy()的内容,但看正在运行中服务还是继续运行着,这是怎么回事呢?

2个回答

PIPI0
PIPI0   2016.01.24 22:15
已采纳

没有任何作用看了半天也没找到我需要的!

devmiao
devmiao   Ds   Rxr 2016.01.24 07:24
Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!