• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

Android:参数从活动传递把服务

用户头像
it1352
帮助1

问题说明

我通过这种方式绑定到服务:

I'm binding to Service by this way:

活动类:

ListenLocationService mService;
@Override
public void onCreate(Bundle savedInstanceState) {
        ...
        Intent intent = new Intent(this, ListenLocationService.class);
        intent.putExtra("From", "Main");
        bindService(intent, mConnection, Context.BIND_AUTO_CREATE);
        ...
}

private ServiceConnection mConnection = new ServiceConnection() {

        public void onServiceConnected(ComponentName className,
                IBinder service) {
            LocalBinder binder = (LocalBinder) service;
            mService = binder.getService();         
        }

        public void onServiceDisconnected(ComponentName arg0) {
        }
    };

这是我的服务onBind方法:

@Override
public IBinder onBind(Intent intent) {
    Bundle extras = intent.getExtras(); 
    if(extras == null)
        Log.d("Service","null");
    else
    {
        Log.d("Service","not null");
        String from = (String) extras.get("From");
        if(from.equalsIgnoreCase("Main"))
            StartListenLocation();
    }
    return mBinder;
}

所以我在 LogCat 中有null" - 尽管我在 bindService

So I have "null" in LogCat - bundle is null in spite of I made intent.putExtra before bindService

总的来说,服务工作正常.但是我只需要从应用程序的主要活动中调用 StartListenLocation(); (我决定通过发送一个标志来做到这一点).

In general Service works fine. However I need to call StartListenLocation(); only from main activity of application (I decide to do this by sending a flag).

我如何向服务发送数据?或者也许有另一种方法来检查启动了哪个活动 onBind?

How can I send a data to service?Or maybe there's another way to check what activity launched onBind?

正确答案

#1

1 创建一个接口,声明您要从 Activity 调用的所有方法签名:

1 Create a interface that declare all method signature that you want to call from a Activity:

public interface ILocationService {
  public void StartListenLocation(Location location);
}

2 让你的 binder 实现 ILocaionService 并定义实际的方法体:

2 Make your binder implements ILocaionService and define the actual method body:

public class MyBinder extends Binder implements ILocationService {
  ... ...

  public void StartListenLocation(Location location) {
    // implement your method properly
  }

  ... ...
}

3 在绑定到服务的活动中,通过接口引用您的绑定器:

3 In activity that bind to the service, reference your binder by the interface:

... ...

ILocationService mService; // communication is handled via Binder not the actual service class.

private ServiceConnection mConnection = new ServiceConnection() {

  public void onServiceConnected(ComponentName className, IBinder service) {
    mService = (ILocationService) service;     
  }

  ... ...
};

... ...

// At some point if you need call service method with parameter:
Location location = new Location();
mService.StartListenLocation(location);

所有通信(即对您的服务的方法调用)都应该通过在 ServiceConnection.onServiceConnected() 中初始化和返回的 binder 类来处理和执行,而不是实际的服务类(binder.getService() 是不必要的).这就是设计用于 API 中的绑定服务通信的方式.

All communications (i.e. method call to your Service) should be handled and performed via the binder class initialize and return in ServiceConnection.onServiceConnected(), not the actual service class (binder.getService() is unnecessary). This is how bind service communication designed to work in the API.

注意 bindService() 是一个异步调用.在您调用 bindService() 之后和在 ServiceConnection.onServiceConnected() 回调被系统参与之前会有一个延迟.所以最好在 ServiceConnection.onServiceConnected() 方法中初始化 mService 之后立即执行 service 方法.

Note that bindService() is an asynchronous call. There will be a lag after you call bindService() and before ServiceConnection.onServiceConnected() callback get involved by system. So the best place to perform service method is immediately after mService get initialized in ServiceConnection.onServiceConnected() method.

希望这会有所帮助.

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /reply/detail/tanhcfjecc
系列文章
更多 icon
同类精品
更多 icon
继续加载