Невозможно получить координаты местоположения GPS с помощью службы с провайдером местоположения SecurityException «gps», требуется разрешение ACCESS_FINE_LOCATION - PullRequest
0 голосов
/ 31 августа 2018

Это мой сервис GPS, который я использую для получения координат GPS в фоновом режиме. Я вошел в различные области кода, чтобы увидеть, какие биты вызываются, а какие нет. Этот сервис вызывается нажатием кнопки. Кажется, что ничего не зарегистрировано для местоположения, и основная проблема связана с обновлениями местоположения, которые ничего не возвращают.

public class SetGPS extends Service {

    private LocationListener listener;
    private LocationManager locationManager;

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

    @Override
    public void onCreate() {

        //this is logged successfully
        Log.i("LocationListener","is called");
        listener = new LocationListener() {

            @Override
            public void onLocationChanged(Location location) {
                Intent i = new Intent("location_update");
                i.putExtra("coordinates", location.getLongitude() + " " + location.getLatitude());
                sendBroadcast(i);
                String latitude = String.valueOf(location.getLatitude());
                //this is NOT logged successfully
                Log.i("coordinates SetGPS",latitude);
            }

            @Override
            public void onStatusChanged(String s, int i, Bundle bundle) {

            }

            @Override
            public void onProviderEnabled(String s) {

            }

            @Override
            public void onProviderDisabled(String s) {
                Intent i = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
                i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                startActivity(i);
                //this is NOT logged successfully
                Log.i("onProviderDisabled","is logged");
            }
        };

        locationManager = (LocationManager) getApplicationContext().getSystemService(Context.LOCATION_SERVICE);

        try {
            //does NOT successfully log
            Log.i("testing","testing");
            locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 500, 0, listener);

        }
        catch (SecurityException e){
            e.printStackTrace();
        }

    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if(locationManager != null){
            locationManager.removeUpdates(listener);
        }
    }
}

А вот и мой файл манифеста.

<?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="com.example.a_phi.nowswap">
        <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
        <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
        <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
        <uses-permission android:name="android.permission.INTERNET" />
        <uses-permission android:name="android.permission.VIBRATE" />
        <uses-permission android:name="android.permission.CAMERA" />

        <uses-feature android:name="android.hardware.camera" />
        <uses-feature android:name="android.hardware.camera.autofocus" />

        <uses-permission android:name="android.permission.NFC" />
        <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
        <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
        <uses-permission
            android:name="android.permission.WRITE_CONTACTS"
            android:required="true" />
        <uses-permission
            android:name="android.permission.READ_CONTACTS"
            android:required="true" />

        <meta-data
            android:name="firebase_crash_collection_enabled"
            android:value="false" />

        <uses-permission android:name="android.permission.READ_PHONE_STATE" />

        <!-- To auto-complete the email text field in the login form with the user's emails -->
        <uses-permission android:name="android.permission.GET_ACCOUNTS" />
        <uses-permission android:name="android.permission.READ_PROFILE" />
        <application
            android:allowBackup="true"
            android:icon="@mipmap/ic_launcher"
            android:label="@string/app_name"
            android:roundIcon="@mipmap/ic_launcher_round"
            android:supportsRtl="true"
            android:theme="@style/AppTheme">
            <service
                android:name=".HceService"
                android:exported="true"
                android:permission="android.permission.BIND_NFC_SERVICE">
                <intent-filter>
                    <action android:name="android.nfc.cardemulation.action.HOST_APDU_SERVICE" />
                </intent-filter>

                <meta-data
                    android:name="android.nfc.cardemulation.host_apdu_service"
                    android:resource="@xml/apduservice" />
            </service>
            <service android:name=".SetGPS"
                     android:permission="android.permission.ACCESS_FINE_LOCATION"/>
            <activity
                android:name=".LoginActivity"
                android:windowSoftInputMode="adjustPan">
                <intent-filter>
                    <action android:name="android.intent.action.MAIN" />

                    <category android:name="android.intent.category.LAUNCHER" />
                </intent-filter>
                <intent-filter>
                    <action android:name="android.nfc.action.NDEF_DISCOVERED" />

                    <category android:name="android.intent.category.DEFAULT" />

                    <data android:mimeType="text/plain" />
                    <data android:scheme="file" />

                    <action android:name="android.intent.action.VIEW" />

                    <category android:name="android.intent.category.CATEGORY_DEFAULT" />
                </intent-filter>
            </activity>
            <activity
                android:name=".Pilot"
                android:theme="@style/AppTheme.NoActionBar"
                android:windowSoftInputMode="adjustPan">
                <intent-filter>
                    <action android:name="android.nfc.action.NDEF_DISCOVERED" />

                    <category android:name="android.intent.category.DEFAULT" />

                    <data android:mimeType="text/plain" />
                    <data android:scheme="file" />

                    <action android:name="android.intent.action.VIEW" />

                    <category android:name="android.intent.category.CATEGORY_DEFAULT" />
                </intent-filter>
            </activity>
            <activity
                android:name=".personalHome"
                android:label="@string/title_activity_personal_home"
                android:theme="@style/AppTheme.NoActionBar"></activity>
            <activity
                android:name=".ProfessionalHome"
                android:label="professionalHome"
                android:theme="@style/AppTheme.NoActionBar">
                <intent-filter>
                    <action android:name="android.intent.action.VIEW" />

                    <category android:name="android.intent.category.CATEGORY_DEFAULT" />

                    <data android:mimeType="text/plain`" />
                    <data android:scheme="file" />
                </intent-filter>
            </activity>
            <activity
                android:name=".EditDetailsPersonal"
                android:label="editDetails"
                android:theme="@style/AppTheme.NoActionBar" />
            <activity
                android:name=".EditDetailsProfessional"
                android:label="editDetailsProfessional"
                android:theme="@style/AppTheme.NoActionBar" />
            <activity
                android:name=".receivedContact"
                android:label="@string/title_activity_received_contact"
                android:theme="@style/AppTheme.NoActionBar" />
            <activity
                android:name=".RegisterActivity"
                android:label="RegisterActivity"
                android:theme="@style/AppTheme.NoActionBar" />
            <activity
                android:name=".AddContact"
                android:label="@string/add_contact_button"
                android:theme="@style/Theme.AppCompat.Light" />
            <activity
                android:name=".DrawerActivity"
                android:label="@string/title_activity_drawer"
                android:theme="@style/Theme.AppCompat.DayNight.DarkActionBar"></activity>
        </application>

    </manifest>

Возвращает следующую трассировку стека.

<code>
08-31 11:44:47.710 1399-1411/? W/System.err: java.lang.SecurityException: "gps" location provider requires ACCESS_FINE_LOCATION permission.
        at com.android.server.LocationManagerService.checkResolutionLevelIsSufficientForProviderUse(LocationManagerService.java:1321)
        at com.android.server.LocationManagerService.getProviderProperties(LocationManagerService.java:2334)
        at android.location.ILocationManager$Stub.onTransact(ILocationManager.java:378)
        at com.android.server.HwLocationManagerService.onTransact(HwLocationManagerService.java:379)
08-31 11:44:47.711 1399-1411/? W/System.err:     at android.os.Binder.execTransact(Binder.java:565)
08-31 11:44:47.712 1399-2192/? W/System.err: java.lang.SecurityException: "network" location provider requires ACCESS_COARSE_LOCATION or ACCESS_FINE_LOCATION permission.
        at com.android.server.LocationManagerService.checkResolutionLevelIsSufficientForProviderUse(LocationManagerService.java:1324)
        at com.android.server.LocationManagerService.getProviderProperties(LocationManagerService.java:2334)
        at android.location.ILocationManager$Stub.onTransact(ILocationManager.java:378)
        at com.android.server.HwLocationManagerService.onTransact(HwLocationManagerService.java:379)
        at android.os.Binder.execTransact(Binder.java:565)</p>

<p>08-31 11:45:06.132 17192-17192/com.example.a_phi.nowswap W/System.err: java.lang.SecurityException: "gps" location provider requires ACCESS_FINE_LOCATION permission.
        at android.os.Parcel.readException(Parcel.java:1665)
        at android.os.Parcel.readException(Parcel.java:1618)
        at android.location.ILocationManager$Stub$Proxy.requestLocationUpdates(ILocationManager.java:614)
        at android.location.LocationManager.requestLocationUpdates(LocationManager.java:952)
        at android.location.LocationManager.requestLocationUpdates(LocationManager.java:536)
        at com.example.a_phi.nowswap.SetGPS.onCreate(SetGPS.java:72)
        at android.app.ActivityThread.handleCreateService(ActivityThread.java:3350)
        at android.app.ActivityThread.-wrap5(ActivityThread.java)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1670)
        at android.os.Handler.dispatchMessage(Handler.java:105)
        at android.os.Looper.loop(Looper.java:156)
        at android.app.ActivityThread.main(ActivityThread.java:6623)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:942)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:832)</p>

<p>

, который указывает, что есть проблема с locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 500, 0, listener);

...