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

时间:2017-12-07 07:45:07

标签: java android permissions android-6.0-marshmallow

当我在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)));
                    }

                });
      }

2 个答案:

答案 0 :(得分:1)

我认为它会对你有用Ask for permission at runtime

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);



}

答案 1 :(得分:0)

你可以这样做,

在清单中:

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

在您需要此权限请求的活动中:     final int 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)));
}