我如何殺死這項服務(wù):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);但該服務(wù)一直有效。''這是我的服務(wù):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"; } };但是我的意圖服務(wù)竊取正在起作用。當(dāng)我銷毀創(chuàng)建此服務(wù)的活動時,我嘗試做 whis,但是當(dāng)此活動在日志中被銷毀時,我發(fā)現(xiàn)此意圖服務(wù)一直是鋼鐵加工
1 回答

POPMUISE
TA貢獻1765條經(jīng)驗 獲得超5個贊
嘗試這個
使用 stopSelf();
一旦使用 stopSelf() 請求停止,系統(tǒng)會盡快銷毀服務(wù)。
有意圖地通過一些動作
Intent intent = new Intent(MainMenu.this, UsbService.class);
intent.setAction(Constants.ACTION.STOPTFOREGROUND_ACTION);
stopService(intent);
在您的服務(wù) 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
提交
取消