Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/184.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 6.0中正确请求位置权限?_Android_Permissions_Location_Android 6.0 Marshmallow - Fatal编程技术网

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

如何在Android 6.0中正确请求位置权限?,android,permissions,location,android-6.0-marshmallow,Android,Permissions,Location,Android 6.0 Marshmallow,我的问题:为什么这段代码没有请求权限,我需要做什么才能使它请求权限 我直接从Android文档中复制了我的代码。它不起作用。我已经仔细阅读了十几个类似的堆栈溢出问题。其中一些似乎引用了我正在使用的代码作为正确答案。我看不出为什么他们的有效而我的无效。其他的看起来方向完全不同,但我不想遵循这些,因为它们与我读过的文档不匹配 我的舱单: <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://s

我的问题:为什么这段代码没有请求权限,我需要做什么才能使它请求权限

我直接从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>

当我运行这段代码时,我的toast消息会发出,告诉我我正在等待许可,并且我没有许可,但是从来没有打开请求许可的对话框。如果我还没有权限,我希望打开一个请求权限的对话框。

@sanjeev-谢谢,但我也尝试过。
@sanjeev-谢谢,但我也尝试过。
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.
        }
    }
}