Android权限启动画面错误API 19

时间:2017-05-01 16:25:59

标签: android splash-screen android-permissions

在我的应用上,我已经为Android 6和7实施了控制权限。 当app运行时显示带有消息的启动画面,并在允许后启动主要活动。在Android 6和7上工作正常,但在Android 19/20上,当我运行应用程序时显示消息,但应用程序在启动屏幕上仍然被阻止,并且不会启动主要活动。 这是我的代码:

public class splashscreen extends Activity implements GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener{
private GoogleApiClient googleApiClient;
private Location myLocation;
private static final int MY_PERMISSIONS_REQUEST_GET_ACCOUNTS = 1;
private SparseIntArray mErrorString;




@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.splash);
    mErrorString = new SparseIntArray();







    GoogleApiAvailability api = GoogleApiAvailability.getInstance();
    int code = api.isGooglePlayServicesAvailable(this);
    if (code != ConnectionResult.SUCCESS)
    {
        AlertDialog.Builder alert =  new AlertDialog.Builder(this);
        alert.setTitle("Attenzione");
        alert.setMessage("Google Play Services non installati o non aggiornati!");
        alert.setNeutralButton("Ok", new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                dialogInterface.dismiss();
                finish();
            }
        });
        alert.show();
    }

    // Create an instance of GoogleAPIClient.
    if (googleApiClient == null) {
        googleApiClient = new GoogleApiClient.Builder(this)
                .addConnectionCallbacks(this)
                .addOnConnectionFailedListener(this)
                .addApi(LocationServices.API)
                .build();
    }

}






protected void onStart() {
    googleApiClient.connect();
    super.onStart();
}

protected void onStop() {
    googleApiClient.disconnect();
    super.onStop();
}


// Permessi
public void onPermissionsGranted(int requestCode) {

    Toast.makeText(getApplicationContext(), "Permission granted", Toast.LENGTH_SHORT).show();
}

public void requestAppPermissions(final String[]requestedPermissions, final int stringId, final int requestCode) {
    mErrorString.put(requestCode, stringId);

    int permissionCheck = PackageManager.PERMISSION_GRANTED;
    boolean showRequestPermissions = false;
    for(String permission: requestedPermissions) {
        permissionCheck = permissionCheck + ContextCompat.checkSelfPermission(this, permission);
        showRequestPermissions = showRequestPermissions || ActivityCompat.shouldShowRequestPermissionRationale(this, permission);
    }

    if (permissionCheck!=PackageManager.PERMISSION_GRANTED) {
        if(showRequestPermissions) {
            Snackbar.make(findViewById(android.R.id.content), stringId, Snackbar.LENGTH_INDEFINITE).setAction("GRANT", new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    ActivityCompat.requestPermissions(splashscreen.this, requestedPermissions, requestCode);
                }
            }).show();
        } else {

            ActivityCompat.requestPermissions(this, requestedPermissions, requestCode);
        }
    } else {
        onPermissionsGranted(requestCode);
    }
}

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

        case 1: {

            if (grantResults.length > 0
                    && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                //Toast.makeText(getApplicationContext(), "Permission granted", Toast.LENGTH_SHORT).show();
                Intent i = new Intent(splashscreen.this, MainActivity.class); //start activity
                splashscreen.this.startActivity(i);
            } else {
                Intent i = new Intent();
                i.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
                i.setData(Uri.parse("package:" + getPackageName()));
                i.addCategory(Intent.CATEGORY_DEFAULT);
                i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                i.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
                i.addFlags(Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
                startActivity(i);
                /*Toast.makeText(getApplicationContext(), "Permission denied", Toast.LENGTH_SHORT).show();
                Snackbar.make(findViewById(android.R.id.content), mErrorString.get(requestCode),
                        Snackbar.LENGTH_INDEFINITE).setAction("ENABLE", new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Intent i = new Intent();
                        i.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
                        i.setData(Uri.parse("package:" + getPackageName()));
                        i.addCategory(Intent.CATEGORY_DEFAULT);
                        i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                        i.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
                        i.addFlags(Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
                        startActivity(i);
                    }
                }).show();*/
            }
            return;
        }

        // other 'case' lines to check for other
        // permissions this app might request
    }
}


@Override
public void onConnected(Bundle connectionHint) {
    if (ContextCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
        myLocation = LocationServices.FusedLocationApi.getLastLocation(googleApiClient);
    }
    SharedPreferences getPrefs = PreferenceManager.getDefaultSharedPreferences(getBaseContext());

    boolean isFirstStart = getPrefs.getBoolean("firstStart", true);
    if (isFirstStart) {
        android.app.AlertDialog alertDialog = new android.app.AlertDialog.Builder(splashscreen.this).create();
        alertDialog.setTitle(getResources().getString(R.string.titolo_intro_sms));
        alertDialog.setMessage(getResources().getString(R.string.intro_sms));
        alertDialog.setButton(android.app.AlertDialog.BUTTON_NEUTRAL, "OK",
                new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        ActivityCompat.requestPermissions(splashscreen.this, new String[]{Manifest.permission.ACCESS_COARSE_LOCATION}, 1);
                        //Intent i = new Intent(splashscreen.this, MainActivity.class);
                        //startActivity(i);

                    }
                });
        alertDialog.show();

        SharedPreferences.Editor e = getPrefs.edit();
        e.putBoolean("firstStart", false);
        e.apply();
    } else {

        startApp();
    }
}

private void startApp() {

    if(googleApiClient != null) {
        googleApiClient.disconnect();
    }
    startActivity(new Intent(splashscreen.this, MainActivity.class));

    finish();
}

@Override
public void onConnectionSuspended(int i) {
    googleApiClient.connect();
}

@Override
public void onConnectionFailed(ConnectionResult connectionResult) {
}

}

1 个答案:

答案 0 :(得分:3)

发生这种情况是因为如果在未请求权限的情况下授予权限,则不会启动Activity。在api 23之前,此检查失败:

if (permissionCheck!=PackageManager.PERMISSION_GRANTED) {
    // Not called before api 23 unless the permissions are not specified in the manifest.
}

因此,您必须修改方法onPermissionsGranted(),如下所示:

public void onPermissionsGranted(int requestCode) {
    Toast.makeText(getApplicationContext(), "Permission granted", Toast.LENGTH_SHORT).show();
    // Start the Activity if permissions are granted.
    startActivity(new Intent(this, MainActivity.class));
}