SplashScreen的Android Marshmallow运行时权限

时间:2016-10-04 06:52:35

标签: android android-studio android-permissions

我在飞溅屏幕上获得棉花糖许可时撞墙。我的动机是在加载启动画面前它应该提示用户许可。但是,在我的情况下,它会提示但只有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();


    }

}

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

1 个答案:

答案 0 :(得分:3)

从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();
 }