SDからリソースを取得/設定する機能を設計し、SDからリソースが見つからない場合はAssetから取得し、可能であればSDにアセットを書き戻します
この関数は、SDがマウントされていてアクセス可能かどうかをメソッド呼び出しで確認できます...
boolean bSDisAvalaible = Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED);
私の設計した機能は、1つのアプリ(プロジェクト)から別のアプリ(Android.permission.WRITE_EXTERNAL_STORAGEの有無にかかわらず)で使用できます。
次に、SecurityExceptionを使用せずに、現在のアプリケーションにこの特定のアクセス許可があるかどうかを確認します。
実行時に現在定義されている権限を調べる「いい」方法はありますか?
これにはContext.checkCallingorSelfPermission()
関数を使用できます。以下に例を示します。
private boolean checkWriteExternalPermission()
{
String permission = Android.Manifest.permission.WRITE_EXTERNAL_STORAGE;
int res = getContext().checkCallingOrSelfPermission(permission);
return (res == PackageManager.PERMISSION_GRANTED);
}
これも別のソリューションです
PackageManager pm = context.getPackageManager();
int hasPerm = pm.checkPermission(
Android.Manifest.permission.WRITE_EXTERNAL_STORAGE,
context.getPackageName());
if (hasPerm != PackageManager.PERMISSION_GRANTED) {
// do stuff
}
これも使用できます:
private boolean doesUserHavePermission()
{
int result = context.checkCallingOrSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE);
return result == PackageManager.PERMISSION_GRANTED;
}
Googleのような ドキュメント :
// Assume thisActivity is the current activity
int permissionCheck = ContextCompat.checkSelfPermission(thisActivity, Manifest.permission.WRITE_EXTERNAL_STORAGE);
誰かがそれらを必要とする場合に備えて私の方法を共有する:
/** Determines if the context calling has the required permission
* @param context - the IPC context
* @param permissions - The permissions to check
* @return true if the IPC has the granted permission
*/
public static boolean hasPermission(Context context, String permission) {
int res = context.checkCallingOrSelfPermission(permission);
Log.v(TAG, "permission: " + permission + " = \t\t" +
(res == PackageManager.PERMISSION_GRANTED ? "GRANTED" : "DENIED"));
return res == PackageManager.PERMISSION_GRANTED;
}
/** Determines if the context calling has the required permissions
* @param context - the IPC context
* @param permissions - The permissions to check
* @return true if the IPC has the granted permission
*/
public static boolean hasPermissions(Context context, String... permissions) {
boolean hasAllPermissions = true;
for(String permission : permissions) {
//you can return false instead of assigning, but by assigning you can log all permission values
if (! hasPermission(context, permission)) {hasAllPermissions = false; }
}
return hasAllPermissions;
}
そしてそれを呼び出すには:
boolean hasAndroidPermissions = SystemUtils.hasPermissions(mContext, new String[] {
Android.Manifest.permission.ACCESS_WIFI_STATE,
Android.Manifest.permission.READ_PHONE_STATE,
Android.Manifest.permission.ACCESS_NETWORK_STATE,
Android.Manifest.permission.INTERNET,
});
次の方法で許可を確認する必要があります(ここで説明するように Android許可 )。
int result = ContextCompat.checkSelfPermission(getContext(), Manifest.permission.READ_PHONE_STATE);
次に、結果を次のいずれかと比較します。
result == PackageManager.PERMISSION_DENIED
または:
result == PackageManager.PERMISSION_GRANTED
ステップ1-許可リクエストを追加する
String[] permissionArrays = new String[]{Manifest.permission.CAMERA,
Manifest.permission.WRITE_EXTERNAL_STORAGE};
int REQUEST_CODE = 101;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
requestPermissions(permissionArrays, REQUEST_CODE );
} else {
// if already permition granted
// PUT YOUR ACTION (Like Open cemara etc..)
}
}
ステップ2-許可の結果を処理する
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
boolean openActivityOnce = true;
boolean openDialogOnce = true;
if (requestCode == REQUEST_CODE ) {
for (int i = 0; i < grantResults.length; i++) {
String permission = permissions[i];
isPermitted = grantResults[i] == PackageManager.PERMISSION_GRANTED;
if (grantResults[i] == PackageManager.PERMISSION_DENIED) {
// user rejected the permission
}else {
// user grant the permission
// you can perfome your action
}
}
}
}
私のためにうまく機能するコードは次のとおりです:-
final int MY_PERMISSIONS_REQUEST_WRITE_EXTERNAL_STORAGE = 102;
if ((ContextCompat.checkSelfPermission(getActivity(),Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED)) {
requestPermissions(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},
MY_PERMISSIONS_REQUEST_WRITE_EXTERNAL_STORAGE);
} else {
// user already provided permission
// perform function for what you want to achieve
}
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
boolean canUseExternalStorage = false;
switch (requestCode) {
case MY_PERMISSIONS_REQUEST_WRITE_EXTERNAL_STORAGE: {
if (grantResults.length > 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
canUseExternalStorage = true;
}
if (!canUseExternalStorage) {
Toast.makeText(getActivity(), "Cannot use this feature without requested permission", Toast.LENGTH_SHORT).show();
} else {
// user now provided permission
// perform function for what you want to achieve
}
}
}
}
GPSロケーションを有効にするAndroid Studio
MapsActivity.Java
public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {
private GoogleMap mMap;
private Context context;
private static final int PERMISSION_REQUEST_CODE = 1;
Activity activity;
/**
* ATTENTION: This was auto-generated to implement the App Indexing API.
* See https://g.co/AppIndexing/AndroidStudio for more information.
*/
private GoogleApiClient client;
@Override
protected void onCreate(Bundle savedInstanceState) {
context = getApplicationContext();
activity = this;
super.onCreate(savedInstanceState);
requestPermission();
checkPermission();
setContentView(R.layout.activity_maps);
// Obtain the SupportMapFragment and get notified when the map is ready to be used.
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
}
@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
LatLng location = new LatLng(0, 0);
mMap.addMarker(new MarkerOptions().position(location).title("Marker in Bangalore"));
mMap.moveCamera(CameraUpdateFactory.newLatLng(location));
mMap.setMyLocationEnabled(true);
}
private void requestPermission() {
if (ActivityCompat.shouldShowRequestPermissionRationale(activity, Manifest.permission.ACCESS_FINE_LOCATION)) {
Toast.makeText(context, "GPS permission allows us to access location data. Please allow in App Settings for additional functionality.", Toast.LENGTH_LONG).show();
} else {
ActivityCompat.requestPermissions(activity, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, PERMISSION_REQUEST_CODE);
}
}
private boolean checkPermission() {
int result = ContextCompat.checkSelfPermission(context, Manifest.permission.ACCESS_FINE_LOCATION);
if (result == PackageManager.PERMISSION_GRANTED) {
return true;
} else {
return false;
}
}
if ((ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) && (ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) && (ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED)) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
requestPermissions(new String[]{Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.CAMERA},
MY_PERMISSIONS_REQUEST_WRITE_EXTERNAL_STORAGE);
}
}
KOTLIN(ランタイム)で権限を確認する
マニフェスト内:(Android.permission.WRITE_EXTERNAL_STORAGE)
fun checkPermissions(){
var permission_array=arrayOf(Android.Manifest.permission.WRITE_EXTERNAL_STORAGE)
if((ContextCompat.checkSelfPermission(this,permission_array[0]))==PackageManager.PERMI SSION_DENIED){
requestPermissions(permission_array,0)
}
}
override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<out String>, grantResults: IntArray) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults)
if(requestCode==0 && grantResults[0]==PackageManager.PERMISSION_GRANTED){
//Do Your Operations Here
---------->
//
}
}