我有我的校长activty
,首先我打电话给对话框警告用户
权限,当用户点击GPS权限中的“接受”时,buttons
工作正常,但当我“拒绝”权限button
不能正常工作
这是我的班级:
public class InicioActivity extends AppCompatActivity {
public int count=0;
int tempInt = 0;
private FirebaseUser mFirebaseUser;
private FirebaseAuth firebaseUser;
private Button mButtonTrabajador;
private Button mButtonCliente;
private static final int RC_LOCATION_CONTACTS_PERM = 99;
private static final int RC_LOCATION_CONTACTS_PERME = 122;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_inicio);
mFirebaseUser = FirebaseAuth.getInstance().getCurrentUser();
CalligraphyConfig.initDefault(new CalligraphyConfig.Builder()
.setDefaultFontPath("fonts/RobotoLight.ttf")
.setFontAttrId(R.attr.fontPath)
.build()
);
String permission = "android.permission.ACCESS_COARSE_LOCATION";
final int res = this.checkCallingOrSelfPermission(permission);
String permission2 = "android.permission.ACCESS_FINE_LOCATION";
final int res2 = this.checkCallingOrSelfPermission(permission2);
mButtonCliente = (Button) findViewById(R.id.buttonIrARegistroCliente);
mButtonTrabajador = (Button) findViewById(R.id.buttonIrARegistroTrabajador);
mButtonCliente.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if(res == PackageManager.PERMISSION_GRANTED && res2 == PackageManager.PERMISSION_GRANTED){
Intent intent = new Intent(InicioActivity.this, RegisterActivity.class);
//intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(intent);
}else{
final AlertDialog alertDialog = new AlertDialog.Builder(getApplicationContext(), R.style.Theme_AppCompat_Light_Dialog).create();
alertDialog.setTitle("Los permisos tienen que ser activados o no podra continuar, desea actualizarlos?");
alertDialog.setCanceledOnTouchOutside(false);
alertDialog.setButton(AlertDialog.BUTTON_POSITIVE, "Ir a configuracion", new DialogInterface.OnClickListener() {
@Override
public void onClick(final DialogInterface dialog, int which) {
Intent intent = new Intent();
intent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
Uri uri = Uri.fromParts("package", InicioActivity.this.getPackageName(), null);
intent.setData(uri);
startActivity(intent);
}
});
alertDialog.setButton(AlertDialog.BUTTON_NEGATIVE, "Cancelar", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
alertDialog.dismiss();
}
});
}
}
});
mButtonTrabajador.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if(res == PackageManager.PERMISSION_GRANTED && res2 == PackageManager.PERMISSION_GRANTED) {
Intent intent = new Intent(InicioActivity.this, RegisterActivityWorkers.class);
//intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(intent);
}else{
final AlertDialog alertDialog = new AlertDialog.Builder(getApplicationContext(), R.style.Theme_AppCompat_Light_Dialog).create();
alertDialog.setTitle("Los permisos tienen que ser activados o no podra continuar, desea actualizarlos?");
alertDialog.setCanceledOnTouchOutside(false);
alertDialog.setButton(AlertDialog.BUTTON_POSITIVE, "Ir a configuracion", new DialogInterface.OnClickListener() {
@Override
public void onClick(final DialogInterface dialog, int which) {
Intent intent = new Intent();
intent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
Uri uri = Uri.fromParts("package", InicioActivity.this.getPackageName(), null);
intent.setData(uri);
startActivity(intent);
}
});
alertDialog.setButton(AlertDialog.BUTTON_NEGATIVE, "Cancelar", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
alertDialog.dismiss();
}
});
}
}
});
RequestPermissions();
}
@Override
protected void attachBaseContext(Context newBase) {
super.attachBaseContext(CalligraphyContextWrapper.wrap(newBase));
}
private void RequestPermissions(){
if (ContextCompat.checkSelfPermission(getApplicationContext(),
Manifest.permission.ACCESS_COARSE_LOCATION)
!= PackageManager.PERMISSION_GRANTED) {
// Should we show an explanation?
if (ActivityCompat.shouldShowRequestPermissionRationale(InicioActivity.this,
Manifest.permission.ACCESS_COARSE_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.
} else {
// No explanation needed, we can request the permission.
ActivityCompat.requestPermissions(InicioActivity.this,
new String[]{Manifest.permission.ACCESS_COARSE_LOCATION},
RC_LOCATION_CONTACTS_PERM);
// MY_PERMISSIONS_REQUEST_READ_CONTACTS is an
// app-defined int constant. The callback method gets the
// result of the request.
}
}
if (ContextCompat.checkSelfPermission(getApplicationContext(),
Manifest.permission.ACCESS_FINE_LOCATION)
!= PackageManager.PERMISSION_GRANTED) {
// Should we show an explanation?
if (ActivityCompat.shouldShowRequestPermissionRationale(InicioActivity.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.
} else {
// No explanation needed, we can request the permission.
ActivityCompat.requestPermissions(InicioActivity.this,
new String[]{Manifest.permission.ACCESS_FINE_LOCATION},
RC_LOCATION_CONTACTS_PERME);
// MY_PERMISSIONS_REQUEST_READ_CONTACTS is an
// app-defined int constant. The callback method gets the
// result of the request.
}
}
}
@Override
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
switch (requestCode) {
case RC_LOCATION_CONTACTS_PERM: {
// If request is cancelled, the result arrays are empty.
if (grantResults.length > 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
} else {
}
return;
}
}
}
}
当我关闭应用程序时,问题仍然存在,当我在onCreate中调用对话框时,我遇到了同样的问题。
答案 0 :(得分:1)
当用户首先拒绝权限时,那么第二次你必须使用rational,这是一个使用它的示例代码
public void checkPermission() {
Log.e(TAG, " inside checkSelfPermission()");
resultCode = ContextCompat.checkSelfPermission(context, Manifest.permission.READ_CONTACTS);
System.out.println(TAG + " resultCode at start :" + String.valueOf(resultCode));
if (resultCode != PackageManager.PERMISSION_GRANTED) {
//This block only will get executed after you denied first
//Here you can show why You need , then try again
if (ActivityCompat.shouldShowRequestPermissionRationale((Activity) context, Manifest.permission.READ_CONTACTS)) {
Log.w(TAG, "---You disabled previously, I have to show it again ----");
new AlertDialog.Builder(context).setCancelable(false).setTitle("Hi Dumb User")
.setMessage("You denied Permission ! before !! Which is required\n" +
"If you still deny ,then go to Setting ").
setPositiveButton("TRY AGAIN ", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
//request Again
requestPermission();
}
}).setNeutralButton("DISMISS", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
dialogInterface.dismiss();
}
}).show();
} else {
Log.w(TAG, " --else --");
requestPermission();//ask
}
}
}
答案 1 :(得分:1)
首先,您没有显示alertDialog。请使用alertdialog.show()
显示AlertDialog。
然后使用以下AlertDialog代码
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(Main3Activity.this);
// set title
//alertDialogBuilder.setTitle("Are you sure to Cancel the Subscription ?");
alertDialogBuilder.setTitle("Los permisos tienen que ser activados o no podra continuar, desea actualizarlos?");
// set dialog message
alertDialogBuilder
//.setMessage("After cancelling you will automatically got logout and then you have to buy subscription again")
.setCancelable(true)
.setPositiveButton("Ir a configuracion", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
Intent intent = new Intent();
intent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
Uri uri = Uri.fromParts("package", getPackageName(), null);
intent.setData(uri);
startActivity(intent);
}
})
.setNegativeButton("Cancelar", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
AlertDialog alertDialog = alertDialogBuilder.create();
alertDialog.show();
答案 2 :(得分:0)
如果您说对话框中的否定按钮或取消按钮不起作用,那么您可以试试这个
alertDialog.setButton(AlertDialog.BUTTON_NEGATIVE, "Cancelar", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
dialogInterface.dismiss();
}
});
我也很惊讶你在哪里打电话给对话节目。
alertDialog.show();