What is Android?
Android, the widely popular operating system, is the beating heart behind millions of smartphones and tablets globally. Developed by Google, Android is an open-source platform that powers a diverse range of devices, offering users an intuitive and customizable experience. With its user-friendly interface, Android provides easy access to a plethora of applications through the Google Play Store, catering to every need imaginable. From social media and gaming to productivity and entertainment, Android seamlessly integrates into our daily lives, ensuring that the world is at our fingertips. Whether you're a tech enthusiast or a casual user, Android's versatility and accessibility make it a cornerstone of modern mobile technology.
Android Runtime Permissions Example: GitHub Code Sample
Table of Contents
- Introduction
- What Are Android Runtime Permissions?
- Example Overview
- Android Runtime Permissions Example on GitHub
- Explanation of the Code
- Declaring Permissions in the Manifest
- Requesting Permissions at Runtime
- Handling Permission Results
- Conclusion
1. Introduction
When developing Android applications, handling runtime permissions is essential, especially when dealing with sensitive features such as accessing the camera, location, microphone, or storage. Android introduced runtime permissions in Android 6.0 (Marshmallow) to give users more control over their privacy and what apps can access.
In this article, we will walk through an example of how to implement Android runtime permissions in a project, and provide a GitHub repository link where you can find the full code for reference.
2. What Are Android Runtime Permissions?
Runtime permissions in Android allow apps to request access to sensitive features only when they are needed during app execution, rather than during installation. This approach ensures that users have a more transparent experience and can decide whether to grant or deny access to features like:
- Location
- Camera
- Microphone
- Contacts
- Storage
Android classifies permissions into normal (automatically granted) and dangerous (which require runtime approval from the user). For dangerous permissions, the app must check if permission has been granted before accessing the sensitive feature.
3. Example Overview
To demonstrate Android runtime permissions in action, we'll go through a simple example where an Android app requests access to the camera and location permissions. When a user grants the permissions, the app can access these features. If denied, the app will show an explanation or restrict the functionality based on the user’s decision.
We'll provide a GitHub link to a complete example you can refer to for building similar functionality in your app.
4. Android Runtime Permissions Example on GitHub
You can find the full code for this Android runtime permissions example on GitHub. This repository contains a complete app that demonstrates how to request permissions at runtime and handle permission results.
GitHub Repository:
https://github.com/yourusername/Android-Runtime-Permissions-Example
Feel free to clone or fork the repository to experiment with the code!
5. Explanation of the Code
Declaring Permissions in the Manifest
First, you need to declare the permissions your app will use in the AndroidManifest.xml file. For this example, we’ll use the CAMERA and ACCESS_FINE_LOCATION permissions:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.runtimepermissions">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/Theme.AppCompat.Light.NoActionBar">
<!-- Declare the permissions in the manifest -->
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<activity android:name=".MainActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
In the above code:
- We’ve declared the CAMERA and ACCESS_FINE_LOCATION permissions in the manifest file.
- These permissions will be checked at runtime and requested if the app needs to access these features.
Requesting Permissions at Runtime
In the MainActivity.java file, we check whether the permissions are granted or not, and if not, we request them from the user. Here's an example of how to request permissions in MainActivity:
public class MainActivity extends AppCompatActivity {
private static final int CAMERA_PERMISSION_REQUEST_CODE = 101;
private static final int LOCATION_PERMISSION_REQUEST_CODE = 102;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Check if the camera permission is granted
if (ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA)
!= PackageManager.PERMISSION_GRANTED) {
// Request camera permission if it's not granted
ActivityCompat.requestPermissions(this,
new String[]{Manifest.permission.CAMERA},
CAMERA_PERMISSION_REQUEST_CODE);
}
// Check if the location permission is granted
if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION)
!= PackageManager.PERMISSION_GRANTED) {
// Request location permission if it's not granted
ActivityCompat.requestPermissions(this,
new String[]{Manifest.permission.ACCESS_FINE_LOCATION},
LOCATION_PERMISSION_REQUEST_CODE);
}
}
}
Here’s what’s happening in this code:
- We’re checking whether the CAMERA and ACCESS_FINE_LOCATION permissions are granted using
ContextCompat.checkSelfPermission(). - If the permission isn’t granted, we call
ActivityCompat.requestPermissions()to request access to the camera and location.
Handling Permission Results
Once the user responds to the permission request, Android calls onRequestPermissionsResult(). This method handles the results of the permission request. Here's how you can implement it:
@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 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// Camera permission granted, you can now access the camera
Toast.makeText(this, "Camera permission granted", Toast.LENGTH_SHORT).show();
} else {
// Camera permission denied, explain why it’s needed
Toast.makeText(this, "Camera permission denied", Toast.LENGTH_SHORT).show();
}
break;
case LOCATION_PERMISSION_REQUEST_CODE:
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// Location permission granted, you can now access the location
Toast.makeText(this, "Location permission granted", Toast.LENGTH_SHORT).show();
} else {
// Location permission denied, explain why it’s needed
Toast.makeText(this, "Location permission denied", Toast.LENGTH_SHORT).show();
}
break;
}
}
In this part of the code:
- We check the permission result by inspecting the
grantResultsarray. - If the permission is granted (
PackageManager.PERMISSION_GRANTED), we can proceed with accessing the camera or location. - If the permission is denied, we inform the user with a Toast message or even prompt them with an explanation for why the permission is necessary.
6. Conclusion
Handling runtime permissions in Android is crucial for user privacy and app functionality. With the right implementation, users can control what data or features they are willing to share with apps.
By using the code example above, you can implement runtime permissions in your Android app. The provided GitHub repository contains a full working example, so you can easily clone the project and modify it for your needs.
For more advanced handling, consider explaining the reasons for requesting permissions when necessary and offering users a way to change their permission settings later. This will ensure a smooth and transparent user experience while maintaining their trust and privacy.
You can explore more Android topics on GitHub and experiment with handling different permissions in your Android projects.
0 Comments