2016-11-16 117 views
1

我想在我的項目中添加權限棉花糖用戶的情況下工作的多個權限是:
我需要3個權限我的設備的攝像頭,手機短信,電話安卓:在模擬器而不是在其他一些設備

當應用程序運行時只顯示一個許可,其他兩個權限得到了跳過,

,我閉上我的應用程序,並重新打開它,它表明第二權限和第三一個跳過,然後再次關閉它,打開它第三個權限顯示如此。

在嘗試它模擬器它完美的所有權限3顯示了在一次,但不是在設備(即三星J5,J7三星):

Permission.Java

public class PermissionActivity extends Activity { 
    int count = 0; 
    public static final String CAMERA_PREF = "camera_pref"; 
    public static final String SMS_PREF = "sms_pref"; 
    public static final String CALL_PREF = "call_pref"; 


    public static final int MY_PERMISSIONS_REQUEST_CAMERA = 100; 
    public static final int MY_PERMISSIONS_REQUEST_RECIEVE_SMS = 101; 
    public static final int MY_PERMISSIONS_REQUEST_PHONE = 102; 


    public static final String ALLOW_KEY = "ALLOWED"; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_permission); 

     int currentapiVersion = android.os.Build.VERSION.SDK_INT; 


     if (currentapiVersion >= android.os.Build.VERSION_CODES.M) { 

      chk_all_permissions(); 

     } else { 

      // do something for phones running an SDK before lollipop 
      start_app(); 

     } 


    } 

    // Example for sms permissions 
    public static Boolean getFromPref(Context context, String key) { 
     SharedPreferences myPrefs = context.getSharedPreferences(CAMERA_PREF, 
       Context.MODE_PRIVATE); 
     Log.e("Key", String.valueOf(key)); 
     return (myPrefs.getBoolean(key, false)); 
    } 
    public static Boolean getFromPrefSms(Context context, String key) { 
     SharedPreferences myPrefs = context.getSharedPreferences(SMS_PREF, 
       Context.MODE_PRIVATE); 
     Log.e("Key", String.valueOf(key)); 
     return (myPrefs.getBoolean(key, false)); 
    } 
    public static Boolean getFromPrefCalls(Context context, String key) { 
     SharedPreferences myPrefs = context.getSharedPreferences(CALL_PREF, 
       Context.MODE_PRIVATE); 
     Log.e("Key", String.valueOf(key)); 
     return (myPrefs.getBoolean(key, false)); 
    } 

    private void chk_all_permissions() { 
     if (ContextCompat.checkSelfPermission(this, 
       Manifest.permission.RECEIVE_SMS) 
       != PackageManager.PERMISSION_GRANTED) { 
      marshmallow_permissions_sms(); 
     } else if (ContextCompat.checkSelfPermission(this, 
       Manifest.permission.CAMERA) 
       != PackageManager.PERMISSION_GRANTED) { 

      marshmallow_permission_camera(); 
     } else if (ContextCompat.checkSelfPermission(this, 
       Manifest.permission.READ_PHONE_STATE) 
       != PackageManager.PERMISSION_GRANTED) { 
      marshmallow_permission_phone(); 

     } 
    } 


    // Example for camera permissions 
    private void marshmallow_permission_camera() { 
     if (ContextCompat.checkSelfPermission(this, 
       Manifest.permission.CAMERA) 
       != PackageManager.PERMISSION_GRANTED) { 
      if (getFromPref(this, ALLOW_KEY)) { 
       customPermDialogue(); 

      } else if (ContextCompat.checkSelfPermission(this, 
        Manifest.permission.CAMERA) 
        != PackageManager.PERMISSION_GRANTED) { 
       // Should we show an explanation? 
       Log.e("Key", "CAMERA"); 

       if (ActivityCompat.shouldShowRequestPermissionRationale(this, 
         Manifest.permission.CAMERA)) { 
        ActivityCompat.requestPermissions(PermissionActivity.this, 
          new String[]{Manifest.permission.CAMERA,}, 
          MY_PERMISSIONS_REQUEST_CAMERA); 
        Log.e("Key5", "SCAmera"); 

       } else { 
        // No explanation needed, we can request the permission. 
        ActivityCompat.requestPermissions(this, 
          new String[]{Manifest.permission.CAMERA,}, 
          MY_PERMISSIONS_REQUEST_CAMERA); 
        Log.e("Key2", "SCAmera"); 


       } 
      } 
     } 
    } 



    private void marshmallow_permissions_sms() { 

     if (ContextCompat.checkSelfPermission(this, 
       Manifest.permission.RECEIVE_SMS) 
       != PackageManager.PERMISSION_GRANTED) { 
      if (getFromPrefSms(this, ALLOW_KEY)) { 
       Log.e("Key", "SMS2"); 

       customPermDialogue(); 

      } else if (ContextCompat.checkSelfPermission(this, 
        Manifest.permission.RECEIVE_SMS) 
        != PackageManager.PERMISSION_GRANTED) { 
       // Should we show an explanation? 
       Log.e("Key", "SMS"); 

       if (ActivityCompat.shouldShowRequestPermissionRationale(this, 
         Manifest.permission.RECEIVE_SMS)) { 
        ActivityCompat.requestPermissions(PermissionActivity.this, 
          new String[]{Manifest.permission.RECEIVE_SMS,}, 
          MY_PERMISSIONS_REQUEST_RECIEVE_SMS); 
        Log.e("Key5", "SMS"); 



       } else { 
        // No explanation needed, we can request the permission. 
        ActivityCompat.requestPermissions(this, 
          new String[]{Manifest.permission.RECEIVE_SMS,}, 
          MY_PERMISSIONS_REQUEST_RECIEVE_SMS); 
        Log.e("Key2", "SMS"); 



       } 
      } 
     } 


    } 


// Example for phone permissions 


private void marshmallow_permission_phone(){ 
     if (ContextCompat.checkSelfPermission(this, 
       Manifest.permission.READ_PHONE_STATE) 
       != PackageManager.PERMISSION_GRANTED) { 
      if (getFromPrefCalls(this, ALLOW_KEY)) { 
       Log.e("Key", "CALL2"); 

       showSettingsAlert(); 

      } else if (ContextCompat.checkSelfPermission(this, 
        Manifest.permission.READ_PHONE_STATE) 
        != PackageManager.PERMISSION_GRANTED) { 
       // Should we show an explanation? 
       Log.e("Key", "CALL"); 

       if (ActivityCompat.shouldShowRequestPermissionRationale(this, 
         Manifest.permission.READ_PHONE_STATE)) { 
        ActivityCompat.requestPermissions(PermissionActivity.this, 
          new String[]{Manifest.permission.READ_PHONE_STATE,}, 
          MY_PERMISSIONS_REQUEST_PHONE); 
        Log.e("Key5", "PHONE"); 


       } else { 
        // No explanation needed, we can request the permission. 
        ActivityCompat.requestPermissions(this, 
          new String[]{Manifest.permission.READ_PHONE_STATE,}, 
          MY_PERMISSIONS_REQUEST_PHONE); 
        Log.e("Key2", "CALL"); 


       } 
      } 
     } 

    } 



    public void showSettingsAlert() { 
     AlertDialog alertDialog = new AlertDialog.Builder(PermissionActivity.this).create(); 
     alertDialog.setTitle("Alert"); 
     alertDialog.setMessage("App needs to access the Camera."); 

     alertDialog.setButton(AlertDialog.BUTTON_NEGATIVE, "DONT ALLOW", 
       new DialogInterface.OnClickListener() { 
        public void onClick(DialogInterface dialog, int which) { 
         dialog.dismiss(); 
         finish(); 
        } 
       }); 

     alertDialog.setButton(AlertDialog.BUTTON_POSITIVE, "ALLOWAnce", 
       new DialogInterface.OnClickListener() { 

        public void onClick(DialogInterface dialog, int which) { 

         chk_all_permissions(); 
         dialog.dismiss(); 

        } 
       }); 
     alertDialog.show(); 

    } 

    // A dialogue to show if permission is declined 
    private void customPermDialogue() { 
     AlertDialog alertDialog = new AlertDialog.Builder(new ContextThemeWrapper(this,R.style.AppTheme)).create(); 
     alertDialog.setTitle("Alert"); 
     alertDialog.setCancelable(false); 
     alertDialog.setIcon(R.drawable.icon); 
     alertDialog.setMessage("App needs to access required Permissions !"); 
     alertDialog.setButton(AlertDialog.BUTTON_NEGATIVE, "Don't Allow", 
       new DialogInterface.OnClickListener() { 
        public void onClick(DialogInterface dialog, int which) { 
         dialog.dismiss(); 
         finish(); 
        } 
       }); 
     alertDialog.setButton(AlertDialog.BUTTON_POSITIVE, "Allow", 
       new DialogInterface.OnClickListener() { 
        public void onClick(DialogInterface dialog, int which) { 
         dialog.dismiss(); 
         chk_all_permissions(); 
        } 
       }); 
     alertDialog.show(); 
    } 
    public void start_app() 
    { 
     Intent i=new Intent(this,SplashScreen.class); 
     startActivity(i); 

    } 
    public void showCustomAlert(String msg) 
    { 

     Toast toast = Toast.makeText(getApplicationContext(), 
       msg, 
       Toast.LENGTH_SHORT); 
     TextView v = (TextView) toast.getView().findViewById(android.R.id.message); 
     v.setTextColor(Color.parseColor("#25456d")); 
     View toastView = toast.getView(); 
     toastView.setBackgroundResource(R.drawable.toastdrawable); 
     toast.show(); 

    } 

Mainifest .xml

<uses-permission android:name="android.permission.CAMERA" /> 
    <uses-permission android:name="android.permission.FLASHLIGHT" /> 
    <uses-permission android:name="android.permission.READ_PHONE_STATE" /> 
    <uses-permission android:name="android.permission.RECEIVE_SMS" /> 

回答

0

我以三種不同的活動做了和完美的和便宜的方式工作,但有一個解決辦法:

正確實施與警報框還檢查,如果用戶檢查
不再詢問

PemrissionCamer.java

import android.Manifest; 
    import android.app.Activity; 
    import android.content.DialogInterface; 
    import android.content.Intent; 
    import android.content.pm.PackageManager; 
    import android.net.Uri; 
    import android.provider.Settings; 
    import android.support.v4.app.ActivityCompat; 
    import android.support.v4.content.ContextCompat; 
    import android.support.v7.app.AlertDialog; 
    import android.support.v7.app.AppCompatActivity; 
    import android.os.Bundle; 
    import android.support.v7.internal.view.ContextThemeWrapper; 
    import android.util.Log; 
    import android.view.View; 
    import android.view.Window; 
    import android.view.WindowManager; 

    public class PermissionCamera extends Activity { 
      static int count=0; 
     int alert_time=0; 
     @Override 
     protected void onCreate(Bundle savedInstanceState) { 
      super.onCreate(savedInstanceState); 
      requestWindowFeature(Window.FEATURE_NO_TITLE); 
      getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, 
        WindowManager.LayoutParams.FLAG_FULLSCREEN); 
      requestWindowFeature(Window.FEATURE_NO_TITLE); 
      setContentView(R.layout.activity_permissioncalls); 
      int currentapiVersion = android.os.Build.VERSION.SDK_INT; 


      if (currentapiVersion >= android.os.Build.VERSION_CODES.M) { 
       if (ContextCompat.checkSelfPermission(this, 
         Manifest.permission.CAMERA) 
         != PackageManager.PERMISSION_GRANTED) { 
        ActivityCompat.requestPermissions(PermissionCamera.this, 
          new String[]{Manifest.permission.CAMERA}, 
          3); 
       } else { 
       start_app(); 
       } 
      } 
      else { 
       start_app2(); 
      } 
     } 

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

       case 3: { 

        // If request is cancelled, the result arrays are empty. 
        if (grantResults.length > 0 
          && grantResults[0] == PackageManager.PERMISSION_GRANTED) { 
         count+=1; 

         if(count==1) 
         { 
          start_app(); 


         } 
         // permission was granted, yay! Do the 
         // contacts-related task you need to do. 
        } else { 
         Log.e("Camera3","Camera2"); 


         // permission denied, boo! Disable the 
         // functionality that depends on this permission. 
         showAlert(); 

        } 
        return; 
       } 
       // other 'case' lines to check for other 
       // permissions this app might request 
      } 
     } 
     public void start_app() 
     { 
      Intent i=new Intent(this,PermissionSms.class); 
      startActivity(i); 

     } 
     public void start_app2() 
     { 
      Intent i=new Intent(this,SplashScreen.class); 
      startActivity(i); 

     } 
     private void showAlert() { 

      AlertDialog alertDialog = new AlertDialog.Builder(new ContextThemeWrapper(this,R.style.AppTheme)).create(); 
      alertDialog.setTitle("Alert"); 
      alertDialog.setCancelable(false); 
      alertDialog.setIcon(R.drawable.icon); 
      alertDialog.setMessage("App needs to access required Permissions !"); 
      alertDialog.setButton(AlertDialog.BUTTON_NEGATIVE, "Don't Allow", 
        new DialogInterface.OnClickListener() { 
         public void onClick(DialogInterface dialog, int which) { 
          quit(); 

          dialog.dismiss(); 
          finish(); 
         } 
        }); 
      alertDialog.setButton(AlertDialog.BUTTON_POSITIVE, "Allow", 
        new DialogInterface.OnClickListener() { 
         public void onClick(DialogInterface dialog, int which) { 
          boolean showRationale = shouldShowRequestPermissionRationale(Manifest.permission.CAMERA); 
          if((! showRationale)) 
          { 
           Log.e("Camera2","Camera2"); 
           showAlert2(); 
          } 
          else if (ContextCompat.checkSelfPermission(PermissionCamera.this, 
            Manifest.permission.CAMERA) 
            != PackageManager.PERMISSION_GRANTED) { 
           ActivityCompat.requestPermissions(PermissionCamera.this, 
             new String[]{Manifest.permission.CAMERA}, 
             3); 
           dialog.dismiss(); 
          } 
         } 
        }); 

      alertDialog.show(); 



    } 
     private void showAlert2() { 
      final String package_app="com.gwnt.flashlight.alert.call.and.sms"; 
      AlertDialog alertDialog = new AlertDialog.Builder(new ContextThemeWrapper(this,R.style.AppTheme)).create(); 
      alertDialog.setTitle("Alert"); 
      alertDialog.setCancelable(false); 
      alertDialog.setIcon(R.drawable.icon); 
      alertDialog.setMessage("You have checked 'Never ask again' enable permission manually!"); 

      alertDialog.setButton(AlertDialog.BUTTON_POSITIVE, "Quit", 
        new DialogInterface.OnClickListener() { 
         public void onClick(DialogInterface dialog, int which) { 
          quit(); 

         } 
        }); 
      alertDialog.setButton(AlertDialog.BUTTON_NEGATIVE, "Settings", 
        new DialogInterface.OnClickListener() { 
         public void onClick(DialogInterface dialog, int which) { 
          Intent intent = new Intent(); 
          intent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS); 
          Uri uri = Uri.fromParts("package", package_app, null); 
          intent.setData(uri); 
          intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
          startActivity(intent); 
          // quit(); 
          dialog.dismiss(); 
         } 
        }); 

      alertDialog.show(); 



     } 
     public void quit() 
     { 
      Intent startMain = new Intent(Intent.ACTION_MAIN); 
      startMain.addCategory(Intent.CATEGORY_HOME); 
      startMain.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
      startActivity(startMain); 
      finish(); 
      PermissionCamera.this.finish(); 
      System.exit(0); 

     } 
    } 

個PermissionSms.Java

import android.Manifest; 
import android.app.Activity; 
import android.content.DialogInterface; 
import android.content.Intent; 
import android.content.pm.PackageManager; 
import android.net.Uri; 
import android.provider.Settings; 
import android.support.v4.app.ActivityCompat; 
import android.support.v4.content.ContextCompat; 
import android.support.v7.app.AlertDialog; 
import android.support.v7.app.AppCompatActivity; 
import android.os.Bundle; 
import android.support.v7.internal.view.ContextThemeWrapper; 
import android.view.Window; 
import android.view.WindowManager; 

import java.security.Permission; 

public class PermissionSms extends Activity { 
    static int count=0; 


    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     requestWindowFeature(Window.FEATURE_NO_TITLE); 
     getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, 
       WindowManager.LayoutParams.FLAG_FULLSCREEN); 
     requestWindowFeature(Window.FEATURE_NO_TITLE); 
     setContentView(R.layout.activity_permissioncalls); 

     if (ContextCompat.checkSelfPermission(this, 
       Manifest.permission.RECEIVE_SMS) 
       != PackageManager.PERMISSION_GRANTED) { 
      ActivityCompat.requestPermissions(PermissionSms.this, 
        new String[]{Manifest.permission.RECEIVE_SMS}, 
        1); 
     } else { 
      start_app(); 

     } 
    } 

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

      case 1: { 

       // If request is cancelled, the result arrays are empty. 
       if (grantResults.length > 0 
         && grantResults[0] == PackageManager.PERMISSION_GRANTED) { 
        count+=1; 
        if(count==1) 
        { 

         start_app(); 


        } 
        // permission was granted, yay! Do the 
        // contacts-related task you need to do. 
       } else { 
         showAlert(); 
        // permission denied, boo! Disable the 
        // functionality that depends on this permission. 

       } 
       return; 
      } 
      // other 'case' lines to check for other 
      // permissions this app might request 
     } 
    } 
    public void start_app() 
    { 
     Intent i=new Intent(this,PermisiionCall.class); 
     startActivity(i); 

    } 
    private void showAlert() { 
     AlertDialog alertDialog = new AlertDialog.Builder(new ContextThemeWrapper(this,R.style.AppTheme)).create(); 
     alertDialog.setTitle("Alert!!"); 
     alertDialog.setCancelable(false); 
     alertDialog.setIcon(R.drawable.icon); 
     alertDialog.setMessage("App needs to access required Permissions !"); 
     alertDialog.setButton(AlertDialog.BUTTON_NEGATIVE, "Don't Allow", 
       new DialogInterface.OnClickListener() { 
        public void onClick(DialogInterface dialog, int which) { 
         quit(); 
         dialog.dismiss(); 
         finish(); 
        } 
       }); 
     alertDialog.setButton(AlertDialog.BUTTON_POSITIVE, "Allow", 
       new DialogInterface.OnClickListener() { 
        public void onClick(DialogInterface dialog, int which) { 
         boolean showRationale = shouldShowRequestPermissionRationale(Manifest.permission.RECEIVE_SMS); 
         if((! showRationale)) 
         { 
          showAlert2(); 
         } 
         if (ContextCompat.checkSelfPermission(PermissionSms.this, 
           Manifest.permission.RECEIVE_SMS) 
           != PackageManager.PERMISSION_GRANTED) { 
          ActivityCompat.requestPermissions(PermissionSms.this, 
            new String[]{Manifest.permission.RECEIVE_SMS}, 
            1); 
          dialog.dismiss(); 
         } 
        } 
       }); 

     alertDialog.show(); 



    } 
    private void showAlert2() { 
     final String package_app="com.gwnt.flashlight.alert.call.and.sms"; 
     AlertDialog alertDialog = new AlertDialog.Builder(new ContextThemeWrapper(this,R.style.AppTheme)).create(); 
     alertDialog.setTitle("Alert"); 
     alertDialog.setCancelable(false); 
     alertDialog.setIcon(R.drawable.icon); 
     alertDialog.setMessage("You have checked 'Never ask again' enable permission manually!"); 

     alertDialog.setButton(AlertDialog.BUTTON_POSITIVE, "Quit", 
       new DialogInterface.OnClickListener() { 
        public void onClick(DialogInterface dialog, int which) { 
         quit(); 

        } 
       }); 
     alertDialog.setButton(AlertDialog.BUTTON_NEGATIVE, "Settings", 
       new DialogInterface.OnClickListener() { 
        public void onClick(DialogInterface dialog, int which) { 
         Intent intent = new Intent(); 
         intent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS); 
         Uri uri = Uri.fromParts("package", package_app, null); 
         intent.setData(uri); 
         intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
         startActivity(intent); 
         // quit(); 
         dialog.dismiss(); 
        } 
       }); 

     alertDialog.show(); 



    } 
    public void quit() 
    { 
     Intent startMain = new Intent(Intent.ACTION_MAIN); 
     startMain.addCategory(Intent.CATEGORY_HOME); 
     startMain.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
     startActivity(startMain); 
     finish(); 
     PermissionSms.this.finish(); 
     System.exit(0); 

    } 
} 

PermissionReceivePhone。java的

package com.gwnt.flashlight.alert.call.and.sms; 

import android.Manifest; 
import android.app.Activity; 
import android.content.DialogInterface; 
import android.content.Intent; 
import android.content.pm.PackageManager; 
import android.net.Uri; 
import android.provider.Settings; 
import android.support.v4.app.ActivityCompat; 
import android.support.v4.content.ContextCompat; 
import android.support.v7.app.AlertDialog; 
import android.support.v7.app.AppCompatActivity; 
import android.os.Bundle; 
import android.support.v7.internal.view.ContextThemeWrapper; 
import android.util.Log; 
import android.view.Window; 
import android.view.WindowManager; 

public class PermisiionCall extends Activity { 
    static int count=0; 


    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     requestWindowFeature(Window.FEATURE_NO_TITLE); 
     getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, 
       WindowManager.LayoutParams.FLAG_FULLSCREEN); 
     requestWindowFeature(Window.FEATURE_NO_TITLE); 
     setContentView(R.layout.activity_permissioncalls); 

     if (ContextCompat.checkSelfPermission(this, 
       Manifest.permission.READ_PHONE_STATE) 
       != PackageManager.PERMISSION_GRANTED) { 
      ActivityCompat.requestPermissions(PermisiionCall.this, 
        new String[]{Manifest.permission.READ_PHONE_STATE}, 
        2); 
     } else { 
      start_app(); 

     } 
    } 

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

      case 2: { 

       // If request is cancelled, the result arrays are empty. 
       if (grantResults.length > 0 
         && grantResults[0] == PackageManager.PERMISSION_GRANTED) { 
        count+=1; 
        if(count==1) 
        { 

         start_app(); 


        } 
        // permission was granted, yay! Do the 
        // contacts-related task you need to do. 
       } else { 

        // permission denied, boo! Disable the 
        // functionality that depends on this permission. 
        showAlert(); 

       } 
       return; 
      } 
      // other 'case' lines to check for other 
      // permissions this app might request 
     } 
    } 
    public void start_app() 
    { 
     Intent i=new Intent(this,SplashScreen.class); 
     startActivity(i); 

    } 
    private void showAlert() { 
     AlertDialog alertDialog = new AlertDialog.Builder(new ContextThemeWrapper(this,R.style.AppTheme)).create(); 
     alertDialog.setTitle("Alert!!"); 
     alertDialog.setCancelable(false); 
     alertDialog.setIcon(R.drawable.icon); 
     alertDialog.setMessage("App needs to access required Permissions !"); 
     alertDialog.setButton(AlertDialog.BUTTON_NEGATIVE, "Don't Allow", 
       new DialogInterface.OnClickListener() { 
        public void onClick(DialogInterface dialog, int which) { 
         quit(); 

         dialog.dismiss(); 
         finish(); 
        } 
       }); 
     alertDialog.setButton(AlertDialog.BUTTON_POSITIVE, "Allow", 
       new DialogInterface.OnClickListener() { 
        public void onClick(DialogInterface dialog, int which) { 
         boolean showRationale = shouldShowRequestPermissionRationale(Manifest.permission.READ_PHONE_STATE); 
         if((! showRationale)) 
         { 
          showAlert2(); 
         } 
         else if (ContextCompat.checkSelfPermission(PermisiionCall.this, 
           Manifest.permission.READ_PHONE_STATE) 
           != PackageManager.PERMISSION_GRANTED) { 
          ActivityCompat.requestPermissions(PermisiionCall.this, 
            new String[]{Manifest.permission.READ_PHONE_STATE}, 
            2); 
          dialog.dismiss(); 
         } 
        } 
       }); 

     alertDialog.show(); 



    } 
    private void showAlert2() { 
     final String package_app="com.gwnt.flashlight.alert.call.and.sms"; 
     AlertDialog alertDialog = new AlertDialog.Builder(new ContextThemeWrapper(this,R.style.AppTheme)).create(); 
     alertDialog.setTitle("Alert"); 
     alertDialog.setCancelable(false); 
     alertDialog.setIcon(R.drawable.icon); 
     alertDialog.setMessage("You have checked 'Never ask again' enable permission manually!"); 

     alertDialog.setButton(AlertDialog.BUTTON_POSITIVE, "Quit", 
       new DialogInterface.OnClickListener() { 
        public void onClick(DialogInterface dialog, int which) { 
         quit(); 

        } 
       }); 
     alertDialog.setButton(AlertDialog.BUTTON_NEGATIVE, "Settings", 
       new DialogInterface.OnClickListener() { 
        public void onClick(DialogInterface dialog, int which) { 
         Intent intent = new Intent(); 
         intent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS); 
         Uri uri = Uri.fromParts("package", package_app, null); 
         intent.setData(uri); 
         intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
         startActivity(intent); 
         // quit(); 
         dialog.dismiss(); 
        } 
       }); 

     alertDialog.show(); 



    } 
    public void quit() 
    { 
     Intent startMain = new Intent(Intent.ACTION_MAIN); 
     startMain.addCategory(Intent.CATEGORY_HOME); 
     startMain.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
     startActivity(startMain); 
     finish(); 
     PermisiionCall.this.finish(); 
     System.exit(0); 

    } 
} 

它會先問相機許可,如果允許的話它會去短信許可,如果允許的話它會去電話

的startApp是功能移動到下一個活動,如果允許
ie camera-> sms-> phone->功能

0

這是因爲您正在檢查權限if-else if statements其中有效唯一一個滿足的條件可以同時執行:

if (true) { 
    // only this one is executed 
} else if (true) { 
    // does not execute 
} else if (true) { 
    // does not execute 
} 

此外,我會建議簡化你的代碼,因爲你的問題是過於複雜的影響。 下面是你應該如何要求多個權限的例子: Android 6.0 multiple permissions

//編輯:重讀你的問題後,我注意到你還問到設備和仿真器之間的不同的行爲。這可能是因爲你在兩種環境中都有不同的保存偏好和偏好。請移除設備和模擬器上的應用數據並重置應用權限,然後再次檢查。

相關問題