【问题标题】:How to add Marshmallow permission in my code to share a location? [duplicate]如何在我的代码中添加 Marshmallow 权限以共享位置? [复制]
【发布时间】:2017-12-07 07:45:07
【问题描述】:

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

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

                });
      }

【问题讨论】:

  • 您需要在运行时获得许可。
  • targetSdkVersion 大于 22 的应用程序(因此 Marshmallow 和更新版本)需要在运行时请求一些权限。阅读relevant Android docs。只需确保不要将targetSdkVersion 减少到 22,因为这样用户在下载应用程序时将需要接受权限,而您无需进行任何其他更改。那将是快速、懒惰和错误的做法。

标签: java android permissions android-6.0-marshmallow


【解决方案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);



}

【讨论】:

  • 非常感谢它的工作。
【解决方案2】:

你可以这样,

在清单中:

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

在您需要此权限请求的 Activity 中: 最终 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)));
}

【讨论】:

  • 好问题解决了亲爱的
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-09-23
  • 1970-01-01
  • 1970-01-01
  • 2016-09-17
  • 2018-02-15
  • 1970-01-01
相关资源
最近更新 更多