Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/346.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Android:停止另一个活动中的服务_Java_Android_Service_Android Intent - Fatal编程技术网

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

Java Android:停止另一个活动中的服务,java,android,service,android-intent,Java,Android,Service,Android Intent,如何在其他活动中停止我的服务 我在总结活动中启动服务 SocketServiceIntent = new Intent(this, SocketService.class); SocketServiceIntent.putExtra("MessageParcelable", mp); startService(SocketServiceIntent); Intent intent = new Intent(SummaryActivity.this, Status

如何在其他活动中停止我的服务

我在总结活动中启动服务

SocketServiceIntent = new Intent(this, SocketService.class);
SocketServiceIntent.putExtra("MessageParcelable", mp);
startService(SocketServiceIntent);
Intent intent = new Intent(SummaryActivity.this,
                StatusActivity.class);
intent.putExtra("MessageParcelable", mp);
startActivity(intent);
并从我的summaryActivity开始我的statusActivity

SocketServiceIntent = new Intent(this, SocketService.class);
SocketServiceIntent.putExtra("MessageParcelable", mp);
startService(SocketServiceIntent);
Intent intent = new Intent(SummaryActivity.this,
                StatusActivity.class);
intent.putExtra("MessageParcelable", mp);
startActivity(intent);
我的问题是,我不知道如何为我的Statusactivity提供SocketServiceIntent。

您应该拨打:


您还没有解释当前如何尝试使用stopService()以及出现了什么错误。把你的问题扩大一点,你可能会得到更多有用的回答

您需要从活动中调用此选项:

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

//Kotlin
stopService(Intent(this, SocketService::class.java))
将“SummaryActivity”替换为要停止服务的活动类的名称

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

作为一个设计提示:通常最好从正在运行的服务中调用
stopSelf()
,而不是直接使用
stopService()
。您可以在AIDL界面中添加
shutdown()
方法,该方法允许活动请求调用
stopSelf()
。这封装了停止逻辑,并使您有机会在服务停止时控制其状态,与处理
线程的方式类似

例如:

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;
    }

}

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

只需在summaryActivity中调用stopService即可启动服务:

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

//Kotlin
startService(Intent(this, SocketService::class.java))
在任何活动中停止服务:

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

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

只需调用
stopService()
方法

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

感谢的可能重复,我在这里找到了一个带有服务框架的解决方案:它在我的StatusActivity中不起作用,因为“没有SummaryActivity类型的封闭实例可在范围内访问”。因此将其更改为
StatusActivity
。这只是一个例子,我做了,但不起作用。我需要与启动服务时使用的相同的意图对象。我不能做一个新的。我也试着在我的Parcelable课程中表达我的意图。我知道我必须使用stopService方法。