gpt4 book ai didi

android - 当我拒绝权限时,setOnClickListener 不执行任何操作 - android

转载 作者:搜寻专家 更新时间:2023-11-01 09:29:36 26 4
gpt4 key购买 nike

我有我的主体 activty,首先我调用一个对话框来警告用户权限,当用户在 GPS 权限中单击“接受”时,按钮 工作正常,但是当我“拒绝”权限时,按钮 不起作用

这是我的课:

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 中调用对话框时,我遇到了同样的问题。

最佳答案

首先你没有显示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();

关于android - 当我拒绝权限时,setOnClickListener 不执行任何操作 - android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48179375/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com