如何在Android 6.0中正确请求位置权限?

时间:2019-06-19 11:11:16

标签: android permissions location android-6.0-marshmallow

我的问题:为什么这段特定的代码不请求许可,我需要做些什么才能使它请求许可?

我直接从Android文档中复制了代码。它不起作用。我仔细研究了大概十二个类似的堆栈溢出问题。其中一些似乎引用了我正在用作正确答案的代码。我看不出他们的为什么运作,而我的却行不通。其他人似乎朝着完全不同的方向发展,但我不想遵循这些人,因为它们与我阅读的文档不匹配。

我的清单:

<?xml version="1.0" encoding="utf-8"?>
 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
     package="church.eastjordan.circle">

    <uses-sdk android:minSdkVersion="23"
        android:targetSdkVersion="23"
        android:maxSdkVersion="23" />

    <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">

        <uses-permission-sdk-23 
android:name="android.permission.ACCESS_FINE_LOCATION"/>

        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

    </application>

</manifest>

我的代码:

import android.Manifest;
import android.app.Activity;
import android.content.Context;
import android.content.pm.PackageManager;
import android.location.Location;
import android.location.LocationManager;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;


public class MainActivity extends AppCompatActivity {

    public static final int MY_PERMISSIONS_REQUEST_LOCATION = 99;
    String latitudeString;
    String longitudeString;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Toast.makeText(this, "Awaiting permission", Toast.LENGTH_SHORT).show();

        if (ContextCompat.checkSelfPermission(this,
            Manifest.permission.ACCESS_FINE_LOCATION)
            != PackageManager.PERMISSION_GRANTED) {

            Toast.makeText(this, "You don't have permission", Toast.LENGTH_SHORT).show();

            ActivityCompat.requestPermissions(this,
                    new String[]{Manifest.permission.ACCESS_FINE_LOCATION},
                    MY_PERMISSIONS_REQUEST_LOCATION);
            latitudeString = "No Permission";
            longitudeString = "No Permission";

        }else {

            Toast.makeText(this, "You have permission", Toast.LENGTH_SHORT).show();

        }

        UpdateText();


    }

    public void GetLocation(View view) {
        LocationManager lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
        Location location = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
        double longitude = location.getLongitude();
        double latitude = location.getLatitude();
        latitudeString = Double.toString(latitude);
        longitudeString = Double.toString(longitude);

        UpdateText();
    }

    public void UpdateText() {

        TextView latView = (TextView) findViewById(R.id.latitude);
        TextView longView = (TextView) findViewById(R.id.longitude);
        latView.setText( latitudeString);
        longView.setText(longitudeString);
    }

    @Override
    public void onRequestPermissionsResult(int requestCode,
                                       String[] permissions, int[] grantResults) {
        switch (requestCode) {
            case MY_PERMISSIONS_REQUEST_LOCATION: {
                // If request is cancelled, the result arrays are empty.
                if (grantResults.length > 0
                    && grantResults[0] == PackageManager.PERMISSION_GRANTED) {

                } else {
                    // permission denied, boo! Disable the
                    // functionality that depends on this permission.
                }
                return;
            }

            // other 'case' lines to check for other
            // permissions this app might request.
        }
    }
}

运行此代码时,我的烤面包消息将触发,告诉我我正在等待许可,但我没有许可,但是没有对话框打开请求许可。我希望对话框打开(如果我还没有的话)。

0 个答案:

没有答案