【问题标题】:Android service method callAndroid服务方法调用
【发布时间】:2016-05-15 07:03:19
【问题描述】:

我想在同一个应用的服务 2 中调用服务 1 的方法。 例如服务 1 中有 Method1。

public void Method1(){
....}

在服务 2 中,我的代码将是...

Service1 serviceOne_object=new Service1();
service1_object.Method1();

但这不起作用。

【问题讨论】:

标签: android


【解决方案1】:

这是行不通的。您可以使用标准的 Android Intent 机制在服务之间传递数据。

服务1:

Intent intent = new Intent(this, Service2.class);
intent.putExtra("call_some_method", true);
startService(intent);

服务2:

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    if(intent != null && intent.getBooleanExtra("call_some_method", false)) {
        Method1();
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-28
    • 2017-01-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-01
    • 1970-01-01
    相关资源
    最近更新 更多