gpt4 book ai didi

android - 检查权限并重新加载 Activity

转载 作者:太空宇宙 更新时间:2023-11-03 12:29:27 27 4
gpt4 key购买 nike

大家好我在检查运行时权限后遇到加载 Activity 元素的问题我为 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;
}


}

是的,我在 Activity 中有更多元素,但对于示例来说,只有单个文本很好 :),检查权限很好,但是当我按下允许权限时, Activity 文本状态是默认文本 xD! “小文本”,所以 setText 不起作用,但如果我关闭应用程序并再次打开,一切都可以正常工作,因为我已经允许了许可。嘿,我是第一次为棉花糖制作应用程序,所以可能问题很愚蠢哈哈哈。

好吧,在那之后不起作用,我想也许如果我创建一个新 Activity LoadActivity,我可以在那里检查权限(将功能检查和所有关于检查权限从 MainActivity 移到 LoadActivity),如果条件很好,我初始化 MainActivity,这个很好的启动新 Activity (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问候

最佳答案

你检查权限是否被授予,然后调用 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");
}
}
}

如果您想在授予权限后设置文本,那么您应该覆盖方法 onRequestPermissionResult 并从中再次调用 setText。 Activity的onCreate方法在获得权限后不会再被调用。

这就是为什么它在您关闭并重新打开应用程序时起作用的原因,因为您的应用程序的新打开会再次调用 onCreate Activity ,现在您的方法 checkAndRequestPermissions 返回 true => setText 将被调用

关于android - 检查权限并重新加载 Activity ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37963820/

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