Java android杀死服务

我如何杀死这项服务:


private void startService(Class<?> service, ServiceConnection serviceConnection, Bundle extras) {

    if (!UsbService.SERVICE_CONNECTED) {

        Intent startService = new Intent(this, service);

        if (extras != null && !extras.isEmpty()) {

            Set<String> keys = extras.keySet();

            for (String key : keys) {

                String extra = extras.getString(key);

                startService.putExtra(key, extra);

            }

        }

        startService(startService);

    }


    Intent bindingIntent = new Intent(this, service);

    bindService(bindingIntent, serviceConnection, Context.BIND_AUTO_CREATE);

}

我尝试这样做:


Intent intent = new Intent(MainMenu.this, UsbService.class);

stopService(intent);

但该服务一直有效。''


这是我的服务:


public class UsbService extends Service {


    public static final String ACTION_USB_READY = "pl.gps.connectivityservices.USB_READY";

    public static final String ACTION_USB_ATTACHED = "android.hardware.usb.action.USB_DEVICE_ATTACHED";

    public static final String ACTION_USB_DETACHED = "android.hardware.usb.action.USB_DEVICE_DETACHED";

    public static final String ACTION_USB_NOT_SUPPORTED = "pl.gps.usbservice.USB_NOT_SUPPORTED";

    public static final String ACTION_NO_USB = "pl..gps.usbservice.NO_USB";

    public static final String ACTION_USB_PERMISSION_GRANTED = "pl.gps.usbservice.USB_PERMISSION_GRANTED";


        }

    };



但是我的意图服务窃取正在起作用。当我销毁创建此服务的活动时,我尝试做 whis,但是当此活动在日志中被销毁时,我发现此意图服务一直是钢铁加工


哆啦的时光机
浏览 147回答 1
1回答

POPMUISE

尝试这个使用 stopSelf();一旦使用 stopSelf() 请求停止,系统会尽快销毁服务。有意图地通过一些动作Intent intent = new Intent(MainMenu.this, UsbService.class);intent.setAction(Constants.ACTION.STOPTFOREGROUND_ACTION);stopService(intent);在您的服务 onStartCommand()&nbsp;if(intent.getAction()==Constants.ACTION.STOPTFOREGROUND_ACTION){&nbsp; &nbsp; &nbsp;stopForeground(true);&nbsp; &nbsp; &nbsp;stopSelf();&nbsp; &nbsp;}&nbsp; public class Constants {&nbsp; public interface ACTION {&nbsp; &nbsp; &nbsp; String STOPFOREGROUND_ACTION = "com.package.packageName.action.stopforeground";&nbsp; }&nbsp; }
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java