Android:在运行时获取设备位置

时间:2017-09-14 00:01:48

标签: android xml location android-permissions

我正在开发一个应用程序,我想要获取用户设备的lat,lng。我发现我需要Access_Fine_Location权限以及Access_Coarse_Location,我知道我应该在期间要求它们运行时以及将它们包含在我的AndroidManifest.xml中,但应用程序只询问我的权限ACCESS_COARSE_LOCATION,并忽略询问ACCESS_FINE_LOCATION。 这是我的代码

 private boolean checkAndRequestPermissions(){
    int permissionReadContacts = ContextCompat.checkSelfPermission(this, Manifest.permission.READ_CONTACTS);
    int locationPermission = ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION);
    int locationPermission2 =ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION);

    List<String> listPermissionsNeeded = new ArrayList<String>();
    if (locationPermission != PackageManager.PERMISSION_GRANTED) {
        listPermissionsNeeded.add(Manifest.permission.ACCESS_COARSE_LOCATION);
    }

    if (locationPermission2 != PackageManager.PERMISSION_GRANTED) {
        Log.i("addingPerm","addingPerm");
        listPermissionsNeeded.add(Manifest.permission.ACCESS_FINE_LOCATION);
    }

    if (permissionReadContacts != PackageManager.PERMISSION_GRANTED) {
        listPermissionsNeeded.add(Manifest.permission.READ_CONTACTS);
    }
    if (!listPermissionsNeeded.isEmpty()) {
        ActivityCompat.requestPermissions(this, listPermissionsNeeded.toArray(new String[listPermissionsNeeded.size()]),REQUEST_ID_MULTIPLE_PERMISSIONS);
        return false;
    }
    return true;

}

这里是回调:

@Override
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
    String TAG = "sonder.SplashActivity";
    Log.d(TAG, "Permission callback called-------");
    switch (requestCode) {
        case REQUEST_ID_MULTIPLE_PERMISSIONS: {

            Map<String, Integer> perms = new HashMap<String, Integer>();
            // Initialize the map with both permissions
            perms.put(Manifest.permission.ACCESS_COARSE_LOCATION, PackageManager.PERMISSION_GRANTED);
            perms.put(Manifest.permission.ACCESS_FINE_LOCATION, PackageManager.PERMISSION_GRANTED);
            perms.put(Manifest.permission.READ_CONTACTS, PackageManager.PERMISSION_GRANTED);

            // Fill with actual results from user
            if (grantResults.length > 0) {
                for (int i = 0; i < permissions.length; i++)
                    perms.put(permissions[i], grantResults[i]);
                // Check for both permissions

                if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
                    LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
                    LocationListener locationListener = new LocListener();
                    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 5000, 10, locationListener);
                    UserData.setLocation(LocListener.getLat() + "," + LocListener.getLon());
                    Log.i("aaaaaaaaaa", "aaaaaaaaaa");
                    Log.i("" + LocListener.getLat(), "" + LocListener.getLon());

                    if (perms.get(Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
                        Log.i("Ahmed", "Ahmed");
                        Log.d("Permissions", "location services permission granted");
                        FacebookSdk.sdkInitialize(getApplicationContext());
                        Thread myThread = new Thread() {
                            @Override
                            public void run() {
                                try {
                                    Log.i("May", "May");
                                    sleep(300);
                                    Intent intent = new Intent(getApplicationContext(), setup_activity.class);
                                    startActivity(intent);
                                    finish();
                                } catch (InterruptedException e) {
                                    e.printStackTrace();
                                }
                            }
                        };
                        myThread.start();
                    } else {
                        //If permissions are denied
                        Log.d(TAG, "Location permission not granted ask again");
                        Intent intent = new Intent(getApplicationContext(), setup_activity.class);
                        startActivity(intent);
                        finish();
                    }
                }
            }
        }
    }
}

这是我的AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>

<uses-permission android:name="android.permission.AUTHENTICATE_ACCOUNTS" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.USE_CREDENTIALS" />
<uses-permission android:name="android.permission.MANAGE_ACCOUNTS" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permissions.NETWORK_ACCESS" />
<uses-permission android:name="android.permission.READ_CONTACTS" />
<uses-permission android:name="android.permissions.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permissions.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_LOCATION_EXTRA_COMMANDS"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

<uses-feature android:name="android.hardware.location.gps"
    android:required="true"/>

<uses-feature android:name="android.hardware.location.network"
    android:required="true"/>

<uses-feature
    android:name="android.hardware.location"
    android:required="true" />

<application
    android:name=".utils.orientation_adj"
    android:allowBackup="true"
    android:icon="@drawable/icon"
    android:label="@string/app_name"
    android:roundIcon="@drawable/icon"
    android:theme="@style/AppTheme"
    android:largeHeap="true">
    <activity
        android:name=".home.splashscreen"
        android:theme="@style/AppTheme.NoActionBar">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

    <activity
        android:name=".home.main_activity"
        android:theme="@style/AppTheme.NoActionBar">
    </activity>
    <activity android:name=".drawer.drawer_preferences"></activity>
    <activity
        android:name=".setup.edit_profile_tags"
        android:theme="@style/DialogNoTitle"></activity>
    <activity
        android:name=".setup.setup_activity"
        android:theme="@style/AppTheme">
    </activity>
    <activity android:name=".setup.backend_test"></activity>
    <activity
        android:name=".drawer.drawer_view_profile"
        android:theme="@style/AppTheme.NoActionBar"></activity>

    <activity android:name=".drawer.drawer_settings"
        android:theme="@style/Switch"></activity>
    <activity android:name=".drawer.drawer_help"></activity>

    <activity android:name=".setup.not_available_in_your_country"
        android:theme="@style/AppTheme.NoActionBar">
        <!--Add intent filter here-->
    </activity>

    <activity android:name=".setup.readiness"
        android:theme="@style/AppTheme.NoActionBar">
    </activity>

    <activity android:name=".setup.locationPermission"
        android:theme="@style/AppTheme.NoActionBar">
    </activity>
    <service
        android:name=".utils.GenericAccountService"
        android:exported="false">
        <intent-filter>
            <action android:name="android.accounts.AccountAuthenticator" />
        </intent-filter>

        <meta-data
            android:name="android.accounts.AccountAuthenticator"
            android:resource="@xml/authenticator" />
    </service>

    <meta-data
        android:name="com.facebook.sdk.ApplicationId"
        android:value="@string/facebook_app_id" />
    <meta-data
        android:name="com.google.android.geo.API_KEY"
        android:value="@string/google_maps_key" />
    <meta-data
        android:name="com.google.android.gms.version"
        android:value="@integer/google_play_services_version" />
    <meta-data
        android:name="io.fabric.ApiKey"
        android:value="43a4edcd41e180a4beca5aae4040115cfc1b52fe" />
</application>

1 个答案:

答案 0 :(得分:1)

您的三个<uses-permission>元素的权限名称不正确。没有以android.permissions开头的Android SDK权限(请注意最后的s)。因此,例如,您需要将android.permissions.ACCESS_FINE_LOCATION更改为android.permission.ACCESS_FINE_LOCATION。如果<uses-permission>元素缺失或无效,则无法请求运行时权限。