внутренний класс: не общедоступный и недоступный из внешнего пакета - PullRequest
0 голосов
/ 19 мая 2019

У меня есть GpsService в пакете (packageShared), и мне нужно получить к нему доступ из другого пакета (packageClient). Я получаю "getService () не является общедоступным в packageShared.GpsService.Localbinder '. Доступ из внешнего пакета невозможен." в следующей строке mService = (GpsService) binder.getService (); в Android Studio.

У меня есть другой класс внутри пакета. Отлично работает, используя тот же код.

Вот код на упаковке клиента:

    // Monitors the state of the connection to the service.
private final ServiceConnection mServiceConnection = new ServiceConnection() {

    @Override
    public void onServiceConnected(ComponentName name, IBinder service) {
        GpsService.LocalBinder binder = (GpsService.LocalBinder) service;
        mService = (GpsService) binder.getService();
        mBound = true;

    }

    @Override
    public void onServiceDisconnected(ComponentName name) {
        mService = null;
        mBound = false;
    }
};

Вот код на упаковке Shared:

public class GpsService extends Service {


    public GpsService() {
    }

    other methods here...

    /**
     * Class used for the client Binder.  Since this service runs in the same process as its
     * clients, we don't need to deal with IPC.
     */
    public class LocalBinder extends Binder {
        GpsService getService() {
            return GpsService.this;
        }
    }
}

Заранее спасибо.

1 Ответ

2 голосов
/ 19 мая 2019

Это потому, что ваш getService() метод является видимым для пакета.Вам нужно сделать это public, чтобы получить доступ к нему из других пакетов:

public class LocalBinder extends Binder {
    public GpsService getService() {   //<-- The public keyword is added
        return GpsService.this;
    }
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...