如何在我的代码中添加 Marshmallow 权限以共享位置?

How to add Marshmallow permission in my code to share a location?

此代码 运行 4.4.2 当我 运行 此 Marshmallow 崩溃时未共享位置。

shar_btn = (Button) findViewById(R.id.shrbtn);

    shar_btn.setOnClickListener(
            new View.OnClickListener() {
                @Override
                public void onClick(View v) {



                        link = formatLocation(getLocation(), getResources().getStringArray(R.array.link_templates)[0]);
                        Intent intent = new Intent();
                        intent.setAction(Intent.ACTION_SEND);
                        intent.putExtra(Intent.EXTRA_TEXT, link);
                        intent.setType("text/plain");
                        startActivity(Intent.createChooser(intent, getString(R.string.share_location_via)));
                    }

                });
      }

我觉得对你有帮助

if (Build.VERSION.SDK_INT>= Build.VERSION_CODES.M){
        if (ActivityCompat.checkSelfPermission(AboutProduct.this, Manifest.permission.ACCESS_FINE_LOCATION)
                != PackageManager.PERMISSION_GRANTED){
            requestPermissions(new String[]{Manifest.permission.ACCESS_FINE_LOCATION},1);

            return;
        }
// write your code here 
link = formatLocation(getLocation(), getResources().getStringArray(R.array.link_templates)[0]);
                    Intent intent = new Intent();
                    intent.setAction(Intent.ACTION_SEND);
                    intent.putExtra(Intent.EXTRA_TEXT, link);
                    intent.setType("text/plain");
                    startActivity(Intent.createChooser(intent, getString(R.string.share_location_via)));

    }
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
    super.onRequestPermissionsResult(requestCode, permissions, grantResults);



}

你可以这样做,

在清单中:

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

在 Activity 您想要此权限请求的位置: 最终整数 REQUEST_ID_PERMISSIONS = 1;

shar_btn = (Button) findViewById(R.id.shrbtn);

shar_btn.setOnClickListener(
        new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (checkAndRequestPermissions()) {
                    // call comman function that you want to execute
                    shareLocation();
                }
            }

        });

然后对于 checkAndRequestPermissions() 函数,

/**
 * Check the permission for the ACCESS_FINE_LOCATION
 * if already accepted the permission then pass true
 * else ask permission for the ACCESS_FINE_LOCATION
 *
 * @return
 */
private boolean checkAndRequestPermissions() {
    int externalStoragePermission = ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION);

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

    if (!listPermissionsNeeded.isEmpty()) {
        ActivityCompat.requestPermissions(this, listPermissionsNeeded.toArray(new String[listPermissionsNeeded.size()]), REQUEST_ID_PERMISSIONS);
        return false;
    }
    return true;
}

如果用户接受一次,那么您的应用程序将记住它,您将不再需要发送此权限对话框。请注意,如果用户决定,他可以稍后将其禁用。 然后在请求位置之前,您必须测试权限是否仍然被授予:

public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
switch (requestCode) {
    case REQUEST_ID_PERMISSIONS: {
        // If request is cancelled, the result arrays are empty.
        if (grantResults.length > 0
        && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
            shareLocation();
        } 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
}
}

你的 ShareLocation() 喜欢,

private void shareLocation(){
    link = formatLocation(getLocation(), getResources().getStringArray(R.array.link_templates)[0]);
    Intent intent = new Intent();
    intent.setAction(Intent.ACTION_SEND);
    intent.putExtra(Intent.EXTRA_TEXT, link);
    intent.setType("text/plain");
    startActivity(Intent.createChooser(intent, getString(R.string.share_location_via)));
}