Java Code Examples for android.support.v4.content.PermissionChecker#checkSelfPermission()

The following examples show how to use android.support.v4.content.PermissionChecker#checkSelfPermission() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: CaptureActivity.java    From ScanZbar with Apache License 2.0 6 votes vote down vote up
private void checkPermissionCamera() {
    int checkPermission = 0;
    if (Build.VERSION.SDK_INT >= 23) {
        // checkPermission =ContextCompat.checkSelfPermission(this,Manifest.permission.CAMERA);
        checkPermission = PermissionChecker.checkSelfPermission(this, Manifest.permission.CAMERA);
        if (checkPermission != PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(this,
                    new String[]{Manifest.permission.CAMERA},
                    MY_PERMISSIONS_REQUEST_CAMERA);
        } else {
            isOpenCamera = true;
        }

    } else {
        checkPermission = checkPermission(26);
        if (checkPermission == AppOpsManager.MODE_ALLOWED) {
            isOpenCamera = true;
        } else if (checkPermission == AppOpsManager.MODE_IGNORED) {
            isOpenCamera = false;
            displayFrameworkBugMessageAndExit();
        }
    }
}
 
Example 2
Source File: Activity_Main.java    From Pedometer with Apache License 2.0 6 votes vote down vote up
@Override
protected void onCreate(final Bundle b) {
    super.onCreate(b);
    startService(new Intent(this, SensorListener.class));
    if (b == null) {
        // Create new fragment and transaction
        Fragment newFragment = new Fragment_Overview();
        FragmentTransaction transaction = getFragmentManager().beginTransaction();

        // Replace whatever is in the fragment_container view with this
        // fragment,
        // and add the transaction to the back stack
        transaction.replace(android.R.id.content, newFragment);

        // Commit the transaction
        transaction.commit();
    }

    if (BuildConfig.DEBUG && Build.VERSION.SDK_INT >= 23 && PermissionChecker
            .checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) !=
            PermissionChecker.PERMISSION_GRANTED) {
        requestPermissions(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 0);
    }
}
 
Example 3
Source File: PermissionManager.java    From Musicoco with Apache License 2.0 6 votes vote down vote up
public boolean checkPermission(Context context, String... permission) {

        boolean nr = true;

        for (int i = 0; i < permission.length; i++) {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                // targetSdkVersion >= Android M, we can
                // use Context#checkSelfPermission
                nr = context.checkSelfPermission(permission[i])
                        == PackageManager.PERMISSION_GRANTED;
            } else {
                // targetSdkVersion < Android M, we have to use PermissionChecker
                nr = PermissionChecker.checkSelfPermission(context, permission[i])
                        == PermissionChecker.PERMISSION_GRANTED;
            }

            if (!nr) {
                break;
            }
        }
        return nr;
    }
 
Example 4
Source File: EmuCheckUtil.java    From CacheEmulatorChecker with Apache License 2.0 6 votes vote down vote up
public static boolean checkPermissionGranted(Context context, String permission) {

        boolean result = true;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            try {
                final PackageInfo info = context.getPackageManager().getPackageInfo(
                        context.getPackageName(), 0);
                int targetSdkVersion = info.applicationInfo.targetSdkVersion;
                if (targetSdkVersion >= Build.VERSION_CODES.M) {
                    result = context.checkSelfPermission(permission)
                            == PackageManager.PERMISSION_GRANTED;
                } else {
                    result = PermissionChecker.checkSelfPermission(context, permission)
                            == PermissionChecker.PERMISSION_GRANTED;
                }
            } catch (Exception e) {
            }
        }
        return result;
    }
 
Example 5
Source File: Main.java    From PictureChooser with Apache License 2.0 6 votes vote down vote up
private void checkPermission(boolean askForPermission) {
    if (Build.VERSION.SDK_INT >= 23 && PermissionChecker
            .checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE) !=
            PermissionChecker.PERMISSION_GRANTED && PermissionChecker
            .checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) !=
            PermissionChecker.PERMISSION_GRANTED) {
        if (askForPermission) {
            requestPermissions(new String[]{Manifest.permission.READ_EXTERNAL_STORAGE,
                    Manifest.permission.WRITE_EXTERNAL_STORAGE}, REQUEST_STORAGE_PERMISSION);
        } else {
            finish();
        }
    } else {
        start();
    }
}
 
Example 6
Source File: LocationFragment.java    From android with Apache License 2.0 6 votes vote down vote up
protected boolean requestLocationPermissions() {
    int permission = PermissionChecker.checkSelfPermission(getActivity(),
            Manifest.permission.ACCESS_FINE_LOCATION);

    if (permission == PermissionChecker.PERMISSION_GRANTED) {
        return true;
    }

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        requestPermissions(new String[] {
                Manifest.permission.ACCESS_COARSE_LOCATION,
                Manifest.permission.ACCESS_FINE_LOCATION
        }, DEFAULT_PERMISSIONS_REQUEST_CODE);

        return false;
    }

    return true;
}
 
Example 7
Source File: AcpService.java    From SprintNBA with Apache License 2.0 6 votes vote down vote up
/**
 * 检查权限授权状态
 *
 * @param context
 * @param permission
 * @return
 */
int checkSelfPermission(Context context, String permission) {
    try {
        final PackageInfo info = context.getPackageManager().getPackageInfo(
                context.getPackageName(), 0);
        int targetSdkVersion = info.applicationInfo.targetSdkVersion;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            if (targetSdkVersion >= Build.VERSION_CODES.M) {
                Log.i(TAG, "targetSdkVersion >= Build.VERSION_CODES.M");
                return ContextCompat.checkSelfPermission(context, permission);
            } else {
                return PermissionChecker.checkSelfPermission(context, permission);
            }
        }
    } catch (PackageManager.NameNotFoundException e) {
        e.printStackTrace();
    }
    return ContextCompat.checkSelfPermission(context, permission);
}
 
Example 8
Source File: LocationServiceImpl.java    From Cangol-appcore with Apache License 2.0 6 votes vote down vote up
private boolean checkLocationPermission(Activity activity) {
    List<String> list = new ArrayList<>();
    if (PermissionChecker.checkSelfPermission(activity, Manifest.permission.ACCESS_FINE_LOCATION) != PermissionChecker.PERMISSION_GRANTED) {
        list.add(Manifest.permission.ACCESS_FINE_LOCATION);
    } else {
        Log.e(TAG, "requestLocation need Permission " + Manifest.permission.ACCESS_FINE_LOCATION);
    }
    if (PermissionChecker.checkSelfPermission(activity, Manifest.permission.ACCESS_COARSE_LOCATION) != PermissionChecker.PERMISSION_GRANTED) {
        list.add(Manifest.permission.ACCESS_COARSE_LOCATION);
    } else {
        Log.e(TAG, "requestLocation  NETWORK_PROVIDER is disabled ");
    }
    if (!list.isEmpty()) {
        Log.e(TAG, "requestLocation need Permission " + list.toString());
        String[] permissions = new String[list.size()];
        list.toArray(permissions);
        mBetterLocationListener.needPermission(permissions);
        return false;
    }
    return true;
}
 
Example 9
Source File: CaptureActivity.java    From ZbarCode with Apache License 2.0 6 votes vote down vote up
private void checkPermissionCamera() {
    int checkPermission = 0;
    if (Build.VERSION.SDK_INT >= 23) {
        // checkPermission =ContextCompat.checkSelfPermission(this,Manifest.permission.CAMERA);
        checkPermission = PermissionChecker.checkSelfPermission(this, Manifest.permission.CAMERA);
        if (checkPermission != PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(this,
                    new String[]{Manifest.permission.CAMERA},
                    MY_PERMISSIONS_REQUEST_CAMERA);
        } else {
            isOpenCamera = true;
        }

    } else {
        checkPermission = checkPermission(26);
        if (checkPermission == AppOpsManager.MODE_ALLOWED) {
            isOpenCamera = true;
        } else if (checkPermission == AppOpsManager.MODE_IGNORED) {
            isOpenCamera = false;
            displayFrameworkBugMessageAndExit();
        }
    }
}
 
Example 10
Source File: Activity_Main.java    From Pedometer with Apache License 2.0 5 votes vote down vote up
@Override
protected void onCreate(final Bundle b) {
    super.onCreate(b);
    if (Build.VERSION.SDK_INT >= 26) {
        API26Wrapper.startForegroundService(this, new Intent(this, SensorListener.class));
    } else {
        startService(new Intent(this, SensorListener.class));
    }
    if (b == null) {
        // Create new fragment and transaction
        Fragment newFragment = new Fragment_Overview();
        FragmentTransaction transaction = getFragmentManager().beginTransaction();

        // Replace whatever is in the fragment_container view with this
        // fragment,
        // and add the transaction to the back stack
        transaction.replace(android.R.id.content, newFragment);

        // Commit the transaction
        transaction.commit();
    }


    GoogleApiClient.Builder builder = new GoogleApiClient.Builder(this, this, this);
    builder.addApi(Games.API, Games.GamesOptions.builder().build());
    builder.addScope(Games.SCOPE_GAMES);
    builder.addApi(Fitness.HISTORY_API);
    builder.addApi(Fitness.RECORDING_API);
    builder.addScope(new Scope(Scopes.FITNESS_ACTIVITY_READ_WRITE));

    mGoogleApiClient = builder.build();

    if (BuildConfig.DEBUG && Build.VERSION.SDK_INT >= 23 && PermissionChecker
            .checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) !=
            PermissionChecker.PERMISSION_GRANTED) {
        requestPermissions(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 0);
    }
}
 
Example 11
Source File: PermissionUtil.java    From imsdk-android with MIT License 5 votes vote down vote up
public static boolean checkAndRequestPermissionsInActivity(Activity cxt, String... checkPermissions) {
    boolean isHas = true;
    List<String> permissions = new ArrayList<>();
    for (String checkPermission : checkPermissions) {
        if (PermissionChecker.checkSelfPermission(cxt, checkPermission) != PackageManager.PERMISSION_GRANTED) {
            isHas = false;
            permissions.add(checkPermission);
        }
    }
    if (!isHas) {
        String[] p = permissions.toArray(new String[permissions.size()]);
        requestPermissionsInActivity(cxt, Code.REQUEST_PERMISSION, p);
    }
    return isHas;
}
 
Example 12
Source File: Map.java    From MapsMeasure with Apache License 2.0 5 votes vote down vote up
private boolean hasLocationPermission() {
    return PermissionChecker
            .checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) ==
            PermissionChecker.PERMISSION_GRANTED && PermissionChecker
            .checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) ==
            PermissionChecker.PERMISSION_GRANTED;
}
 
Example 13
Source File: Map.java    From MapsMeasure with Apache License 2.0 5 votes vote down vote up
@SuppressLint("NewApi")
@Override
public void onCreate(final Bundle savedInstanceState) {
    if (BuildConfig.DEBUG && Build.VERSION.SDK_INT >= 23 && PermissionChecker
            .checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) !=
            PermissionChecker.PERMISSION_GRANTED) {
        requestPermissions(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 0);
    }
    try {
        super.onCreate(savedInstanceState);
    } catch (final BadParcelableException bpe) {
        if (BuildConfig.DEBUG) Logger.log(bpe);
    }
    init();
}
 
Example 14
Source File: BaseActivity.java    From Album with Apache License 2.0 5 votes vote down vote up
private static List<String> getDeniedPermissions(Context context, String... permissions) {
    List<String> deniedList = new ArrayList<>(2);
    for (String permission : permissions) {
        if (PermissionChecker.checkSelfPermission(context, permission) != PermissionChecker.PERMISSION_GRANTED) {
            deniedList.add(permission);
        }
    }
    return deniedList;
}
 
Example 15
Source File: LocationFragment.java    From android with Apache License 2.0 5 votes vote down vote up
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions,
                                       @NonNull int[] grantResults) {
    if (requestCode == DEFAULT_PERMISSIONS_REQUEST_CODE) {
        CheckBoxPreference auto = (CheckBoxPreference) findPreference("location_automatic");

        int permission = PermissionChecker.checkSelfPermission(getActivity(),
                Manifest.permission.ACCESS_FINE_LOCATION);

        auto.setChecked(permission == PermissionChecker.PERMISSION_GRANTED);
    }
}
 
Example 16
Source File: Utils.java    From 365browser with Apache License 2.0 4 votes vote down vote up
public static boolean isBluetoothPermissionGranted() {
    return PermissionChecker.checkSelfPermission(
            ContextUtils.getApplicationContext(), Manifest.permission.BLUETOOTH)
            == PackageManager.PERMISSION_GRANTED;
}
 
Example 17
Source File: Utils.java    From AndroidChromium with Apache License 2.0 4 votes vote down vote up
public static boolean isBluetoothPermissionGranted() {
    return PermissionChecker.checkSelfPermission(
            ContextUtils.getApplicationContext(), Manifest.permission.BLUETOOTH)
            == PackageManager.PERMISSION_GRANTED;
}
 
Example 18
Source File: Utils.java    From delion with Apache License 2.0 4 votes vote down vote up
public static boolean isBluetoothPermissionGranted(Context context) {
    return PermissionChecker.checkSelfPermission(context, Manifest.permission.BLUETOOTH)
            == PackageManager.PERMISSION_GRANTED;
}
 
Example 19
Source File: OverlayViewManager.java    From DebugOverlay-Android with Apache License 2.0 4 votes vote down vote up
public static boolean hasSystemAlertPermission(@NonNull Context context) {
    return PermissionChecker.checkSelfPermission(context, Manifest.permission.SYSTEM_ALERT_WINDOW)
            == PermissionChecker.PERMISSION_GRANTED;
}
 
Example 20
Source File: MainActivity.java    From apkextractor with GNU General Public License v3.0 4 votes vote down vote up
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
    drawerLayout.closeDrawers();
    switch (menuItem.getItemId()){
        default:break;
        case R.id.nav_receive:{
            if(Build.VERSION.SDK_INT>=23&&PermissionChecker.checkSelfPermission(this,Manifest.permission.WRITE_EXTERNAL_STORAGE)!=PermissionChecker.PERMISSION_GRANTED){
                Global.showRequestingWritePermissionSnackBar(this);
                requestPermissions(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},0);
                return false;
            }
            startActivityForResult(new Intent(this,FileReceiveActivity.class),REQUEST_CODE_RECEIVING_FILES);
        }
        break;
        case R.id.nav_settings:{
            startActivityForResult(new Intent(this,SettingActivity.class),REQUEST_CODE_SETTINGS);
        }
        break;
        case R.id.nav_about:{
            View dialogView=LayoutInflater.from(this).inflate(R.layout.dialog_about, null);
            dialogView.findViewById(R.id.layout_about_donate).setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    try{
                        startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("https://qr.alipay.com/FKX08041Y09ZGT6ZT91FA5")));
                    }catch (Exception e){
                        e.printStackTrace();
                    }

                }
            });
            new AlertDialog.Builder(this)
                    .setTitle(EnvironmentUtil.getAppName(this)+"("+EnvironmentUtil.getAppVersionName(this)+")")
                    .setIcon(R.drawable.icon_launcher)
                    .setCancelable(true)
                    .setView(dialogView)
                    .setPositiveButton(getResources().getString(R.string.dialog_button_confirm), new DialogInterface.OnClickListener() {
                        @Override public void onClick(DialogInterface arg0, int arg1) {}
                    }).show();
        }
        break;
    }
    return false;
}