Android 如何从调用活动类调用服务类的stopservice()方法

Android 如何从调用活动类调用服务类的stopservice()方法,android,Android,我试图从活动中调用我的服务类的stopService()方法。 但是我不知道如何从我的活动类访问stopservice方法。 我有下面的代码,但它不工作 这是主屏幕课程: 任何建议都值得高度重视。 谢谢和问候 Mintu当您取消选中复选框时,它看起来应该停止服务。日志中是否有异常?返回一个布尔值,指示它是否能够停止服务 如果您有意启动服务,那么您可能希望扩展而不是服务。当该类没有更多工作要做时,它将自行停止该服务 自动服务 class AutoService extends IntentServ

我试图从活动中调用我的服务类的stopService()方法。 但是我不知道如何从我的活动类访问stopservice方法。 我有下面的代码,但它不工作

这是主屏幕课程:

任何建议都值得高度重视。 谢谢和问候
Mintu

当您取消选中复选框时,它看起来应该停止服务。日志中是否有异常?返回一个布尔值,指示它是否能够停止服务

如果您有意启动服务,那么您可能希望扩展而不是服务。当该类没有更多工作要做时,它将自行停止该服务

自动服务

class AutoService extends IntentService {
     private static final String TAG = "AutoService";
     private Timer timer;    
     private TimerTask task;

     public onCreate() {
          timer = new Timer();
          timer = new TimerTask() {
            public void run() 
            {
                System.out.println("done");
            }
          }
     }

     protected void onHandleIntent(Intent i) {
        Log.d(TAG, "onHandleIntent");     

        int delay = 5000; // delay for 5 sec.
        int period = 5000; // repeat every sec.


        timer.scheduleAtFixedRate(timerTask, delay, period);
     }

     public boolean stopService(Intent name) {
        // TODO Auto-generated method stub
        timer.cancel();
        task.cancel();
        return super.stopService(name);
    }

}

实际上,我使用的代码与上面的代码基本相同。我在清单中的服务注册如下

<service android:name=".service.MyService" android:enabled="true">
            <intent-filter android:label="@string/menuItemStartService" >
                <action android:name="it.unibz.bluedroid.bluetooth.service.MY_SERVICE"/>
            </intent-filter>
        </service>
根据相应的活动,我称之为

startService(new Intent(MyService.MY_SERVICE));
并停止它与

stopService(new Intent(MyService.MY_SERVICE));
它工作得很好。尝试检查您的配置,如果没有发现任何异常,请尝试调试stopService get是否正确调用。

@Juri


如果您为您的服务添加了IntentFilter,您是说您想将您的服务公开给其他应用程序,那么它可能会被其他应用程序意外停止。

事实上,要停止服务,我们必须使用方法
stopService()
,您的做法是正确的:

启动服务:

  Intent myService = new Intent(MainActivity.this, BackgroundSoundService.class);
  startService(myService);
  Intent myService = new Intent(MainActivity.this, BackgroundSoundService.class);
  stopService(myService);
停止服务:

  Intent myService = new Intent(MainActivity.this, BackgroundSoundService.class);
  startService(myService);
  Intent myService = new Intent(MainActivity.this, BackgroundSoundService.class);
  stopService(myService);
如果调用
stopService()
,则调用服务中的方法
onDestroy()
不是
stopService()
方法
):

您必须实现
onDestroy()
方法


以下是一个示例,包括如何启动/停止服务。

在Kotlin中,您可以执行此操作

服务:

class MyService : Service() {
    init {
        instance = this
    }

    companion object {
        lateinit var instance: MyService

        fun terminateService() {
            instance.stopSelf()
        }
    }
}
在您的活动中(或应用程序中的任何位置):


注意:如果您有任何在Android状态栏中显示通知的未决意图,您可能也希望终止该通知。

related afaik现在以这种方式启动和停止服务存在安全风险
  Intent myService = new Intent(MainActivity.this, BackgroundSoundService.class);
  stopService(myService);
  @Override
    public void onDestroy() {
      timer.cancel();
      task.cancel();    
        Log.i(TAG, "onCreate() , service stopped...");
    }
class MyService : Service() {
    init {
        instance = this
    }

    companion object {
        lateinit var instance: MyService

        fun terminateService() {
            instance.stopSelf()
        }
    }
}
btn_terminate_service.setOnClickListener {
    MyService.terminateService()
}