티스토리 뷰
반응형
boolean hasPermission = requestStoragePermission();
if (hasPermission) {
BoxStore boxStore = MyObjectBox.builder().baseDirectory(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOCUMENTS)).build();
// boxStore = MyObjectBox.builder().androidContext(MoyeoApplication.this).build();
if (BuildConfig.DEBUG) {
new AndroidObjectBrowser(boxStore).start(this);
}
((MoyeoApplication) getApplicationContext()).setBoxStore(boxStore);
} else {
return;
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
switch(requestCode) {
case PERMISSION_REQUEST_CODE_STORAGE:
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Intent i = new Intent(MainActivity.this,MainActivity.class);
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(i);
} else if (!shouldShowRequestPermissionRationale(permissions[0])) {
// User selected the Never Ask Again Option Change settings in app settings manually
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);
alertDialogBuilder.setTitle("Change Permissions in Settings");
alertDialogBuilder
.setMessage("" +
"\nClick SETTINGS to Manually Set\n"+"Permissions to use Database Storage")
.setCancelable(false)
.setPositiveButton("SETTINGS", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
Uri uri = Uri.fromParts("package", getPackageName(), null);
intent.setData(uri);
startActivityForResult(intent, 1000); // Comment 3.
}
});
AlertDialog alertDialog = alertDialogBuilder.create();
alertDialog.show();
} else {
// User selected Deny Dialog to EXIT App ==> OR <== RETRY to have a second chance to Allow Permissions
if (checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_DENIED) {
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);
alertDialogBuilder.setTitle("Second Chance");
alertDialogBuilder
.setMessage("Click RETRY to Set Permissions to Allow\n\n"+"Click EXIT to the Close App")
.setCancelable(false)
.setPositiveButton("RETRY", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
//ActivityCompat.requestPermissions((Activity) context, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, Integer.parseInt(WRITE_EXTERNAL_STORAGE));
Intent i = new Intent(MainActivity.this,MainActivity.class);
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(i);
}
})
.setNegativeButton("EXIT", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
finish();
dialog.cancel();
}
});
AlertDialog alertDialog = alertDialogBuilder.create();
alertDialog.show();
}
}
break;
}
}
private boolean requestStoragePermission() {
ArrayList<String> permissions = new ArrayList<>();
// Storage Write Permission
if (ContextCompat.checkSelfPermission(this,
android.Manifest.permission.WRITE_EXTERNAL_STORAGE)
!= PackageManager.PERMISSION_GRANTED) {
permissions.add(android.Manifest.permission.WRITE_EXTERNAL_STORAGE);
}
if (permissions.size() > 0) {
String[] requestPermissions = new String[permissions.size()];
permissions.toArray(requestPermissions);
ActivityCompat.requestPermissions(this, requestPermissions, PERMISSION_REQUEST_CODE_STORAGE);
return false;
} else {
return true;
}
}
public static String getMemo(Context context, String nickname) {
BoxStore boxStore = ((MoyeoApplication) context.getApplicationContext()).getBoxStore();
Box<MemoData> memoBox = boxStore.boxFor(MemoData.class);
List<MemoData> prevData = memoBox.query().equal(MemoData_.username, nickname)
.build().find();
if (prevData != null && prevData.size() > 0) {
return prevData.get(0).getMemo();
}
return "";
}
if (hasPermission) {
BoxStore boxStore = MyObjectBox.builder().baseDirectory(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOCUMENTS)).build();
// boxStore = MyObjectBox.builder().androidContext(MoyeoApplication.this).build();
if (BuildConfig.DEBUG) {
new AndroidObjectBrowser(boxStore).start(this);
}
((MoyeoApplication) getApplicationContext()).setBoxStore(boxStore);
} else {
return;
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
switch(requestCode) {
case PERMISSION_REQUEST_CODE_STORAGE:
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Intent i = new Intent(MainActivity.this,MainActivity.class);
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(i);
} else if (!shouldShowRequestPermissionRationale(permissions[0])) {
// User selected the Never Ask Again Option Change settings in app settings manually
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);
alertDialogBuilder.setTitle("Change Permissions in Settings");
alertDialogBuilder
.setMessage("" +
"\nClick SETTINGS to Manually Set\n"+"Permissions to use Database Storage")
.setCancelable(false)
.setPositiveButton("SETTINGS", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
Uri uri = Uri.fromParts("package", getPackageName(), null);
intent.setData(uri);
startActivityForResult(intent, 1000); // Comment 3.
}
});
AlertDialog alertDialog = alertDialogBuilder.create();
alertDialog.show();
} else {
// User selected Deny Dialog to EXIT App ==> OR <== RETRY to have a second chance to Allow Permissions
if (checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_DENIED) {
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);
alertDialogBuilder.setTitle("Second Chance");
alertDialogBuilder
.setMessage("Click RETRY to Set Permissions to Allow\n\n"+"Click EXIT to the Close App")
.setCancelable(false)
.setPositiveButton("RETRY", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
//ActivityCompat.requestPermissions((Activity) context, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, Integer.parseInt(WRITE_EXTERNAL_STORAGE));
Intent i = new Intent(MainActivity.this,MainActivity.class);
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(i);
}
})
.setNegativeButton("EXIT", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
finish();
dialog.cancel();
}
});
AlertDialog alertDialog = alertDialogBuilder.create();
alertDialog.show();
}
}
break;
}
}
private boolean requestStoragePermission() {
ArrayList<String> permissions = new ArrayList<>();
// Storage Write Permission
if (ContextCompat.checkSelfPermission(this,
android.Manifest.permission.WRITE_EXTERNAL_STORAGE)
!= PackageManager.PERMISSION_GRANTED) {
permissions.add(android.Manifest.permission.WRITE_EXTERNAL_STORAGE);
}
if (permissions.size() > 0) {
String[] requestPermissions = new String[permissions.size()];
permissions.toArray(requestPermissions);
ActivityCompat.requestPermissions(this, requestPermissions, PERMISSION_REQUEST_CODE_STORAGE);
return false;
} else {
return true;
}
}
public static String getMemo(Context context, String nickname) {
BoxStore boxStore = ((MoyeoApplication) context.getApplicationContext()).getBoxStore();
Box<MemoData> memoBox = boxStore.boxFor(MemoData.class);
List<MemoData> prevData = memoBox.query().equal(MemoData_.username, nickname)
.build().find();
if (prevData != null && prevData.size() > 0) {
return prevData.get(0).getMemo();
}
return "";
}
반응형
'개발 > 안드로이드' 카테고리의 다른 글
[안드로이드/android] back key 두번 눌러서 activity 종료 시키기 (0) | 2018.10.26 |
---|---|
[안드로이드/android] WebView cache, cookie 삭제하기 (0) | 2018.10.26 |
[안드로이드/android] PopupMenu 스타일 적용 (0) | 2018.10.09 |
[안드로이드/android] AlertDialog 스타일 적용 (1) | 2018.10.09 |
[안드로이드/android] SwipeRefreshLayout에 스타일 적용하기 (0) | 2018.10.07 |
댓글
공지사항
최근에 올라온 글