我如何杀死这项服务: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,但是当此活动在日志中被销毁时,我发现此意图服务一直是钢铁加工
1 回答
POPMUISE
TA贡献1765条经验 获得超5个赞
尝试这个
使用 stopSelf();
一旦使用 stopSelf() 请求停止,系统会尽快销毁服务。
有意图地通过一些动作
Intent intent = new Intent(MainMenu.this, UsbService.class);
intent.setAction(Constants.ACTION.STOPTFOREGROUND_ACTION);
stopService(intent);
在您的服务 onStartCommand()
if(intent.getAction()==Constants.ACTION.STOPTFOREGROUND_ACTION){
stopForeground(true);
stopSelf();
}
public class Constants {
public interface ACTION {
String STOPFOREGROUND_ACTION = "com.package.packageName.action.stopforeground";
}
}
添加回答
举报
0/150
提交
取消