本文整理汇总了Java中android.support.v4.app.ActivityCompat.requestPermissions方法的典型用法代码示例。如果您正苦于以下问题:Java ActivityCompat.requestPermissions方法的具体用法?Java ActivityCompat.requestPermissions怎么用?Java ActivityCompat.requestPermissions使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类android.support.v4.app.ActivityCompat
的用法示例。
在下文中一共展示了ActivityCompat.requestPermissions方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: requestPermissionIfNeeded
import android.support.v4.app.ActivityCompat; //导入方法依赖的package包/类
private static boolean requestPermissionIfNeeded(String permission, Activity activity) {
if (ContextCompat.checkSelfPermission(activity, permission) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(activity, new String[]{permission}, 1);
return true;
}
return false;
}
示例2: show
import android.support.v4.app.ActivityCompat; //导入方法依赖的package包/类
/**
* This method creates and display file picker dialog
*/
private void show() {
if (ActivityCompat.checkSelfPermission(mActivity,
Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(mActivity, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},
STORAGE_PERMISSIONS);
return;
}
AlertDialog.Builder alertBuilder = new AlertDialog.Builder(mActivity);
final AlertDialog alertdialog = alertBuilder.create();
mPickerViewModel.setAlertDialog(alertdialog);
PickerDialogBinding binding = DataBindingUtil.inflate(LayoutInflater.from(mActivity),
R.layout.picker_dialog, null, false);
binding.setVariable(BR.viewModel, mPickerViewModel);
alertdialog.setView(binding.getRoot());
alertdialog.show();
}
示例3: requestRuntimePermission
import android.support.v4.app.ActivityCompat; //导入方法依赖的package包/类
public static void requestRuntimePermission(Activity activity, String[] permissions,
OnRequestPermissionsListener listener) {
if (null == activity) {
return;
}
mListener = listener;
List<String> permissionList = new ArrayList<>();
for (String permission : permissions) {
if (ContextCompat.checkSelfPermission(activity, permission)
!= PackageManager.PERMISSION_GRANTED) {
permissionList.add(permission);
}
}
if (!permissionList.isEmpty()) {
ActivityCompat.requestPermissions(activity,
permissionList.toArray(new String[permissionList.size()]),
PERMISSION_REQUEST_CODE);
} else {
mListener.onGranted();
}
}
示例4: onStart
import android.support.v4.app.ActivityCompat; //导入方法依赖的package包/类
@Override
protected void onStart() {
Log.i(getClass().getName(), "onStart");
super.onStart();
// Ask for permissions
int permissionCheckWriteExternalStorage = ContextCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE);
if (permissionCheckWriteExternalStorage != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, PERMISSION_REQUEST_READ_EXTERNAL_STORAGE);
return;
}
ContentProvider.initializeDb(this);
Intent intent = new Intent(this, WriteNumberActivity.class);
startActivity(intent);
finish();
}
示例5: checkPermission
import android.support.v4.app.ActivityCompat; //导入方法依赖的package包/类
private void checkPermission(int requestCode) {
String permission = null;
if (requestCode == 0) {
permission = Manifest.permission.READ_EXTERNAL_STORAGE;
} else {
permission = Manifest.permission.WRITE_EXTERNAL_STORAGE;
}
if (ContextCompat.checkSelfPermission(this, permission) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this,
new String[]{permission},
requestCode);
} else {
if (requestCode == Constants.READ_EXTERNAL_PERMISSION_REQUEST_CODE) {
checkPermission(Constants.WRITE_EXTERNAL_PERMISSION_REQUEST_CODE);
} else {
saveExpansion();
}
}
}
示例6: onCreate
import android.support.v4.app.ActivityCompat; //导入方法依赖的package包/类
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_audio);
ButterKnife.bind(this);
currentState = Status.START;
myHandler = new Handler();
audioMgr = (AudioManager) getSystemService(AUDIO_SERVICE);
Intent incoming = getIntent();
if (incoming.hasExtra("AUDIO_PATH")) {
audioFilePath = incoming.getStringExtra("AUDIO_PATH");
// audioFilePath = dataUri.getPath();
recordBtn.setEnabled(true);
} else {
recordBtn.setEnabled(false);
}
int currentPerm = ActivityCompat.checkSelfPermission(this,
permArr[0]);
if (currentPerm == PackageManager.PERMISSION_DENIED) {
ActivityCompat.requestPermissions(this, permArr, AUDIO_PERMISSION_REQUEST);
}
}
示例7: onCreate
import android.support.v4.app.ActivityCompat; //导入方法依赖的package包/类
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imageView1 = (ImageView) findViewById(R.id.standard_list_imageview1);
imageView2 = (ImageView) findViewById(R.id.standard_list_imageview2);
imageView3 = (ImageView) findViewById(R.id.standard_list_imageview3);
imageView4 = (ImageView) findViewById(R.id.standard_list_imageview4);
if (ContextCompat.checkSelfPermission(MainActivity.this,
Manifest.permission.READ_EXTERNAL_STORAGE)
!= PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(MainActivity.this,
new String[]{Manifest.permission.READ_EXTERNAL_STORAGE},
100);
}
}
示例8: requestPermissions
import android.support.v4.app.ActivityCompat; //导入方法依赖的package包/类
private void requestPermissions(String... permissions) {
ActivityCompat.requestPermissions(this, permissions, PERMISSION_REQUEST_CODE);
}
示例9: requestStoragePermission
import android.support.v4.app.ActivityCompat; //导入方法依赖的package包/类
/**
* 申请权限 SD卡的读写权限
* @param activity
*/
private void requestStoragePermission(Activity activity){
//检测权限
int permission = ContextCompat.checkSelfPermission(activity.getApplicationContext(), Manifest.permission.WRITE_EXTERNAL_STORAGE);
if (permission!= PackageManager.PERMISSION_GRANTED){
//没有权限 则申请权限 弹出对话框
ActivityCompat.requestPermissions(activity,PERMISSION_STORAGES,REQUEST_CODE_STORAGE);
}
}
示例10: onCreate
import android.support.v4.app.ActivityCompat; //导入方法依赖的package包/类
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_bluetooth_devices);
initializeScreen();
//check if the device has a bluetooth or not
//and show Toast message if it does't have
mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
bluetoothDevicesAdapter = new BluetoothDevicesAdapter(this, bluetoothDevicesNamesList);
if (mBluetoothAdapter == null) {
Toast.makeText(this, R.string.does_not_have_bluetooth, Toast.LENGTH_LONG).show();
finish();
} else if (!mBluetoothAdapter.isEnabled()) {
Intent enableIntentBluetooth = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableIntentBluetooth, REQUEST_ENABLE_BT);
} else if (mBluetoothAdapter.isEnabled()) {
PairedDevicesList();
}
setBroadCastReceiver();
//request location permission for bluetooth scanning for android API 23 and above
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, REQUEST_ENABLE_FINE_LOCATION);
//press the button to start search new Devices
searchForNewDevices.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
searchForNewDevices.setEnabled(false);
bluetoothDevicesAdapter.clear();
PairedDevicesList();
NewDevicesList();
}
});
}
示例11: onCreate
import android.support.v4.app.ActivityCompat; //导入方法依赖的package包/类
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
setContentView(R.layout.activity_main);
if (ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.CAMERA)
!= PackageManager.PERMISSION_GRANTED) {
Log.i("permission", "request CAMERA");
ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.CAMERA}, REQUEST_CAMERA);
}else {
Log.i("permission", "CAMERA already granted");
camera_granted = true;
}
if (ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.READ_EXTERNAL_STORAGE)
!= PackageManager.PERMISSION_GRANTED) {
Log.i("permission", "request EXTERNAL_STROAGE");
ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, REQUEST_EXTERNAL_STROAGE);
}else {
Log.i("permission", "EXTERNAL_STROAGE already granted");
read_external_storage_granted = true;
}
OpenCVLoader.initAsync(OpenCVLoader.OPENCV_VERSION_3_2_0, this, mLoaderCallback);
mOpenCvCameraView = (CameraBridgeViewBase) findViewById(R.id.java_surface_view);
mOpenCvCameraView.setVisibility(SurfaceView.VISIBLE);
mOpenCvCameraView.setCvCameraViewListener(this);
Log.d("OCR", "onCreate done");
}
示例12: performPermissionDeniedAction
import android.support.v4.app.ActivityCompat; //导入方法依赖的package包/类
private void performPermissionDeniedAction(DeviceInfoBean deviceInfo) {
if (ContextCompat.checkSelfPermission(getBaseContext(), Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions((Activity) mContext,
new String[]{Manifest.permission.ACCESS_COARSE_LOCATION},
REQUEST_ACCESS_COARSE_LOCATION);
} else {
startDeviceConnection(deviceInfo, healthCareReceiver);
}
}
示例13: requestPermissionForRecord
import android.support.v4.app.ActivityCompat; //导入方法依赖的package包/类
public void requestPermissionForRecord() {
if (ActivityCompat.shouldShowRequestPermissionRationale(activity, Manifest.permission.RECORD_AUDIO)) {
ActivityCompat.requestPermissions(activity, new String[]{Manifest.permission.RECORD_AUDIO}, RECORD_PERMISSION_REQUEST_CODE);
} else {
ActivityCompat.requestPermissions(activity, new String[]{Manifest.permission.RECORD_AUDIO}, RECORD_PERMISSION_REQUEST_CODE);
}
}
示例14: checkPermissionAndDonateWeixin
import android.support.v4.app.ActivityCompat; //导入方法依赖的package包/类
private void checkPermissionAndDonateWeixin() {
//检测微信是否安装
if (!WeiXinDonate.hasInstalledWeiXinClient(this)) {
Toast.makeText(this, "未安装微信客户端", Toast.LENGTH_SHORT).show();
return;
}
if (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) {
//已经有权限
showDonateTipDialog();
} else {
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, REQUEST_CODE);
}
}
示例15: getDeviceLoc
import android.support.v4.app.ActivityCompat; //导入方法依赖的package包/类
/**
* ref: https://chantisandroid.blogspot.ca/2017/06/get-current-location-example-in-android.html
* @return current location
*/
private Location getDeviceLoc() {
service = (LocationManager) getSystemService(LOCATION_SERVICE);
if (ActivityCompat.checkSelfPermission(ViewHabitEvent.this, Manifest.permission.ACCESS_FINE_LOCATION)
!= PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission
(ViewHabitEvent.this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(ViewHabitEvent.this,
new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, REQUEST_LOCATION);
}
else {
Location location = service.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
Location location1 = service.getLastKnownLocation(LocationManager.GPS_PROVIDER);
Location location2 = service.getLastKnownLocation(LocationManager. PASSIVE_PROVIDER);
if (location != null) {
return location;
}
else if (location1 != null) {
return location1;
}
else if (location2 != null) {
return location2;
}
}
return null;
}