-1

This code run on 4.4.2 when i run this on Marshmallow crash not shared location.

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

                });
      }
Phantômaxx
  • 37,352
  • 21
  • 80
  • 110
  • you need permission at runtime. – Ratilal Chopda Dec 07 '17 at 07:47
  • Apps that have `targetSdkVersion` above 22 (so Marshmallow and newer) need to ask for some permissions during runtime. Read the [relevant Android docs](https://developer.android.com/training/permissions/requesting.html). Just make sure not to reduce the `targetSdkVersion` to 22 because then users will need to accept permissions when they download the app and you won't have to make any other changes. That would be the quick, lazy and wrong way to do it. – TimSim Dec 07 '17 at 07:55

2 Answers2

1

I think it will help you 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);



}
Teja
  • 757
  • 6
  • 21
0

You can do it like this,

In Manifest:

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

In Activity where you want this permission request: 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();
                }
            }

        });

Then for the checkAndRequestPermissions() function,

/**
 * 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;
}

If the user accepts it once, then your app will remember it and you won't need to send this Permission Dialog anymore. Note that the user could disable it later if he decided to. Then before requesting the location, you would have to test if the permission is still granted :

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

And your ShareLocation() like,

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