Hi everyone I have a problem with load activity elements after checking runtime permission I made a app for Lollipop but now I need update to Marshmallow so I have this problem
I have a MainActivity
public static final int REQUEST_ID_MULTIPLE_PERMISSIONS = 1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if(checkAndRequestPermissions(this)) {
//Var created
Internetstatus = (TextView) findViewById(R.id.statusInternet);
if (!verificaConexion(this)) {
Internetstatus.setText("NOT CONNECTED");
} else {
Internetstatus.setText("CONNECTED");
}
}
}
public static boolean verificaConexion(Context ctx) {
boolean bConectado = false;
ConnectivityManager cm = (ConnectivityManager) ctx.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
if (activeNetwork != null) {
bConectado = true;
}
return bConectado;
}
private boolean checkAndRequestPermissions(Context ctx) {
if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
Toast.makeText(this, "Checking permission…", Toast.LENGTH_SHORT).show ();
//List of permissions
int permissionInternet = ContextCompat.checkSelfPermission(this,
Manifest.permission.INTERNET);
int permissionAccessNetwork = ContextCompat.checkSelfPermission(this,
Manifest.permission.ACCESS_NETWORK_STATE);
int permissionReadExternal = ContextCompat.checkSelfPermission(this,
Manifest.permission.READ_EXTERNAL_STORAGE);
int permissionWriteExternal = ContextCompat.checkSelfPermission(this,
Manifest.permission.WRITE_EXTERNAL_STORAGE);
List<String> listPermissionsNeeded = new ArrayList<>();
//Verify status of permissions
if (permissionInternet != PackageManager.PERMISSION_GRANTED) {
listPermissionsNeeded.add(Manifest.permission.INTERNET);
}
if (permissionAccessNetwork != PackageManager.PERMISSION_GRANTED) {
listPermissionsNeeded.add(Manifest.permission.ACCESS_NETWORK_STATE);
}
if (permissionReadExternal != PackageManager.PERMISSION_GRANTED) {
listPermissionsNeeded.add(Manifest.permission.READ_EXTERNAL_STORAGE);
}
if (permissionWriteExternal != PackageManager.PERMISSION_GRANTED) {
listPermissionsNeeded.add(Manifest.permission.WRITE_EXTERNAL_STORAGE);
}
if (!listPermissionsNeeded.isEmpty()) {
ActivityCompat.requestPermissions(this, listPermissionsNeeded.toArray(new String[listPermissionsNeeded.size()]), REQUEST_ID_MULTIPLE_PERMISSIONS);
return false;
}
return true;
}
return true;
}
}
Right, I have more elements in the activity but with the only single text is fine for the example :), the checking permission is fine but when I press allow the permissions the activity text status is the default text xD! "Small Text", so the setText not work, but if I close the app and open again all work perfectly because I have allowed the permission yet. Hey is my first time that I make app for marshmallow, so maybe the problem is very stupid hahahha.
Well after that dont work I thought that maybe if I create a new activity LoadActivity I can check the permissions there (move function check and all about check permission from MainActivity to LoadActivity) and if the conditional is fine I initialize the MainActivity, this fine start new activity (LoadActivity), I check permissions but start the MainActivity dont work after I allow permissions
The new LoadActivity is
public class LoadActivity extends AppCompatActivity {
public static final int REQUEST_ID_MULTIPLE_PERMISSIONS = 1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_load);
if(checkAndRequestPermissions(this)) {
Intent intent = new Intent(this, MainActivity.class);
startActivityForResult(intent, 0);
}
}
private boolean checkAndRequestPermissions(Context ctx) {
if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
Toast.makeText(this, "Checking permission…", Toast.LENGTH_SHORT).show ();
//List of permissions
int permissionInternet = ContextCompat.checkSelfPermission(this,
Manifest.permission.INTERNET);
int permissionAccessNetwork = ContextCompat.checkSelfPermission(this,
Manifest.permission.ACCESS_NETWORK_STATE);
int permissionReadExternal = ContextCompat.checkSelfPermission(this,
Manifest.permission.READ_EXTERNAL_STORAGE);
int permissionWriteExternal = ContextCompat.checkSelfPermission(this,
Manifest.permission.WRITE_EXTERNAL_STORAGE);
List<String> listPermissionsNeeded = new ArrayList<>();
//Verify status of permissions
if (permissionInternet != PackageManager.PERMISSION_GRANTED) {
listPermissionsNeeded.add(Manifest.permission.INTERNET);
}
if (permissionAccessNetwork != PackageManager.PERMISSION_GRANTED) {
listPermissionsNeeded.add(Manifest.permission.ACCESS_NETWORK_STATE);
}
if (permissionReadExternal != PackageManager.PERMISSION_GRANTED) {
listPermissionsNeeded.add(Manifest.permission.READ_EXTERNAL_STORAGE);
}
if (permissionWriteExternal != PackageManager.PERMISSION_GRANTED) {
listPermissionsNeeded.add(Manifest.permission.WRITE_EXTERNAL_STORAGE);
}
if (!listPermissionsNeeded.isEmpty()) {
ActivityCompat.requestPermissions(this, listPermissionsNeeded.toArray(new String[listPermissionsNeeded.size()]), REQUEST_ID_MULTIPLE_PERMISSIONS);
return false;
}
return true;
}
return true;
}
}
I hope someone can help me :D Regards
You check if the permission is granted then call setText else do nothing.
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if(checkAndRequestPermissions(this)) { //Var created
Internetstatus = (TextView) findViewById(R.id.statusInternet);
if (!verificaConexion(this)) {
Internetstatus.setText("NOT CONNECTED");
} else {
Internetstatus.setText("CONNECTED");
}
}
}
If you want to setText after permission was granted then you should override method onRequestPermissionResult and call setText from this again. The activity onCreate method would not be called again after permission was granted.
Thats the point why it works when you close and reopen the app, because the new opening of your app calls activity onCreate again and now your method checkAndRequestPermissions returns true => setText will be called
Read this page: https://developer.android.com/training/permissions/requesting.html
The major problem is you request permissions and then return false
if (!listPermissionsNeeded.isEmpty()) {
ActivityCompat.requestPermissions(this, listPermissionsNeeded.toArray(new String[listPermissionsNeeded.size()]), REQUEST_ID_MULTIPLE_PERMISSIONS);
return false;
}
So this if statement will not pass
if(checkAndRequestPermissions(this))
You must implement onRequestPermissionsResult() as has been said on comments.
Also, your method is private and not static so you don't need to pass Context
as an argument, you can call this
inside it like you do, just pointing not very important.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With