Java Android:在另一个活动中停止服务

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9665256/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-16 05:50:40  来源:igfitidea点击:

Android: stopService in another Activity

javaandroidserviceandroid-intent

提问by user1263776

How can I stop my Service in another Activity?

如何在另一个活动中停止我的服务?

I start the service in my summaryActivity

我在我的 summaryActivity 中启动服务

SocketServiceIntent = new Intent(this, SocketService.class);
SocketServiceIntent.putExtra("MessageParcelable", mp);
startService(SocketServiceIntent);

And start my statusActivity from my summaryActivity

并从我的 summaryActivity 开始我的 statusActivity

Intent intent = new Intent(SummaryActivity.this,
                StatusActivity.class);
intent.putExtra("MessageParcelable", mp);
startActivity(intent);

My problem is that I don't know how I can give my Statusactivity the SocketServiceIntent.

我的问题是我不知道如何为我的 Statusactivity 提供 SocketServiceIntent。

回答by MByD

You should call Activity(ContextWrapper)#stopService:

你应该打电话Activity(ContextWrapper)#stopService

stopService(new Intent(SummaryActivity.this, SocketService.class));

回答by seanhodges

You haven't explained how you're currently trying to use stopService() and what error you are getting. Expand your question a bit and you might get more helpful responses.

您还没有解释您目前如何尝试使用 stopService() 以及您遇到了什么错误。稍微扩展您的问题,您可能会得到更多有用的答复。

You need to call this from your activity:

您需要从您的活动中调用它:

stopService(new Intent(SummaryActivity.this, SocketService.class));

Replace "SummaryActivity" with the name of the Activity class you are stopping the service from.

将“SummaryActivity”替换为您停止服务的活动类的名称。

Make sure you have unboundthe service from allbinding activities before attempting to stop it. As the Android docsexplain, you cannot stop a Service that is currently bound to an activity.

在尝试停止服务之前,请确保您已将服务从所有绑定活动中解除绑定。正如Android 文档所解释的,您无法停止当前绑定到活动的服务。

As a design tip: It's often better to call stopSelf()from within the running Service rather than using stopService()directly. You can add a shutdown()method into your AIDL interface, which allows an Activity to request a stopSelf()be called. This encapsulates the stopping logic and gives you the opportunity to control the state of your Service when it is stopped, similar to how you would handle a Thread.

作为设计提示:stopSelf()从正在运行的服务内部调用通常比stopService()直接使用更好。您可以shutdown()在 AIDL 接口中添加一个方法,该方法允许 Activity 请求stopSelf()调用。这封装了停止逻辑,并让您有机会在停止时控制服务的状态,类似于处理Thread.

For example:

例如:

public MyService extends IntentService {

    private boolean shutdown = false;

    public void doSomeLengthyTask() {
        // This can finish, and the Service will not shutdown before 
        // getResult() is called...
        ...
    }

    public Result getResult() {
        Result result = processResult();

        // We only stop the service when we're ready
        if (shutdown) {
            stopSelf();
        }

        return result;
    }

    // This method is exposed via the AIDL interface
    public void shutdown() {
        shutdown = true;
    }

}

This is particularly relevant since your Intent name suggests you might be dealing with network sockets. You will want to ensure you have properly closed your socket connections before your Service is stopped.

这尤其重要,因为您的意图名称表明您可能正在处理网络套接字。您需要确保在服务停止之前正确关闭了套接字连接。

回答by Pedro Paiva Portugal

just call stopService in summaryActivity

只需在 summaryActivity 中调用 stopService

回答by kelvincer

Start the service:

启动服务:

// Java
Intent SocketServiceIntent = new Intent(this, SocketService.class);
SocketServiceIntent.putExtra("MessageParcelable", mp);
startService(SocketServiceIntent);

//Kotlin
startService(Intent(this, SocketService::class.java))

Stop the service in any activity:

在任何活动中停止服务:

//Java
stopService(new Intent(this, SocketService.class))

//Kotlin
stopService(Intent(this, SocketService::class.java))

回答by Pradeep Goyal

Just call the stopService()Method

只需调用stopService()方法

Intent intent = new Intent(this,SocketService.class);
stopService(intent);