Проверяет каждую ситуацию
, если отрицается, - показывает диалог оповещений пользователю, для чего нам нужно разрешение
public static final int MULTIPLE_PERMISSIONS = 1;
public static final int CAMERA_PERMISSION_REQUEST_CODE = 2;
public static final int STORAGE_PERMISSION_REQUEST_CODE = 3;
private void askPermissions() {
int permissionCheckStorage = ContextCompat.checkSelfPermission(this,
Manifest.permission.WRITE_EXTERNAL_STORAGE);
int permissionCheckCamera = ContextCompat.checkSelfPermission(this,
Manifest.permission.CAMERA);
// we already asked for permisson & Permission granted, call camera intent
if (permissionCheckStorage == PackageManager.PERMISSION_GRANTED && permissionCheckCamera == PackageManager.PERMISSION_GRANTED) {
launchCamera();
} //asking permission for the first time
else if (permissionCheckStorage != PackageManager.PERMISSION_GRANTED && permissionCheckCamera != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this,
new String[]{Manifest.permission.CAMERA, Manifest.permission.WRITE_EXTERNAL_STORAGE},
MULTIPLE_PERMISSIONS);
} else {
// Permission denied, so request permission
// if camera request is denied
if (ActivityCompat.shouldShowRequestPermissionRationale(this,
Manifest.permission.CAMERA)) {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("You need to give permission to take pictures in order to work this feature.");
builder.setNegativeButton("CANCEL", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
dialogInterface.dismiss();
}
});
builder.setPositiveButton("GIVE PERMISSION", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
dialogInterface.dismiss();
// Show permission request popup
ActivityCompat.requestPermissions(this,
new String[]{Manifest.permission.CAMERA},
CAMERA_PERMISSION_REQUEST_CODE);
}
});
builder.show();
} // if storage request is denied
else if (ActivityCompat.shouldShowRequestPermissionRationale(this,
Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("You need to give permission to access storage in order to work this feature.");
builder.setNegativeButton("CANCEL", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
dialogInterface.dismiss();
}
});
builder.setPositiveButton("GIVE PERMISSION", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
dialogInterface.dismiss();
// Show permission request popup
ActivityCompat.requestPermissions(this,
new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},
STORAGE_PERMISSION_REQUEST_CODE);
}
});
builder.show();
}
}
}
Проверка результатов разрешения
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
switch (requestCode) {
case CAMERA_PERMISSION_REQUEST_CODE:
if (grantResults.length > 0 && permissions[0].equals(Manifest.permission.CAMERA)) {
// check whether camera permission granted or not.
if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
launchCamera();
}
}
break;
case STORAGE_PERMISSION_REQUEST_CODE:
if (grantResults.length > 0 && permissions[0].equals(Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
// check whether storage permission granted or not.
if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
launchCamera();
}
}
break;
case MULTIPLE_PERMISSIONS:
if (grantResults.length > 0 && permissions[0].equals(Manifest.permission.CAMERA) && permissions[1].equals(Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
// check whether All permission granted or not.
if (grantResults[0] == PackageManager.PERMISSION_GRANTED && grantResults[1] == PackageManager.PERMISSION_GRANTED) {
launchCamera();
}
}
break;
default:
break;
}
}
вы можете просто скопировать и вставить этот код, он отлично работает. изменить контекст (это) & amp; разрешений в соответствии с вами.
blockquote>
Используйте <Switch>
// app.js
import {BrowserRouter as Router, Route, Switch } from 'react-router-dom';
<Router>
<Switch>
<Route path="/account" component={Account} />
</Switch>
</Router>
и
// account.js
import { Route, Switch } from 'react-router-dom';
class Account extends Component {
render() {
return (
<div>
<Link to="/account">Messages</Link>
<Link to="/account/orders">Orders</Link>
<Switch>
<Route exact path="/account" component={Messages} />
<Route path="/account/:id" component={AccountSwitch} />
</Switch>
</div>
)
}
}
В https://github.com/Rynebenson/og-website/blob/master/src/_components/wrappers/Account/index.js , где вы используете переключатель маршрута, попробуйте использовать точное соответствие для "/ account" и посмотрите, работает ли это.
Использование:
<Switch>
<Route exact path={`/account`} component={AccountMessages} />
<Route path={`/account/:id`} component={AccountSwitch} />
</Switch>
Все во время экспорта попробуйте это:
экспорт по умолчанию с помощью роутера (соединение ( mapStateToProps, mapDispatchToProps) (Аккаунт))
Вы пробовали с помощью роутера?
import { withRouter } from 'react-router-dom'
const AccountSwitch = props => {
switch(props.match.params.id) {
case '':
return (
<AccountMessages />
);
case 'orders':
return (
<AccountOrders />
)
default:
return <span>select a page<span/>
}
}
export default withRouter(AccountSwitch)