本文整理汇总了Java中android.support.v4.app.ActivityCompat类的典型用法代码示例。如果您正苦于以下问题:Java ActivityCompat类的具体用法?Java ActivityCompat怎么用?Java ActivityCompat使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
ActivityCompat类属于android.support.v4.app包,在下文中一共展示了ActivityCompat类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: requestRuntimePermission
import android.support.v4.app.ActivityCompat; //导入依赖的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: requestStoragePermissions
import android.support.v4.app.ActivityCompat; //导入依赖的package包/类
private void requestStoragePermissions() {
if (ActivityCompat.shouldShowRequestPermissionRationale(getActivity(),
Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
// Provide an additional rationale to the user if the permission was not granted
// and the user would benefit from additional context for the use of the permission.
// For example if the user has previously denied the permission.
Snackbar.make(mRootLayout, "Storage access permissions are required to upload/download files.",
Snackbar.LENGTH_LONG)
.setAction("Okay", new View.OnClickListener() {
@Override
public void onClick(View view) {
requestPermissions(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},
PERMISSION_WRITE_EXTERNAL_STORAGE);
}
})
.show();
} else {
// Permission has not been granted yet. Request it directly.
requestPermissions(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},
PERMISSION_WRITE_EXTERNAL_STORAGE);
}
}
示例3: onSharedPreferenceChanged
import android.support.v4.app.ActivityCompat; //导入依赖的package包/类
@Override
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
// update the summeries:
updateSummaries();
// Screen Orientation:
String screenOrientation = sharedPreferences.getString(getString(R.string.pref_screenOrientation), "auto");
setScreenOrientation(screenOrientation);
// check WRITE_EXTERNAL_STORAGE permission if logging is active:
if(sharedPreferences.getBoolean(getString(R.string.pref_logging), false)) {
if (ContextCompat.checkSelfPermission(this.getActivity(), "android.permission.WRITE_EXTERNAL_STORAGE")
!= PackageManager.PERMISSION_GRANTED) {
// request permission:
ActivityCompat.requestPermissions(this.getActivity(), new String[]{"android.permission.WRITE_EXTERNAL_STORAGE"},
PERMISSION_REQUEST_LOGGING_WRITE_FILES);
}
}
}
示例4: requestPermissions
import android.support.v4.app.ActivityCompat; //导入依赖的package包/类
private void requestPermissions(){
try {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
int permission = ActivityCompat.checkSelfPermission(this,
Manifest.permission.WRITE_EXTERNAL_STORAGE);
if(permission!= PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this,new String[] {Manifest.permission.WRITE_EXTERNAL_STORAGE,
Manifest.permission.LOCATION_HARDWARE,Manifest.permission.READ_PHONE_STATE,
Manifest.permission.WRITE_SETTINGS,Manifest.permission.READ_EXTERNAL_STORAGE,
Manifest.permission.RECORD_AUDIO,Manifest.permission.READ_CONTACTS},0x0010);
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
示例5: requestPermissions
import android.support.v4.app.ActivityCompat; //导入依赖的package包/类
private void requestPermissions() {
boolean shouldProvideRationale =
ActivityCompat.shouldShowRequestPermissionRationale(this,
android.Manifest.permission.ACCESS_COARSE_LOCATION);
// Provide an additional rationale to the user. This would happen if the user denied the
// request previously, but didn't check the "Don't ask again" checkbox.
if (shouldProvideRationale) {
Log.i(TAG, "Displaying permission rationale to provide additional context.");
} else {
Log.i(TAG, "Requesting permission");
// Request permission. It's possible this can be auto answered if device policy
// sets the permission in a given state or the user denied the permission
// previously and checked "Never ask again".
startLocationPermissionRequest();
}
}
示例6: onBackPressed
import android.support.v4.app.ActivityCompat; //导入依赖的package包/类
@Override
public void onBackPressed() {
//获取数据返回获取
long currPosition = exoPlayerManager.getCurrentPosition();
if (exoPlayerManager.onBackPressed()) {//使用播放返回键监听
isBack=true;
Toast.makeText(MainListInfoCustomActivity.this, "返回", Toast.LENGTH_LONG).show();
Intent intent = new Intent();
intent.putExtra("isEnd", isEnd);
intent.putExtra("currPosition", currPosition);
Log.d(TAG, "sss:" + exoPlayerManager.getCurrentPosition());
setResult(RESULT_OK, intent);
ActivityCompat.finishAfterTransition(this);
}
}
示例7: getLastKnownLocation
import android.support.v4.app.ActivityCompat; //导入依赖的package包/类
private Location getLastKnownLocation(){
if( ActivityCompat.checkSelfPermission( this, Manifest.permission.ACCESS_FINE_LOCATION ) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission( this, Manifest.permission.ACCESS_COARSE_LOCATION ) != PackageManager.PERMISSION_GRANTED ){
return null;
}
LocationManager locationManager =
(LocationManager) this.getSystemService( LOCATION_SERVICE );
List<String> providers = locationManager.getProviders( true );
Location bestLocation = null;
for( String provider : providers ){
Location l = locationManager.getLastKnownLocation( provider );
if( l == null ){
continue;
}
if( bestLocation == null || l.getAccuracy() < bestLocation.getAccuracy() ){
bestLocation = l; // Found best last known location;
}
}
return bestLocation;
}
示例8: checkPermission
import android.support.v4.app.ActivityCompat; //导入依赖的package包/类
/********************************** 以下是权限检查部分 ********************************/
private void checkPermission(){
if (ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA)
== PackageManager.PERMISSION_GRANTED) {
mCameraView.start();
} else if (ActivityCompat.shouldShowRequestPermissionRationale(this,
Manifest.permission.CAMERA)) {
ConfirmationDialogFragment
.newInstance(R.string.camera_permission_confirmation,
new String[]{Manifest.permission.CAMERA},
REQUEST_CAMERA_PERMISSION,
R.string.camera_permission_not_granted)
.show(getSupportFragmentManager(), FRAGMENT_DIALOG);
} else {
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.CAMERA},
REQUEST_CAMERA_PERMISSION);
}
}
示例9: requestStoragePermissions
import android.support.v4.app.ActivityCompat; //导入依赖的package包/类
protected void requestStoragePermissions() {
if(PermissionUtil.hasStoragePermission(this)) {
again();
} else {
if (ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
Utils.showRetrySnackBar(this, "Storage permissions are needed for Exploring.", new View.OnClickListener() {
@Override
public void onClick(View v) {
ActivityCompat.requestPermissions(BaseActivity.this, storagePermissions, REQUEST_STORAGE);
}
});
} else {
ActivityCompat.requestPermissions(this, storagePermissions, REQUEST_STORAGE);
}
}
}
示例10: onCreate
import android.support.v4.app.ActivityCompat; //导入依赖的package包/类
@Override
protected void onCreate(Bundle savedInstanceState) {
if (Utils.hasLollipop()) requestWindowFeature(Window.FEATURE_CONTENT_TRANSITIONS);
super.onCreate(savedInstanceState);
if (Utils.hasLollipop()) {
getWindow().setExitTransition(new Fade());
}
//TODO: Remove after evaluation testing
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
Intent intent = new Intent(this, MainActivity.class);
ActivityOptionsCompat options = ActivityOptionsCompat.makeSceneTransitionAnimation(this);
ActivityCompat.startActivity(this, intent, options.toBundle());
ActivityCompat.finishAfterTransition(this);
}
示例11: cheseFile
import android.support.v4.app.ActivityCompat; //导入依赖的package包/类
private void cheseFile() {
int checkCode = ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.WRITE_EXTERNAL_STORAGE);
int checkRead = ContextCompat.checkSelfPermission(MainActivity.this,Manifest.permission.READ_EXTERNAL_STORAGE);
//如果拒绝
if (checkCode== PackageManager.PERMISSION_DENIED||checkRead==PackageManager.PERMISSION_DENIED){
//申请权限
if (checkCode==PackageManager.PERMISSION_DENIED){
ActivityCompat.requestPermissions(this,new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},100);
}
if (checkRead==PackageManager.PERMISSION_DENIED){
ActivityCompat.requestPermissions(this,new String[]{Manifest.permission.READ_EXTERNAL_STORAGE},101);
}
}else if (checkCode==PackageManager.PERMISSION_GRANTED){
if (mSelected!=null&&mSelected.size()>0)
mSelected.clear();
CANPLAY=false;
doSomething();
}
}
示例12: isReadStoragePermissionGranted
import android.support.v4.app.ActivityCompat; //导入依赖的package包/类
private static boolean isReadStoragePermissionGranted(Activity activity) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (activity.checkSelfPermission(Manifest.permission.READ_EXTERNAL_STORAGE)
== PackageManager.PERMISSION_GRANTED) {
Timber.d("Permission is granted");
return true;
} else {
Timber.d("Permission is revoked");
ActivityCompat.requestPermissions(activity,
new String[]{Manifest.permission.READ_EXTERNAL_STORAGE},
activity.getResources().getInteger(R.integer.read_external_storage_request));
return false;
}
} else { //permission is automatically granted on sdk<23 upon installation
Timber.d("Permission is granted");
return true;
}
}
示例13: showUpdateDialog
import android.support.v4.app.ActivityCompat; //导入依赖的package包/类
/**
* 更新Dialog
*
* @param url
* @param version
* @param desc
* @param force
*/
private void showUpdateDialog(String url, String version, String desc, boolean force) {
if (updateDialog == null) {
updateDialog = new UpdateDialog(mContext);
updateDialog.setValue(url, version, desc, force);
updateDialog.setOnClickUpdateListener(() -> {
updateDialog.cancel();
if (ContextCompat.checkSelfPermission(mContext, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions((Activity) mContext, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, PERMISSION_WRITE_STORAGE);
} else {
updateDialog.onClickUpdate();
}
});
}
updateDialog.show();
}
示例14: onAttachClick
import android.support.v4.app.ActivityCompat; //导入依赖的package包/类
public void onAttachClick() {
final int permissionStatus = ContextCompat.checkSelfPermission(
this,
Manifest.permission.READ_EXTERNAL_STORAGE
);
if (permissionStatus != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(
this,
new String[]{Manifest.permission.READ_EXTERNAL_STORAGE},
REQUEST_ATTACH_PERMISSION
);
return;
}
final Intent attach = new Intent(Intent.ACTION_GET_CONTENT)
.addCategory(Intent.CATEGORY_OPENABLE)
.setType("*/*");
startActivityForResult(attach, REQUEST_ATTACH_FILE);
}
示例15: onCreate
import android.support.v4.app.ActivityCompat; //导入依赖的package包/类
@Override
public void onCreate(Bundle savedInstanceState) {
Log.i(TAG, "called onCreate");
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 READ_EXTERNAL_STORAGE");
ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.CAMERA}, REQUEST_CAMERA);
}else {
Log.i("permission", "READ_EXTERNAL_STORAGE already granted");
camera_granted = true;
}
mOpenCvCameraView = (CameraBridgeViewBase) findViewById(R.id.main_activity_surface_view);
mOpenCvCameraView.setCvCameraViewListener(this);
}