开发者

Android Service did not stop

I have a main class with two buttons to start and stop the service.

public void onClick(View src) {
    switch (src.getId()) {
    case R.id.buttonStart:
        Log.d(TAG, "onClick: starting srvice");
        myService = new Intent(this, MyService.class);
        startService(myService);
        break;
    case R.id.buttonStop:
        Log.d(TAG, "onClick: stopping srvice");
        stopService(myService);
        break;
    }
}

Start Service intended to send current GPS coordinates to the serve开发者_如何学运维r. it starts perfectely but did not stop on button click.

In MYService.java i have this code onDestroy method.

public void onDestroy() {
    Toast.makeText(this, "My Service Stopped", Toast.LENGTH_LONG).show();
    Log.d(TAG, "onDestroy");
}

Please help how to kill this Service.


try this for stop services stopService(itnt_BackServices ); in this example itnt_BackServices is name of services. you cant stop services in services class but stop in any activity.


Try adding this to your service

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    this.startId = startId;
    return mStartMode;
}

@Override
public void onDestroy() {
    stopSelf(startId);
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜