2016-10-04 10 views
2

スプラッシュ画面でmarshmallowの許可を与えている間に壁に当たった。私の動機はスプラッシュ画面を読み込む前にユーザーに許可を求める必要があります。しかし、私の場合はプロンプトが表示されます2秒間は隠されます。Android Marshmallowランタイムスプラッシュ画面のアクセス許可

これは、コード

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


    } 

} 

私はscreen.Couldはあなたが変更を加えるために私を助けてスプラッシュをロードする前にプロンプ​​トショーを持ちたいですか?

+0

私はあなたをお勧めしますこのライブラリを見てください。それは物事をもっと楽にします。 [PermissionsDispatcher](https://github.com/hotchemi/PermissionsDispatcher) –

+0

Musafa、ええ、私はそれを読んだが、どういうわけか、これを最初に解決したい。 – notTdar

+0

これはあなたが使っている非常に悪いデザインです。あなたのデザインには多くの抜け穴があります。また、スプラッシュ画面でアニメーションを行い、許可が与えられていないかどうかを確認し、許可を求める専用のアクティビティを起動し、許可されていれば、それ以外の場合は終了します。 –

答えて

3

メソッドを削除します。netValidator(); onResumeからここでそれを呼び出す:

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

また、あなたは、このチェックされ、このメソッドを呼び出す:あなたのonCreate(中

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

)を次のようにします:

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

私はonStart()を削除しなければなりません。次に、次回にアプリケーションを開くときに、スプラッシュにアクセス権が与えられたら、スプラッシュに貼り付けられます。 – notTdar

+0

あなたはonStart()をチェックインする必要はありません –

+0

もちろん、 – notTdar

関連する問題