大家好我在检查运行时权限后遇到加载活动元素的问题我为Lollipop做了一个应用程序但是现在我需要更新到Marshmallow所以我有这个问题
我有一个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;
}
}
是的,我在活动中有更多的元素但是只有单个文本对于示例:)很好,检查权限很好但是当我按下权限时,活动文本状态是默认文本xD! &#34;小文本&#34;,所以setText不起作用,但如果我关闭应用程序并再次打开所有工作完美,因为我已经允许了许可。嘿,这是我第一次为棉花糖制作应用程序,所以也许这个问题非常愚蠢hahahha。
那之后不行,我想也许如果我创建一个新的活动LoadActivity我可以检查那里的权限(移动函数检查和所有关于从MainActivity到LoadActivity的检查权限),如果条件很好我初始化MainActivity,这个很好的开始新活动(LoadActivity),我检查权限,但在允许权限后启动MainActivity不起作用
新的LoadActivity是
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;
}
}
我希望有人可以帮助我:D 问候
答案 0 :(得分:3)
您检查是否已授予权限,然后调用setText否则不执行任何操作。
@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");
}
}
}
如果你想在授予权限后想要setText,那么你应该重写方法onRequestPermissionResult并再次调用setText。授予权限后,不会再次调用onCreate方法。
这就是为什么当你关闭并重新打开应用程序时它的工作原理,因为你的应用程序的新开放再次调用onCreate活动,现在你的方法checkAndRequestPermissions返回true =&gt;将调用setText
答案 1 :(得分:2)
阅读本页: https://developer.android.com/training/permissions/requesting.html
主要问题是您请求权限然后返回false
if (!listPermissionsNeeded.isEmpty()) {
ActivityCompat.requestPermissions(this, listPermissionsNeeded.toArray(new String[listPermissionsNeeded.size()]), REQUEST_ID_MULTIPLE_PERMISSIONS);
return false;
}
所以这个if语句不会传递
if(checkAndRequestPermissions(this))
您必须按照评论中的说法实施onRequestPermissionsResult()。
此外,您的方法是私有的而不是静态的,因此您不需要将Context
作为参数传递,您可以像调用this
一样调用它,只是指出不是很重要
答案 2 :(得分:1)
非常感谢大家现在我能理解这一点,好吧我粘贴我的新功能代码,也许可以帮助别人。
public class LoadActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_load);
insertDummyContactWrapper();
}
final private int REQUEST_CODE_ASK_MULTIPLE_PERMISSIONS = 124;
private void insertDummyContactWrapper() {
if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
List<String> permissionsNeeded = new ArrayList<String>();
final List<String> permissionsList = new ArrayList<String>();
if (!addPermission(permissionsList, Manifest.permission.INTERNET))
permissionsNeeded.add("Internet.");
if (!addPermission(permissionsList, Manifest.permission.ACCESS_NETWORK_STATE))
permissionsNeeded.add("Internet Status.");
if (!addPermission(permissionsList, Manifest.permission.READ_EXTERNAL_STORAGE))
permissionsNeeded.add("Read Files.");
if (!addPermission(permissionsList, Manifest.permission.WRITE_EXTERNAL_STORAGE))
permissionsNeeded.add("Write Files.");
if (permissionsList.size() > 0) {
if (permissionsNeeded.size() > 0) {
// Need Rationale
String message = "You need to grant access to " + permissionsNeeded.get(0);
for (int i = 1; i < permissionsNeeded.size(); i++)
message = message + ", " + permissionsNeeded.get(i);
showMessageOKCancel(message,
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
requestPermissions(permissionsList.toArray(new String[permissionsList.size()]),
REQUEST_CODE_ASK_MULTIPLE_PERMISSIONS);
}
}
});
return;
}
requestPermissions(permissionsList.toArray(new String[permissionsList.size()]),
REQUEST_CODE_ASK_MULTIPLE_PERMISSIONS);
return;
}
}
startApp();
}
private boolean addPermission(List<String> permissionsList, String permission) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (checkSelfPermission(permission) != PackageManager.PERMISSION_GRANTED) {
permissionsList.add(permission);
// Check for Rationale Option
if (!shouldShowRequestPermissionRationale(permission))
return false;
}
}
return true;
}
private void showMessageOKCancel(String message, DialogInterface.OnClickListener okListener) {
new AlertDialog.Builder(LoadActivity.this)
.setMessage(message)
.setPositiveButton("OK", okListener)
.setNegativeButton("Cancel", null)
.create()
.show();
}
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
switch (requestCode) {
case REQUEST_CODE_ASK_MULTIPLE_PERMISSIONS:
{
Map<String, Integer> perms = new HashMap<String, Integer>();
// Initial
perms.put(Manifest.permission.INTERNET, PackageManager.PERMISSION_GRANTED);
perms.put(Manifest.permission.ACCESS_NETWORK_STATE, PackageManager.PERMISSION_GRANTED);
perms.put(Manifest.permission.READ_EXTERNAL_STORAGE, PackageManager.PERMISSION_GRANTED);
perms.put(Manifest.permission.WRITE_EXTERNAL_STORAGE, PackageManager.PERMISSION_GRANTED);
// Fill with results
for (int i = 0; i < permissions.length; i++)
perms.put(permissions[i], grantResults[i]);
// Check for ACCESS_FINE_LOCATION
if (perms.get(Manifest.permission.INTERNET) == PackageManager.PERMISSION_GRANTED
&& perms.get(Manifest.permission.ACCESS_NETWORK_STATE) == PackageManager.PERMISSION_GRANTED
&& perms.get(Manifest.permission.READ_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED
&& perms.get(Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) {
// All Permissions Granted
startApp();
} else {
// Permission Denied
Toast.makeText(LoadActivity.this, "Some Permission is Denied, please allow permission for that the app can work.", Toast.LENGTH_SHORT)
.show();
}
}
break;
default:
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
}
}
private void startApp(){
Intent intent = new Intent(this, MainActivity.class);
startActivityForResult(intent, 0);
}
}
希望它可以帮助其他人:D
为了让它基于其他用户评论的文章,最后来自本文
并推荐这个视频为noobs我在android marshmallow
https://www.youtube.com/watch?v=C8lUdPVSzDk
问候!
答案 3 :(得分:0)
通过实现ActivityCompat.OnRequestPermissionsResultCallback接口,我们可以检查用户是否获得了请求的权限。如果给定,我们可以通过实现名为onRequestPermissionsResult的方法再次加载活动。有关详细信息,请查看此链接https://developer.android.com/training/permissions/requesting.html
public class MainActivity extends AppCompatActivity implements ActivityCompat.OnRequestPermissionsResultCallback {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if(isStoragePermissionGranted()) {
// code to implement after permission granted
}
}
public boolean isStoragePermissionGranted() {
String TAG = "logcheck";
if (Build.VERSION.SDK_INT >= 23) {
if (checkSelfPermission(android.Manifest.permission.READ_EXTERNAL_STORAGE)
== PackageManager.PERMISSION_GRANTED ) {
Log.v(TAG,"Permission is granted");
return true;
} else {
Log.v(TAG,"Permission is revoked");
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, 1);
return false;
}
}
else { //permission is automatically granted on sdk<23 upon installation
Log.v(TAG,"Permission is granted");
return true;
}
}
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults){
boolean flag;
flag = true;
if(requestCode == 1)
{
for (int i:grantResults)
{
if( i == PackageManager.PERMISSION_DENIED)
flag = false;
}
if (flag)
{
Intent intent = new Intent(this,MainActivity.class);
finish();
startActivity(intent);
}
}
}
}