以前下面的代码在The Marshmellow上工作正常,但昨天我第一次看到检测到屏幕覆盖,而我获得了运行时位置权限,而在onPause()期间{{1}在那次崩溃之后,请帮助我,我甚至可以按照这么多的答案,但没有一个能解决我的问题。
代码: OnCreate:
W/Activity: Can reqeust only one set of permissions at a time
onStart():
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (!isGooglePlayServicesAvailable()) {
Toast.makeText(getApplicationContext(),"Install Google Play Service",Toast.LENGTH_SHORT).show();
finish();
}
Boolean bool = checkPermission();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
Log.e("overlayPro", String.valueOf(Settings.canDrawOverlays(this)));
}
setContentView(R.layout.activity_find_service_providers);
linearLayout =(LinearLayout)findViewById(R.id.linearlayout);
Intent intent = getIntent();
if(null!=intent.getExtras()) {
//get Intent value
}
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(LocationServices.API)
.build();
}
checkPermission():
@Override
protected void onStart() {
super.onStart();
mGoogleApiClient.connect();
if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (checkPermission()) {
mapFrag.getMapAsync(this);
if (!Utility.isLocationEnabled(getApplicationContext())) {
setingsrequest();
}
} else {
checkLocationPermission();
}
}
else if (!Utility.isLocationEnabled(getApplicationContext())){
mapFrag.getMapAsync(this);
setingsrequest();
}
else {
mapFrag.getMapAsync(this);
}
}
settingRequest():
private boolean checkPermission() {
return ( ContextCompat.checkSelfPermission(getApplicationContext(),Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED);
}
checkLocationPermission()
public void setingsrequest()
{
LocationRequest locationRequest = LocationRequest.create();
locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
locationRequest.setInterval(30 * 1000);
locationRequest.setFastestInterval(5 * 1000);
LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder()
.addLocationRequest(locationRequest);
builder.setAlwaysShow(true); //this is the key ingredient
PendingResult<LocationSettingsResult> result =
LocationServices.SettingsApi.checkLocationSettings(mGoogleApiClient, builder.build());
result.setResultCallback(new ResultCallback<LocationSettingsResult>() {
@Override
public void onResult(LocationSettingsResult result) {
final Status status = result.getStatus();
final LocationSettingsStates state = result.getLocationSettingsStates();
switch (status.getStatusCode()) {
case LocationSettingsStatusCodes.SUCCESS:
// All location settings are satisfied. The client can initialize location
// requests here.
linearLayout.setVisibility(View.VISIBLE);
break;
case LocationSettingsStatusCodes.RESOLUTION_REQUIRED:
// Location settings are not satisfied. But could be fixed by showing the user
// a dialog.
try {
// Show the dialog by calling startResolutionForResult(),
// and check the result in onActivityResult().
status.startResolutionForResult(FindServiceProviders.this, REQUEST_CHECK_SETTINGS);
} catch (IntentSender.SendIntentException e) {
// Ignore the error.
}
break;
case LocationSettingsStatusCodes.SETTINGS_CHANGE_UNAVAILABLE:
// Location settings are not satisfied. However, we have no way to fix the
// settings so we won't show the dialog.
break;
}
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == PERM_REQUEST_CODE_DRAW_OVERLAYS) {
if (android.os.Build.VERSION.SDK_INT >= 23) { //Android M Or Over
if (!Settings.canDrawOverlays(this)) {
Log.e("overlayResult","false");
// ADD UI FOR USER TO KNOW THAT UI for SYSTEM_ALERT_WINDOW permission was not granted earlier...
}
}
}
switch (requestCode) {
// Check for the integer request code originally supplied to startResolutionForResult().
case REQUEST_CHECK_SETTINGS:
switch (resultCode) {
case Activity.RESULT_OK:
linearLayout.setVisibility(View.VISIBLE);
mapFrag.getMapAsync(this);
break;
case Activity.RESULT_CANCELED:
//setingsrequest();//keep asking if imp or do whatever
finish();
break;
}
break;
}
}
private void checkLocationPermission() {
if (!checkPermission()) {
// Should we show an explanation?
if (ActivityCompat.shouldShowRequestPermissionRationale(this,
Manifest.permission.ACCESS_FINE_LOCATION)) {
// Show an explanation to the user *asynchronously* -- don't block
// this thread waiting for the user's response! After the user
// sees the explanation, try again to request the permission.
new AlertDialog.Builder(this, R.style.MyDialogTheme)
.setTitle("Location Permission Needed")
.setMessage("This app needs the Location permission, please accept to use location functionality")
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
//Prompt the user once explanation has been shown
ActivityCompat.requestPermissions(FindServiceProviders.this,
new String[]{Manifest.permission.ACCESS_FINE_LOCATION},
MY_PERMISSIONS_REQUEST_LOCATION);
}
})
.create()
.show();
} else {
Log.e("overlay","detected but Request New Permission");
// No explanation needed, we can request the permission.
ActivityCompat.requestPermissions(this,
new String[]{Manifest.permission.ACCESS_FINE_LOCATION},
MY_PERMISSIONS_REQUEST_LOCATION);
}
}
}
@Override
public void onRequestPermissionsResult(int requestCode,
@NonNull String permissions[],@NonNull int[] grantResults) {
switch (requestCode) {
case MY_PERMISSIONS_REQUEST_LOCATION: {
// If request is cancelled, the result arrays are empty.
if (grantResults.length > 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// permission was granted, yay! Do the
// location-related task you need to do.
if (checkPermission()) {
//allowed
Log.e("allowed", String.valueOf(permissions));
if (!Utility.isLocationEnabled(getApplicationContext())) {
setingsrequest();
}
if (mGoogleApiClient == null) {
buildGoogleApiClient();
}
mapFrag.getMapAsync(this);
}else{
//set to never ask again
Log.e("set to never ask again", String.valueOf(permissions));
//do something here.
}
} else {
// permission denied, boo! Disable the
// functionality that depends on this permission.
checkLocationPermission();
}
return;
}
// other 'case' lines to check for other
// permissions this app might request
}
}
在onActivityResult中:
@Override
public void onRequestPermissionsResult(int permsRequestCode, String[] permissions, int[] grantResults) {
switch (permsRequestCode) {
case R_PERM: {
if (grantResults.length > 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
PermHandling();
}
else if (Build.VERSION.SDK_INT >= 23 && !shouldShowRequestPermissionRationale(permissions[0])) {
//Previously Permission Request was cancelled with 'Dont Ask Again',
// Redirect to Settings after showing Information about why you need the permission
AlertDialog.Builder builder = new AlertDialog.Builder(TestActivity.this,R.style.MyDialogTheme);
builder.setTitle("Need Storage Permission");
builder.setMessage("This app needs storage permission.");
builder.setPositiveButton("Grant", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
sentToSettings = true;
Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
Uri uri = Uri.fromParts("package", getPackageName(), null);
intent.setData(uri);
startActivityForResult(intent, REQUEST_PERMISSION_SETTING);
// Toast.makeText(getBaseContext(), "Go to Permissions to Grant Storage", Toast.LENGTH_LONG).show();
}
});
builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
builder.show();
}
else {
new AlertDialog.Builder(this,R.style.MyDialogTheme)
.setTitle("Location Permission Needed")
.setMessage("This app needs the Location permission, please accept to use location functionality")
.setPositiveButton("Grant", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
//Prompt the user once explanation has been shown
ActivityCompat.requestPermissions(TestActivity.this,
new String[]{android.Manifest.permission.ACCESS_FINE_LOCATION},
R_PERM );
}
})
.create()
.show();
}
return;
}
}
}