本文整理汇总了Java中android.support.v4.content.ContextCompat.checkSelfPermission方法的典型用法代码示例。如果您正苦于以下问题:Java ContextCompat.checkSelfPermission方法的具体用法?Java ContextCompat.checkSelfPermission怎么用?Java ContextCompat.checkSelfPermission使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类android.support.v4.content.ContextCompat
的用法示例。
在下文中一共展示了ContextCompat.checkSelfPermission方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: requestRuntimePermission
import android.support.v4.content.ContextCompat; //导入方法依赖的package包/类
public static void requestRuntimePermission(Activity activity, String[] permissions,
OnRequestPermissionsListener listener) {
Activity topActivity = activity;
if (null == topActivity) {
return;
}
mListener = listener;
List<String> permissionList = new ArrayList<>();
for (String permission : permissions) {
if (ContextCompat.checkSelfPermission(topActivity, permission)
!= PackageManager.PERMISSION_GRANTED) {
permissionList.add(permission);
}
}
if (!permissionList.isEmpty()) {
ActivityCompat.requestPermissions(topActivity,
permissionList.toArray(new String[permissionList.size()]),
PERMISSION_REQUEST_CODE);
} else {
mListener.onGranted();
}
}
示例2: takePhoto
import android.support.v4.content.ContextCompat; //导入方法依赖的package包/类
/**
* 拍照
*/
private void takePhoto() {
//在每次拍照做权限的判断,防止出现权限不全而获取不到图像
if (ContextCompat.checkSelfPermission(activity,
Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED
|| ContextCompat.checkSelfPermission(activity,
Manifest.permission.CAMERA)
!= PackageManager.PERMISSION_GRANTED) {
return;
}
Intent takeIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (Build.VERSION.SDK_INT>=Build.VERSION_CODES.N){
photoUri = get24MediaFileUri(TYPE_TAKE_PHOTO);
takeIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
}else {
photoUri = getMediaFileUri(TYPE_TAKE_PHOTO);
}
takeIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoUri);
activity.startActivityForResult(takeIntent, CODE_TAKE_PHOTO);
}
示例3: getPermission
import android.support.v4.content.ContextCompat; //导入方法依赖的package包/类
private void getPermission() {
if (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
// Should we show an explanation?
if (ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, MY_PERMISSIONS_REQUEST_WRITE_EXTERNAL_STORAGE);
// Show an expanation to the user *asynchronously* -- don't block
// this thread waiting for the user's response! After the user
// sees the explanation, try again to request the permission.
} else {
// No explanation needed, we can request the permission.
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, MY_PERMISSIONS_REQUEST_WRITE_EXTERNAL_STORAGE);
// MY_PERMISSIONS_REQUEST_READ_CONTACTS is an
// app-defined int constant. The callback method gets the
// result of the request.
}
} else {
}
}
示例4: connectToBluetooth
import android.support.v4.content.ContextCompat; //导入方法依赖的package包/类
/**
* connect to the bluetooth
*
* @param deviceInfo
*/
private void connectToBluetooth(final DeviceInfoBean deviceInfo) {
try {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
switch (ContextCompat.checkSelfPermission(getBaseContext(), Manifest.permission.ACCESS_COARSE_LOCATION)) {
case PackageManager.PERMISSION_DENIED:
performPermissionDeniedAction(deviceInfo);
break;
case PackageManager.PERMISSION_GRANTED:
startDeviceConnection(deviceInfo, healthCareReceiver);
break;
default:
break;
}
} else {
startDeviceConnection(deviceInfo, healthCareReceiver);
}
} catch (RuntimeException e) {
showToastMessage(e.getMessage(), mContext);
Log.e(AppUtility.TAG, Log.getStackTraceString(e));
}
}
示例5: onCreate
import android.support.v4.content.ContextCompat; //导入方法依赖的package包/类
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (!SharedPreferenceHelper.getToken().isEmpty()) {
Intent intent = new Intent(RegisterActivity.this, MainActivity.class);
startActivity(intent);
finish();
} else {
setContentView(R.layout.activity_register);
ButterKnife.bind(this);
FormatWatcher formatWatcher = new MaskFormatWatcher(
MaskImpl.createTerminated(PredefinedSlots.RUS_PHONE_NUMBER) // маска для серии и номера
);
formatWatcher.installOn(phoneEditText);
if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this,
new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, 0);
}
}
}
示例6: permissionsRequest
import android.support.v4.content.ContextCompat; //导入方法依赖的package包/类
public void permissionsRequest() {
if (ContextCompat.checkSelfPermission(this,
Manifest.permission.INTERNET)
!= PackageManager.PERMISSION_GRANTED || ContextCompat.checkSelfPermission(this,
Manifest.permission.WRITE_EXTERNAL_STORAGE)
!= PackageManager.PERMISSION_GRANTED || ContextCompat.checkSelfPermission(this,
Manifest.permission.READ_EXTERNAL_STORAGE)
!= PackageManager.PERMISSION_GRANTED ||ContextCompat.checkSelfPermission(this,
Manifest.permission.CAMERA)
!= PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this,
new String[]{Manifest.permission.INTERNET, Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.CAMERA}, 111);
}
}
示例7: downloadVideo
import android.support.v4.content.ContextCompat; //导入方法依赖的package包/类
public static long downloadVideo(Context context, String url) {
if (ContextCompat.checkSelfPermission(context,
Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
EventBus.getDefault().post(new EventPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE));
return -1;
}
DownloadManager manager = (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE);
if (url != null) {
Uri uri = Uri.parse(url);
DownloadManager.Request request = new DownloadManager.Request(uri);
File dst = FileUtils.createVideoFile(uri);
if (dst.exists()) {
Toast.makeText(context, R.string.video_exist, Toast.LENGTH_SHORT).show();
} else {
request.setDestinationUri(Uri.fromFile(dst));
request.allowScanningByMediaScanner();
long id = manager.enqueue(request);
Log.d("download id", "-->" + id);
return id;
}
}
return -1;
}
示例8: onCreate
import android.support.v4.content.ContextCompat; //导入方法依赖的package包/类
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splashscreen_main);
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
//marshmallow check permission permissions
boolean hasPermission = (ContextCompat.checkSelfPermission(this,
Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED);
if (!hasPermission) {
ActivityCompat.requestPermissions(this,
new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},
REQUEST_WRITE_STORAGE);
} else {
validateFilesAndDownload();
}
}
示例9: checkStorageAccess
import android.support.v4.content.ContextCompat; //导入方法依赖的package包/类
private boolean checkStorageAccess(boolean yay) {
if (ContextCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this,
new String[]{Manifest.permission.READ_EXTERNAL_STORAGE},
yay? REQUEST_READ_EXTERNAL_STORAGE : REQUEST_READ_EXTERNAL_STORAGE_NOYAY);
return false;
}
return true;
}
示例10: hasAudioRecordPermissions
import android.support.v4.content.ContextCompat; //导入方法依赖的package包/类
private boolean hasAudioRecordPermissions() {
final Context app = getApplicationContext();
int recordPermission = ContextCompat.checkSelfPermission(app, Manifest.permission.RECORD_AUDIO);
int writePermission = ContextCompat.checkSelfPermission(app, Manifest.permission.WRITE_EXTERNAL_STORAGE);
return recordPermission == PackageManager.PERMISSION_GRANTED && writePermission == PackageManager.PERMISSION_GRANTED;
}
示例11: onCreate
import android.support.v4.content.ContextCompat; //导入方法依赖的package包/类
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mLocationClient = new LocationClient(getApplicationContext());
mLocationClient.registerLocationListener(new MyLocationListener());
SDKInitializer.initialize(getApplicationContext());
setContentView(R.layout.activity_main);
mapView = (MapView) findViewById(R.id.bmapView);
baiduMap = mapView.getMap();
baiduMap.setMyLocationEnabled(true);
positionText = (TextView) findViewById(R.id.position_text_view);
List<String> permissionList = new ArrayList<>();
if (ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED){
permissionList.add(Manifest.permission.ACCESS_FINE_LOCATION);
}
if (ContextCompat.checkSelfPermission(MainActivity.this,Manifest.permission.READ_PHONE_STATE) != PackageManager.PERMISSION_GRANTED){
permissionList.add(Manifest.permission.READ_PHONE_STATE);
}
if (ContextCompat.checkSelfPermission(MainActivity.this,Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED){
permissionList.add(Manifest.permission.WRITE_EXTERNAL_STORAGE);
}
if (!permissionList.isEmpty()){
String [] permissions = permissionList.toArray(new String[permissionList.size()]);
ActivityCompat.requestPermissions(MainActivity.this,permissions,1);
}else {
requestLocation();
}
}
示例12: start
import android.support.v4.content.ContextCompat; //导入方法依赖的package包/类
/**
* 用于启动扫码的Activity,启动之前要做权限判断。
*/
@LuaViewApi
public void start() {
if (ContextCompat.checkSelfPermission(mActivity, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {//还没有授予权限
if (ActivityCompat.shouldShowRequestPermissionRationale(mActivity, Manifest.permission.CAMERA)) {
Toast.makeText(mActivity, "Please Allow the Camera permission.", Toast.LENGTH_SHORT).show();
} else {
ActivityCompat.requestPermissions(mActivity, new String[]{Manifest.permission.CAMERA}, MainActivity.REQUEST_CAMERA);
}
} else {
mActivity.startActivityForResult(new Intent(mActivity , CaptureActivity.class) , 0);
}
}
示例13: ensureExternalStoragePermissionGranted
import android.support.v4.content.ContextCompat; //导入方法依赖的package包/类
private boolean ensureExternalStoragePermissionGranted() {
if (ContextCompat.checkSelfPermission(getContext(), Manifest.permission.READ_EXTERNAL_STORAGE)
!= PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(
getActivity(),
new String[]{Manifest.permission.READ_EXTERNAL_STORAGE},
PERMISSION_REQUEST_READ_EXTERNAL_STORAGE);
return false;
}
return true;
}
示例14: notHasPermission
import android.support.v4.content.ContextCompat; //导入方法依赖的package包/类
private String[] notHasPermission(Context context, String[] permissions) {
ArrayList<String> notPermissions = new ArrayList<>();
for (String permission : permissions) {
try {
if (ContextCompat.checkSelfPermission(context, permission) != PackageManager.PERMISSION_GRANTED) {
notPermissions.add(permission);
}
} catch (Throwable t) {
Log.e(String.format("Failure checking permission %s", permission), t.getMessage());
}
}
return notPermissions.toArray(new String[]{});
}
示例15: setupLocationClient
import android.support.v4.content.ContextCompat; //导入方法依赖的package包/类
public void setupLocationClient(Context context) {
if(locationClient == null) {
if(ContextCompat.checkSelfPermission(context, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
locationClient = LocationServices.getFusedLocationProviderClient(context);
}
}
}