How to Run an if Else Again in Java
Starting from Android six.0 (API 23), users are non asked for permissions at the time of installation rather developers need to request the permissions at the run time. Simply the permissions that are divers in the manifest file tin can be requested at run time.
Types of Permissions
1. Install-Fourth dimension Permissions: If the Android 5.i.1 (API 22) or lower, the permission is requested at the installation fourth dimension at the Google Play Shop.
If the user Accepts the permissions, the app is installed. Else the app installation is canceled.
2. Run-Time Permissions: If the Android half dozen (API 23) or college, the permission is requested at the run time during the running of the app.
If the user Accepts the permissions, then that feature of the app can be used. Else to utilize the characteristic, the app requests permission again.
So, now the permissions are requested at runtime. In this article, we will talk over how to request permissions in an Android Application at run time.
Steps for Requesting permissions at run fourth dimension
Step ane: Declare the permission in the Android Manifest file : In Android, permissions are declared in the AndroidManifest.xml file using the uses-permission tag.
<uses-permission android:name="android.permission.PERMISSION_NAME"/>
Here we are declaring storage and camera permission.
XML
<
uses-permission
android:name
=
"android.permission.READ_EXTERNAL_STORAGE"
/>
<
uses-permission
android:proper noun
=
"android.permission.WRITE_EXTERNAL_STORAGE"
/>
<
uses-permission
android:proper name
=
"android.permission.Photographic camera"
/>
Pace two: Modify activity_main.xml file to Add together two buttons to request permission on button click: Permission will be checked and requested on button click. Open the activity_main.xml file and add two buttons to information technology.
XML
<
Button
android:id
=
"@+id/storage"
android:layout_width
=
"wrap_content"
android:layout_height
=
"wrap_content"
android:text
=
"Storage"
android:layout_marginTop
=
"16dp"
android:padding
=
"8dp"
android:layout_below
=
"@id/toolbar"
android:layout_centerHorizontal
=
"true"
/>
<
Push button
android:id
=
"@+id/camera"
android:layout_width
=
"wrap_content"
android:layout_height
=
"wrap_content"
android:text
=
"Camera"
android:layout_marginTop
=
"16dp"
android:padding
=
"8dp"
android:layout_below
=
"@id/storage"
android:layout_centerHorizontal
=
"truthful"
/>
Step 3: Check whether permission is already granted or not. If permission isn't already granted, asking the user for the permission: In order to use any service or feature, the permissions are required. Hence we have to ensure that the permissions are given for that. If non, and then the permissions are requested.
Check for permissions: Kickoff with Android six.0 (API level 23), the user has the right to revoke permissions from any app at whatsoever time, fifty-fifty if the app targets a lower API level. So to use the service, the app needs to check for permissions every time.
Syntax:
if(ContextCompat.checkSelfPermission(thisActivity, Manifest.permission.WRITE_CALENDAR) != PackageManager.PERMISSION_GRANTED) { // Permission is not granted }
Asking Permissions: When PERMISSION_DENIED is returned from the checkSelfPermission() method in the to a higher place syntax, we need to prompt the user for that permission. Android provides several methods that can be used to request permission, such as requestPermissions().
Syntax:
ActivityCompat.requestPermissions(MainActivity.this, permissionArray, requestCode); Here permissionArray is an array of type String.
Example:
Java
public
void
checkPermission(String permission,
int
requestCode)
{
if
(ContextCompat.checkSelfPermission(MainActivity.
this
, permission) == PackageManager.PERMISSION_DENIED) {
ActivityCompat.requestPermissions(MainActivity.
this
,
new
String[] { permission }, requestCode);
}
else
{
Toast.makeText(MainActivity.
this
,
"Permission already granted"
, Toast.LENGTH_SHORT).evidence();
}
}
Kotlin
private
fun checkPermission(permission: String, requestCode: Int) {
if
(ContextCompat.checkSelfPermission(
this
@MainActivity
, permission) == PackageManager.PERMISSION_DENIED) {
ActivityCompat.requestPermissions(
this
@MainActivity
, arrayOf(permission), requestCode)
}
else
{
Toast.makeText(
this
@MainActivity
,
"Permission already granted"
, Toast.LENGTH_SHORT).evidence()
}
}
This function will show a Toast message if permission is already granted otherwise prompt the user for permission.
Step 4: Override onRequestPermissionsResult() method: onRequestPermissionsResult() is chosen when user grant or turn down the permission. RequestCode is one of the parameters of this part which is used to check user action for the corresponding requests. Here a toast bulletin is shown indicating the permission and user action.
Example:
Coffee
@Override
public
void
onRequestPermissionsResult(
int
requestCode,
@NonNull
String[] permissions,
@NonNull
int
[] grantResults)
{
super
.onRequestPermissionsResult(requestCode, permissions, grantResults);
if
(requestCode == CAMERA_PERMISSION_CODE) {
if
(grantResults.length >
0
&& grantResults[
0
] == PackageManager.PERMISSION_GRANTED) {
Toast.makeText(MainActivity.
this
,
"Camera Permission Granted"
, Toast.LENGTH_SHORT).show();
}
else
{
Toast.makeText(MainActivity.
this
,
"Camera Permission Denied"
, Toast.LENGTH_SHORT).show();
}
}
else
if
(requestCode == STORAGE_PERMISSION_CODE) {
if
(grantResults.length >
0
&& grantResults[
0
] == PackageManager.PERMISSION_GRANTED) {
Toast.makeText(MainActivity.
this
,
"Storage Permission Granted"
, Toast.LENGTH_SHORT).show();
}
else
{
Toast.makeText(MainActivity.
this
,
"Storage Permission Denied"
, Toast.LENGTH_SHORT).show();
}
}
}
Kotlin
override fun onRequestPermissionsResult(requestCode: Int,
permissions: Array<Cord>,
grantResults: IntArray) {
super
.onRequestPermissionsResult(requestCode, permissions, grantResults)
if
(requestCode == CAMERA_PERMISSION_CODE) {
if
(grantResults.isNotEmpty() && grantResults[
0
] == PackageManager.PERMISSION_GRANTED) {
Toast.makeText(
this
@MainActivity
,
"Camera Permission Granted"
, Toast.LENGTH_SHORT).show()
}
else
{
Toast.makeText(
this
@MainActivity
,
"Camera Permission Denied"
, Toast.LENGTH_SHORT).bear witness()
}
}
else
if
(requestCode == STORAGE_PERMISSION_CODE) {
if
(grantResults.isNotEmpty() && grantResults[
0
] == PackageManager.PERMISSION_GRANTED) {
Toast.makeText(
this
@MainActivity
,
"Storage Permission Granted"
, Toast.LENGTH_SHORT).show()
}
else
{
Toast.makeText(
this
@MainActivity
,
"Storage Permission Denied"
, Toast.LENGTH_SHORT).show()
}
}
}
Below is the complete code of this application:
Beneath is the code for the activity_main.xml file.
XML
<?
xml
version
=
"i.0"
encoding
=
"utf-8"
?>
<
RelativeLayout
android:layout_width
=
"match_parent"
android:layout_height
=
"match_parent"
tools:context
=
".MainActivity"
>
<
android.support.v7.widget.Toolbar
android:id
=
"@+id/toolbar"
android:layout_width
=
"match_parent"
android:groundwork
=
"@colour/colorPrimary"
app:championship
=
"GFG | Permission Example"
app:titleTextColor
=
"@android:color/white"
android:layout_height
=
"?android:attr/actionBarSize"
/>
<
Button
android:id
=
"@+id/storage"
android:layout_width
=
"wrap_content"
android:layout_height
=
"wrap_content"
android:text
=
"Storage"
android:layout_marginTop
=
"16dp"
android:padding
=
"8dp"
android:layout_below
=
"@id/toolbar"
android:layout_centerHorizontal
=
"truthful"
/>
<
Push
android:id
=
"@+id/camera"
android:layout_width
=
"wrap_content"
android:layout_height
=
"wrap_content"
android:text
=
"Photographic camera"
android:layout_marginTop
=
"16dp"
android:padding
=
"8dp"
android:layout_below
=
"@id/storage"
android:layout_centerHorizontal
=
"true"
/>
</
RelativeLayout
>
Below is the code for the AndroidManifest.xml file.
XML
<?
xml
version
=
"1.0"
encoding
=
"utf-8"
?>
package
=
"org.geeksforgeeks.requestPermission"
>
<
uses-permission
android:name
=
"android.permission.READ_EXTERNAL_STORAGE"
/>
<
uses-permission
android:name
=
"android.permission.WRITE_EXTERNAL_STORAGE"
/>
<
uses-permission
android:name
=
"android.permission.CAMERA"
/>
<
application
android:allowBackup
=
"truthful"
android:icon
=
"@mipmap/ic_launcher"
android:label
=
"@string/app_name"
android:roundIcon
=
"@mipmap/ic_launcher_round"
android:supportsRtl
=
"truthful"
android:theme
=
"@style/AppTheme"
>
<
activity
android:proper noun
=
".MainActivity"
>
<
intent-filter
>
<
activity
android:name
=
"android.intent.action.MAIN"
/>
<
category
android:name
=
"android.intent.category.LAUNCHER"
/>
</
intent-filter
>
</
activity
>
</
application
>
</
manifest
>
Beneath is the lawmaking for the MainActivity file.
Kotlin
import
android.Manifest
import
android.content.pm.PackageManager
import
android.bone.Package
import
android.widget.Button
import
android.widget.Toast
import
androidx.appcompat.app.AppCompatActivity
import
androidx.core.app.ActivityCompat
import
androidx.core.content.ContextCompat
grade
MainActivity : AppCompatActivity() {
companion object {
private
const
val CAMERA_PERMISSION_CODE =
100
private
const
val STORAGE_PERMISSION_CODE =
101
}
override fun onCreate(savedInstanceState: Bundle?) {
super
.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val storage: Push? = findViewById(R.id.storage)
val camera: Push? = findViewById(R.id.camera)
storage?.setOnClickListener {checkPermission(
Manifest.permission.WRITE_EXTERNAL_STORAGE,
STORAGE_PERMISSION_CODE)
}
camera?.setOnClickListener {
checkPermission(Manifest.permission.Photographic camera,
CAMERA_PERMISSION_CODE)
}
}
private
fun checkPermission(permission: String, requestCode: Int) {
if
(ContextCompat.checkSelfPermission(
this
@MainActivity
, permission) == PackageManager.PERMISSION_DENIED) {
ActivityCompat.requestPermissions(
this
@MainActivity
, arrayOf(permission), requestCode)
}
else
{
Toast.makeText(
this
@MainActivity
,
"Permission already granted"
, Toast.LENGTH_SHORT).show()
}
}
override fun onRequestPermissionsResult(requestCode: Int,
permissions: Array<Cord>,
grantResults: IntArray) {
super
.onRequestPermissionsResult(requestCode, permissions, grantResults)
if
(requestCode == CAMERA_PERMISSION_CODE) {
if
(grantResults.isNotEmpty() && grantResults[
0
] == PackageManager.PERMISSION_GRANTED) {
Toast.makeText(
this
@MainActivity
,
"Camera Permission Granted"
, Toast.LENGTH_SHORT).show()
}
else
{
Toast.makeText(
this
@MainActivity
,
"Photographic camera Permission Denied"
, Toast.LENGTH_SHORT).show()
}
}
else
if
(requestCode == STORAGE_PERMISSION_CODE) {
if
(grantResults.isNotEmpty() && grantResults[
0
] == PackageManager.PERMISSION_GRANTED) {
Toast.makeText(
this
@MainActivity
,
"Storage Permission Granted"
, Toast.LENGTH_SHORT).show()
}
else
{
Toast.makeText(
this
@MainActivity
,
"Storage Permission Denied"
, Toast.LENGTH_SHORT).show()
}
}
}
}
Java
import
android.Manifest;
import
android.content.pm.PackageManager;
import
android.support.annotation.NonNull;
import
android.support.v4.app.ActivityCompat;
import
android.back up.v4.content.ContextCompat;
import
android.back up.v7.app.AppCompatActivity;
import
android.os.Parcel;
import
android.view.View;
import
android.widget.Push;
import
android.widget.Toast;
public
class
MainActivity
extends
AppCompatActivity {
private
Push button storage, camera;
individual
static
last
int
CAMERA_PERMISSION_CODE =
100
;
private
static
concluding
int
STORAGE_PERMISSION_CODE =
101
;
@Override
protected
void
onCreate(Bundle savedInstanceState)
{
super
.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
storage = findViewById(R.id.storage);
camera = findViewById(R.id.camera);
storage.setOnClickListener(
new
View.OnClickListener() {
@Override
public
void
onClick(View five)
{
checkPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE, STORAGE_PERMISSION_CODE);
}
});
photographic camera.setOnClickListener(
new
View.OnClickListener() {
@Override
public
void
onClick(View v)
{
checkPermission(Manifest.permission.Camera, CAMERA_PERMISSION_CODE);
}
});
}
public
void
checkPermission(Cord permission,
int
requestCode)
{
if
(ContextCompat.checkSelfPermission(MainActivity.
this
, permission) == PackageManager.PERMISSION_DENIED) {
ActivityCompat.requestPermissions(MainActivity.
this
,
new
String[] { permission }, requestCode);
}
else
{
Toast.makeText(MainActivity.
this
,
"Permission already granted"
, Toast.LENGTH_SHORT).testify();
}
}
@Override
public
void
onRequestPermissionsResult(
int
requestCode,
@NonNull
Cord[] permissions,
@NonNull
int
[] grantResults)
{
super
.onRequestPermissionsResult(requestCode,
permissions,
grantResults);
if
(requestCode == CAMERA_PERMISSION_CODE) {
if
(grantResults.length >
0
&& grantResults[
0
] == PackageManager.PERMISSION_GRANTED) {
Toast.makeText(MainActivity.
this
,
"Camera Permission Granted"
, Toast.LENGTH_SHORT) .show();
}
else
{
Toast.makeText(MainActivity.
this
,
"Camera Permission Denied"
, Toast.LENGTH_SHORT) .show();
}
}
else
if
(requestCode == STORAGE_PERMISSION_CODE) {
if
(grantResults.length >
0
&& grantResults[
0
] == PackageManager.PERMISSION_GRANTED) {
Toast.makeText(MainActivity.
this
,
"Storage Permission Granted"
, Toast.LENGTH_SHORT).show();
}
else
{
Toast.makeText(MainActivity.
this
,
"Storage Permission Denied"
, Toast.LENGTH_SHORT).evidence();
}
}
}
}
Output:
On starting the awarding:
On clicking the photographic camera push button for the first time:
On Granting the permission:
On clicking the camera push button again:
Source: https://www.geeksforgeeks.org/android-how-to-request-permissions-in-android-application/
0 Response to "How to Run an if Else Again in Java"
Post a Comment