Permissions
Android controls what applications are allowed to do by requiring that they ask for permission to perform critical actions. When the application is installed on a real device, the user can choose whether to allow the requested permissions and proceed with installation, or to reject installation (in the emulator environment, it is assumed all permission requests are granted). It is important to include only the permissions needed by your application; you don’t want to ask the user for permissions that you don’t need. For MJAndroid, we’ll need the permissions shown in this tab:
Types of Permissions
1. Install-Time Permissions: If the Android 5.1.1 (API 22) or lower, the permission is requested at the installation time at the Google Play Store.

If the user Accepts the permissions, the app is installed. Else the app installation is cancelled.
2. Run-Time Permissions: If the Android 6 (API 23) or higher, 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 use the feature, the app requests permission again.
So, now the permissions are requested at runtime. In this article, we will discuss how to request permissions in an Android Application at run time.
Steps for Requesting permissions at run time
Step 1: 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.
<!–Declaring the required permissions–>
<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” />
Step 2: Modify activity_main.xml file to Add 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 it.
<!–Button to request storage permission–>
<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”/>
<!–Button to request camera permission–>
<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=”true”/>
Step 3: Check whether permission is already granted or not. If permission isn’t already granted, request 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 not, then the permissions are requested.
Check for permissions: Beginning with Android 6.0 (API level 23), the user has the right to revoke permissions from any app at any time, even 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
}
Request Permissions: When PERMISSION_DENIED is returned from the checkSelfPermission() method in the above 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);
Services to application
A service is a component that runs in the background to perform long-running operations without needing to interact with the user and it works even if the application is destroyed. A service can essentially take two states −
| Sr.No. | State & Description |
| 1 | StartedA service is started when an application component, such as an activity, starts it by calling startService(). Once started, a service can run in the background indefinitely, even if the component that started it is destroyed. |
| 2 | BoundA service is bound when an application component binds to it by calling bindService(). A bound service offers a client-server interface that allows components to interact with the service, send requests, get results, and even do so across processes with interprocess communication (IPC). |
A service has life cycle callback methods that you can implement to monitor changes in the service’s state and you can perform work at the appropriate stage. The following diagram on the left shows the life cycle when the service is created with startService() and the diagram on the right shows the life cycle when the service is created with bindService(): (image courtesy : android.com )

To create a service, you create a Java class that extends the Service base class or one of its existing subclasses. The Service base class defines various callback methods and the most important are given below. You don’t need to implement all the callbacks methods. However, it’s important that you understand each one and implement those that ensure your app behaves the way users expect.
| Sr.No. | Callback & Description |
| 1 | onStartCommand()The system calls this method when another component, such as an activity, requests that the service be started, by calling startService(). If you implement this method, it is your responsibility to stop the service when its work is done, by calling stopSelf() or stopService() methods. |
| 2 | onBind()The system calls this method when another component wants to bind with the service by calling bindService(). If you implement this method, you must provide an interface that clients use to communicate with the service, by returning an IBinder object. You must always implement this method, but if you don’t want to allow binding, then you should return null. |
| 3 | onUnbind()The system calls this method when all clients have disconnected from a particular interface published by the service. |
| 4 | onRebind()The system calls this method when new clients have connected to the service, after it had previously been notified that all had disconnected in its onUnbind(Intent). |
| 5 | onCreate()The system calls this method when the service is first created using onStartCommand() or onBind(). This call is required to perform a one-time set-up. |
| 6 | onDestroy()The system calls this method when the service is no longer used and is being destroyed. Your service should implement this to clean up any resources such as threads, registered listeners, receivers, etc. |
The following skeleton service demonstrates each of the life cycle methods −
package com.apjtuts;
import android.app.Service;
import android.os.IBinder;
import android.content.Intent;
import android.os.Bundle;
public class HelloService extends Service {
int mStartMode;
IBinder mBinder;
boolean mAllowRebind;
@Override
public void onCreate() {
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
return mStartMode;
}
@Override
public IBinder onBind(Intent intent) {
return mBinder;
}
@Override
public boolean onUnbind(Intent intent) {
return mAllowRebind;
}
@Override
public void onRebind(Intent intent) {
}
@Override
public void onDestroy() {
}
}