gpt4 book ai didi

SplashScreen 的 Android Marshmallow 运行时权限

转载 作者:搜寻专家 更新时间:2023-11-01 08:31:52 25 4
gpt4 key购买 nike

我在启动画面中的棉花糖中授予权限时撞墙了。我的动机是在加载启动画面之前它应该提示用户获得许可。但是,在我的情况下,它会提示但只提示 2 秒然后被隐藏.

这是代码

    public class SplashScreen extends AppCompatActivity {
AlertDialog dailog;
AlertDialog.Builder builder;

ProgressBar progressBar;
int progressStatus = 0;
TextView textView1, textView2;
Handler handler = new Handler();
private SessionManager session;
ConnectivityManager cm;
boolean isConnected;
private int STORAGE_PERMISSION_CODE = 23;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splash_screen);
builder = new AlertDialog.Builder(SplashScreen.this);
session = new SessionManager(getApplicationContext());
if (getSupportActionBar() != null) {
getSupportActionBar().hide();
}

cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);

NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
isConnected = activeNetwork != null &&
activeNetwork.isConnectedOrConnecting();

if (Build.VERSION.SDK_INT > Build.VERSION_CODES.KITKAT) {
setupWindowAnimations();
}


if(isReadStorageAllowed()){
//If permission is already having then showing the toast
Toast.makeText(SplashScreen.this,"You already have the permission",Toast.LENGTH_LONG).show();
//Existing the method with return
return;
}

//If the app has not the permission then asking for the permission
requestStoragePermission();

}

@Override
protected void onStart() {
super.onStart();

if(isReadStorageAllowed()){
//If permission is already having then showing the toast
Toast.makeText(SplashScreen.this,"You already have the permission",Toast.LENGTH_LONG).show();
//Existing the method with return
return;
}else //If the app has not the permission then asking for the permission
requestStoragePermission();
}

//First permission before activity creation then it would undergo onpause then onResume check net
@Override
protected void onResume() {
super.onResume();
netValidator();
}

public void netValidator(){
if(isConnected) {
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
forNext();
}
}, 2000);
}else{
showDialog();
}

}
private boolean isReadStorageAllowed() {
//Getting the permission status
int result = ContextCompat.checkSelfPermission(this, Manifest.permission.READ_PHONE_STATE);
//If permission is granted returning true
if (result == PackageManager.PERMISSION_GRANTED)
return true;

//If permission is not granted returning false
return false;
}
private void requestStoragePermission(){

if(ActivityCompat.shouldShowRequestPermissionRationale(this,Manifest.permission.READ_PHONE_STATE)){
//If the user has denied the permission previously your code will come to this block
//Here you can explain why you need this permission
//Explain here why you need this permission
//Toast.makeText(SplashScreen.this, "For Marsmallow,we do need this permission", Toast.LENGTH_SHORT).show();
}

//And finally ask for the permission
ActivityCompat.requestPermissions(this,new String[]{Manifest.permission.READ_PHONE_STATE},STORAGE_PERMISSION_CODE);
}

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {

//Checking the request code of our request
if(requestCode == STORAGE_PERMISSION_CODE){

//If permission is granted
if(grantResults.length >0 && grantResults[0] == PackageManager.PERMISSION_GRANTED){


//Displaying a toast
// Toast.makeText(this,"Permission granted now you can read the storage",Toast.LENGTH_LONG).show();
}else{
// finish();
//Displaying another toast if permission is not granted
//Toast.makeText(this,"You have just denied the permission",Toast.LENGTH_LONG).show();
}
}
}
private void forNext(){

if ((TextUtils.isEmpty(session.getUserName()))) {
startActivity(new Intent(SplashScreen.this,splash_Login.class));
finish();
} else {
Intent i = new Intent(SplashScreen.this, MainActivity.class);
startActivity(i);
finish();
}
}
private void showDialog(){

builder.setTitle("Warning !");
builder.setCancelable(false);
builder.setMessage("This application requires Internet connection.Go to Setting and Activate Internet");
builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {

finish();
startActivity(new Intent(Settings.ACTION_SETTINGS));

}

});
builder.setNegativeButton("EXIT", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {

finish();
}

});

dailog = builder.create();
dailog.show();


}

}

我想在加载启动画面之前进行提示显示。你能帮我在哪里进行更改吗?

最佳答案

从 onResume 中删除您的方法 netValidator(); 并在此处调用它:

  if(grantResults.length >0 && grantResults[0] == PackageManager.PERMISSION_GRANTED){

netValidator();

//Displaying a toast
// Toast.makeText(this,"Permission granted now you can read the storage",Toast.LENGTH_LONG).show();
}

也可以在您要检查的地方调用此方法:

 if(isReadStorageAllowed()){
netValidator();
}

在你的 onCreate() 中这样做:

  if(isReadStorageAllowed()){
//If permission is already having then showing the toast
Toast.makeText(SplashScreen.this,"You already have the
permission",Toast.LENGTH_LONG).show();

netValidator();
//Existing the method with return
//return; Remove this
}else{

//If the app has not the permission then asking for the permission
requestStoragePermission();
}

关于SplashScreen 的 Android Marshmallow 运行时权限,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39845907/

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