Android: stopService in another Activity

2020-02-14 03:57发布

How can I stop my Service in another Activity?

I start the service in my summaryActivity

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

And start my statusActivity from my summaryActivity

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.

5条回答
啃猪蹄的小仙女
2楼-- · 2020-02-14 04:14

Just call the stopService() Method

Intent intent = new Intent(this,SocketService.class);
stopService(intent);
查看更多
我欲成王,谁敢阻挡
3楼-- · 2020-02-14 04:30

You should call Activity(ContextWrapper)#stopService:

stopService(new Intent(SummaryActivity.this, SocketService.class));
查看更多
Animai°情兽
4楼-- · 2020-02-14 04:30

just call stopService in summaryActivity

查看更多
Juvenile、少年°
5楼-- · 2020-02-14 04:37

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.

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.

Make sure you have unbound the service from all binding activities before attempting to stop it. As the Android docs explain, you cannot stop a Service that is currently bound to an activity.

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.

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.

查看更多
仙女界的扛把子
6楼-- · 2020-02-14 04:38

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))
查看更多
登录 后发表回答