Android 在强制关闭应用程序时关闭后台服务

Android 在强制关闭应用程序时关闭后台服务,android,android-service,Android,Android Service,我正在通过蓝牙从arduino板接收一些数据。。数据接收和数据存储到文本文件是在一个名为BackgroundService的类中完成的,该类扩展了服务类 所以现在我想在我的android应用程序强制关闭时断开接收的数据,我该怎么做,我尝试了onStop(),stopService()方法,但它不起作用 停止服务法 @Override public boolean stopService(Intent name) { // TODO Auto-generated meth

我正在通过蓝牙从arduino板接收一些数据。。数据接收和数据存储到文本文件是在一个名为BackgroundService的类中完成的,该类扩展了服务类

所以现在我想在我的android应用程序强制关闭时断开接收的数据,我该怎么做,我尝试了onStop(),stopService()方法,但它不起作用

停止服务法

 @Override
    public boolean stopService(Intent name) {
        // TODO Auto-generated method stub
       Amarino.disconnect(getApplicationContext(), DEVICE_ADDRESS);
       unregisterReceiver(arduinoReceiver);
        return super.stopService(name);

    }

您可以使用中添加的
ontrimmory()


看看这个。Ver good链接,解释了应用程序如何工作以及它们如何在内存不足时关闭。

您是否已将活动绑定到服务?您是否试图通过设置强制停止应用程序?@Zohra否我不是通过设置强制停止应用程序ID您显式调用启动服务的应用程序中的某个位置的Finish()?不确定这是否可以保证。那么如何解决我的问题?有什么解决办法吗?
public abstract void onTrimMemory (int level)
Called when the operating system has determined that it is a good time for a process
to trim unneeded memory from its process. This will happen for example when it goes 
in the background and there is not enough memory to keep as many background 
processes running as desired.