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

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

最佳答案

BoundService在客户端-服务器体系结构中充当服务器。(您可能知道这部分)。
服务器始终将某些服务导出到客户端。该服务可能是一个公共方法。(例如“ public void reverse(String)”)。
getService()函数用于返回LocalService的实例,以便客户端可以通过该实例访问该BoundService的公共方法。


以下是来自官方Android文档的示例程序

`公共类LocalService扩展Service {
    //给客户的活页夹
    私有最终IBinder mBinder = new LocalBinder();
    //随机数生成器
    私有最终的随机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);
}


}
`

公共类BindingActivity扩展了Activity {
    LocalService mService;
    布尔值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;
    }
};


}`

让我们更仔细地看。

服务器部分。

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

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

上面的代码是服务器导出的实际功能或服务。

客户端部分。

LocalBinder binder = (LocalBinder) service; mService = binder.getService(); mBound = true;

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

然后,

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

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

08-16 22:03