gpt4 book ai didi

安卓本地绑定(bind)

转载 作者:行者123 更新时间:2023-11-29 17:14:12 25 4
gpt4 key购买 nike

这里为什么要使用getService方法?

public class LocalBinder extends Binder {
LocalService getService() {
return LocalService.this;
}
}

最佳答案

  • BoundService 充当客户端-服务器架构中的服务器。(您可能知道这部分内容)。
  • 服务器总是向客户端输出一些服务。该服务可能是公共(public)方法。(如“public void reverse(String)”)。
  • getService() 函数用于返回 LocalService 的实例,以便客户端可以通过该实例访问该 BoundService 的公共(public)方法。

下面是Android官方文档中的示例程序

public class LocalService extends Service {
// Binder given to clients
private final IBinder mBinder = new LocalBinder();
// Random number generator
private final Random mGenerator = new Random();

/**
* Class used for the client Binder. Because we know this service always
* runs in the same process as its clients, we don't need to deal with IPC.
*/
public class LocalBinder extends Binder {
LocalService getService() {
// Return this instance of LocalService so clients can call public methods
return LocalService.this;
}
}

@Override
public IBinder onBind(Intent intent) {
return mBinder;
}

/** method for clients */
public int getRandomNumber() {
return mGenerator.nextInt(100);
}
}

.

public class BindingActivity extends Activity {
LocalService mService;
boolean mBound = false;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}

@Override
protected void onStart() {
super.onStart();
// Bind to LocalService
Intent intent = new Intent(this, LocalService.class);
bindService(intent, mConnection, Context.BIND_AUTO_CREATE);
}

@Override
protected void onStop() {
super.onStop();
// Unbind from the service
if (mBound) {
unbindService(mConnection);
mBound = false;
}
}

/** Called when a button is clicked (the button in the layout file attaches to
* this method with the android:onClick attribute) */
public void onButtonClick(View v) {
if (mBound) {
// Call a method from the LocalService.
// However, if this call were something that might hang, then this request should
// occur in a separate thread to avoid slowing down the activity performance.
int num = mService.getRandomNumber();
Toast.makeText(this, "number: " + num, Toast.LENGTH_SHORT).show();
}
}

/** Defines callbacks for service binding, passed to bindService() */
private ServiceConnection mConnection = new ServiceConnection() {

@Override
public void onServiceConnected(ComponentName className,
IBinder service) {
// We've bound to LocalService, cast the IBinder and get LocalService instance
LocalBinder binder = (LocalBinder) service;
mService = binder.getService();
mBound = true;
}

@Override
public void onServiceDisconnected(ComponentName arg0) {
mBound = false;
}
};
}

让我们仔细看看。

服务器部分。

公共(public)类 LocalService 是向客户端 Activity 或服务导出(或提供)某些功能的服务。客户端导入(或使用)功能。

public int getRandomNumber() { 返回 mGenerator.nextInt(100); }

以上代码是服务器输出的实际功能或服务。

客户端部分。

LocalBinder binder = (LocalBinder) 服务; mService = binder.getService(); mBound = true;

这里的 binder.getService() 用于获取 LocalBinder 类的运行(或当前)实例(或对象)。

然后,

int num = mService.getRandomNumber(); Toast.makeText(this, "number: "+num,Toast.LENGTH_SHORT).show();

mService.getRandomNumber() 用于访问公共(public)方法。

关于安卓本地绑定(bind),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39697424/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com