Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/203.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
Android 使用服务或绑定对象在服务内设置变量_Android_Android Service_Android Binder - Fatal编程技术网

Android 使用服务或绑定对象在服务内设置变量

Android 使用服务或绑定对象在服务内设置变量,android,android-service,android-binder,Android,Android Service,Android Binder,我可以使用binder对象或服务类对象本身设置变量int[]。代码位于公共类MyActivity extends Activity中。哪种方法可行,两种方法都有效: private BackgroundService.BackgroundBinder mBoundBinder; // to get methods of nested binder class inside BackgroundService private BackgroundService mBoundServ

我可以使用binder对象或服务类对象本身设置变量int[]。代码位于公共类MyActivity extends Activity中。哪种方法可行,两种方法都有效:

    private BackgroundService.BackgroundBinder mBoundBinder; // to get methods of nested binder class inside BackgroundService
    private BackgroundService mBoundService; //service class object

    public void onServiceConnected(ComponentName className, IBinder binder) {
        mBoundService = ((BackgroundService.BackgroundBinder) binder).getService(); //google version
        mBoundBinder = ((BackgroundService.BackgroundBinder) binder);
    }
服务int[]数组可以通过两种方式设置:

1. mBoundService.setListeners = genArr(); //genArr() returns int[]
2. mBoundBinder.setListeners(genArr());
谷歌版本使用以下内部服务类。这将必要的
嵌套绑定类
缩减为一个返回服务对象本身的方法,因此可以直接访问其方法。很好

  public class LocalBinder extends Binder {
    LocalService getService() {
        // Return this instance of LocalService so clients can call public methods
        return LocalService.this;
    }
  }