When developing an Android app, setting permissions is essential to use specific features or data. In this tutorial, we will learn in detail about the importance and methods of permission settings in Android app development using Java.
1. The Importance of Permissions
Permissions are the rights that allow the app to access specific features. For example, when accessing the camera, microphone, location information, or storage, permissions must be requested. If permissions are not set, these features cannot be used, and they play an important role in protecting user data and enhancing security.
1.1 Types of Permissions
In Android, permissions can be divided into two main types:
- Normal Permissions: Features that do not significantly affect the user and are automatically granted during app installation. E.g., internet access.
- Dangerous Permissions: Features that access sensitive user information and require explicit user consent. E.g., location information, camera access.
2. How to Set Permissions
There are two steps to set permissions in an Android app. We will look at each method based on the type.
2.1 Declaring Permissions in the AndroidManifest.xml File
All permissions must first be declared in the AndroidManifest.xml
file. This file defines the app’s basic information, including permission settings.
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.myapp">
<uses-permission android:name="android.permission.CAMERA"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<application
...
>
...
</application>
</manifest>
2.2 Requesting Runtime Permissions
Starting from Android 6.0 (API level 23), when using dangerous permissions, user approval must be requested at runtime. You can use the code below to request permissions.
import android.Manifest;
import android.content.pm.PackageManager;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.Toast;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
public class MainActivity extends AppCompatActivity {
private static final int CAMERA_REQUEST_CODE = 100;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.CAMERA}, CAMERA_REQUEST_CODE);
} else {
// If permission has already been granted
Toast.makeText(this, "Camera permission has been granted.", Toast.LENGTH_SHORT).show();
}
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (requestCode == CAMERA_REQUEST_CODE) {
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Toast.makeText(this, "Camera permission has been granted.", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(this, "Camera permission has been denied.", Toast.LENGTH_SHORT).show();
}
}
}
}
In the above code, the ContextCompat.checkSelfPermission()
method checks if the permission has already been granted, and ActivityCompat.requestPermissions()
requests approval. When the user chooses to allow or deny, the result is handled in the onRequestPermissionsResult()
method.
3. Permission Handling Example
In the example below, we will request camera permission and implement functionality to take a photo if the user grants the request.
import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
public class MainActivity extends AppCompatActivity {
private static final int CAMERA_REQUEST_CODE = 100;
private static final int IMAGE_CAPTURE_REQUEST = 1;
private ImageView imageView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imageView = findViewById(R.id.imageView);
Button button = findViewById(R.id.captureButton);
button.setOnClickListener(v -> {
if (ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.CAMERA}, CAMERA_REQUEST_CODE);
} else {
openCamera();
}
});
}
private void openCamera() {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (intent.resolveActivity(getPackageManager()) != null) {
startActivityForResult(intent, IMAGE_CAPTURE_REQUEST);
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == IMAGE_CAPTURE_REQUEST && resultCode == RESULT_OK) {
Bundle extras = data.getExtras();
Bitmap imageBitmap = (Bitmap) extras.get("data");
imageView.setImageBitmap(imageBitmap);
}
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (requestCode == CAMERA_REQUEST_CODE) {
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
openCamera();
} else {
Toast.makeText(this, "Camera permission has been denied.", Toast.LENGTH_SHORT).show();
}
}
}
}
In this example, the user is prompted to request camera permission, and if granted, the camera app is opened, allowing them to take a picture. The captured image is displayed in the ImageView
.
4. Tips Related to Permissions
When using permissions, consider the following tips:
- Request Minimum Permissions: Since users may not accept them, only request the permissions you need.
- Provide a Clear Reason: It’s a good idea to provide a message that explains why the permission is needed, so users can understand the request.
- Consider User Experience: To avoid disrupting the user’s flow during permission requests, ask for them at appropriate times.
5. Conclusion
In this tutorial, we discussed how to set permissions in Android apps. Since permissions are crucial in protecting user data, it is essential to set and request them correctly. Utilize what you learned in this tutorial when developing your apps in the future.