Java 如何在我的代码中添加棉花糖权限以共享位置?
当我在棉花糖上运行此代码时,此代码在Java 如何在我的代码中添加棉花糖权限以共享位置?,java,android,permissions,android-6.0-marshmallow,Java,Android,Permissions,Android 6.0 Marshmallow,当我在棉花糖上运行此代码时,此代码在4.4.2上运行 shar_btn = (Button) findViewById(R.id.shrbtn); shar_btn.setOnClickListener( new View.OnClickListener() { @Override public void onClick(View v) { li
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)));
}
});
}
我想这会对你有帮助
你可以这样做 在舱单中:
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
然后对于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;
}
你的ShareLocation()就像
您需要在运行时获得权限。具有22以上
targetSdkVersion
的应用程序(如棉花糖和更新版本)需要在运行时请求一些权限。读这本书。只需确保不要将targetSdkVersion
减少到22,因为这样用户在下载应用程序时需要接受权限,而您不必进行任何其他更改。这将是一个快速、懒惰和错误的方法。非常感谢你的工作。好问题解决了亲爱的
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();
}
}
});
/**
* 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
}
}
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)));
}