How can we switch off an Android phone programmatically?


Introduction

Android phones are designed with a wide range of features and capabilities, including a Power button that allows users to turn the device on and off. Power button in an android device is used to switch on and switch off the mobile device. In this article we will take a look on How we can switch off our android phone programmatically.

Implementation

We will be creating a simple project in which we will be displaying a simple text view for displaying a heading in which we will be displaying the simple heading. Along with this text view we will be creating a button. On clicking this button we will be switching off our phone.

Step 1 : Creating a new project in Android Studio

Navigate to Android studio as shown in below screen. In the below screen click on New Project to create a new Android Studio Project

After clicking on New Project you will get to see the below screen.

Inside this screen we have to simply select Empty Activity and click on Next. After clicking on next you will get to see the screen below.

Inside this screen we have to simply specify the project name. Then the package name will be generated automatically.

Note − Make sure to select the Language as Java.

After specifying all the details click on Finish to create a new Android studio project.

Once our project has been created we will get to see 2 files which are open i.e activity_main.xml and MainActivity.java file.

Step 3 : Working with activity_main.xml

Navigate to activity_main.xml. If this file is not visible. To open this file. In the left pane navigate to app>res>layout>activity_main.xml to open this file. After opening this file. Add the below code to it. Comments are added in the code to get to know in detail.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
   xmlns:app="http://schemas.android.com/apk/res-auto"
   xmlns:tools="http://schemas.android.com/tools"
   android:layout_width="fill_parent"
   android:layout_height="fill_parent"
   android:orientation="vertical"
   tools:context=".MainActivity">

   <!-- on below line creating a text view for displaying a heading-->
   <TextView
      android:id="@+id/idTVHeading"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:layout_centerInParent="true"
      android:layout_margin="10dp"
      android:padding="4dp"
      android:text="Programatically Switch Off Android Phone"
      android:textAlignment="center"
      android:textColor="@color/black"
      android:textSize="20sp"
      android:textStyle="bold" />

   <!-- creating a button to switch off the device-->
   <Button
      android:id="@+id/idBtnSwitchOff"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:layout_below="@id/idTVHeading"
      android:layout_centerInParent="true"
      android:layout_margin="10dp"
      android:text="Switch Off"
      android:textAllCaps="false" />
</RelativeLayout>

Explanation − In the above code we are creating a Relative layout as a root layout and inside that we are creating a simple text view in which we are displaying a simple text view inside which we are displaying the heading of our application. After this text view we are creating a button which we will be using to switch off our phone.

Step 4 : Working with MainActivity.java file

Navigate to MainActivity.java. If this file is not visible. To open this file. In the left pane navigate to app>res>layout>MainActivity.java to open this file. After opening this file. Add the below code to it. Comments are added in the code to get to know in detail.

package com.example.androidjavaapp;

import androidx.appcompat.app.AppCompatActivity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.ContextWrapper;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.RequestFuture;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.util.concurrent.TimeUnit;

public class MainActivity extends AppCompatActivity {

   // on below line we are creating variable for button.
   private Button switchOffBtn;

   @Override
   protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
      
      // on below line initializing web view with id.
      switchOffBtn = findViewById(R.id.idBtnSwitchOff);
      
      // on below line adding click listener for our switch off button.
      switchOffBtn.setOnClickListener(new View.OnClickListener() {
         @Override
         public void onClick(View v) {
            // on below line we are setting our shut down intent.
            Intent intent = new Intent("android.intent.action.ACTION_REQUEST_SHUTDOWN");
            
            // on below line we are setting confirm t false.
            intent.putExtra("android.intent.extra.KEY_CONFIRM", false);

            // on below line we are adding a flag for new task.
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

            // on below line we are starting a new activity.
            startActivity(intent);
         }
      });
   }
}

Explanation − In the above code we are firstly creating a variable for the button. After that we will get to see an onCreate method. Inside this method we will get to see a set content view method. This method will load the UI from the activity_main.xml file. After that we are initializing the variable for our button from the id which we have specified inside our activity_main.xml file. After that we are adding a click listener for our button. Inside the click listener for our button we are opening a shutdown intent to switch off our phone. Then we are setting flags to our intent and then starting our activity.

After adding the above code now we have to simply click on the green icon in the top bar to run our application on a mobile device.

Note − Make sure you are connected to your real device or emulator.

Output

Note − When you click on the power button the device is switched off.

Conclusion

In the above article we have taken a look on How we can switch off an Android phone programmatically by clicking a button in Android.

Updated on: 30-Mar-2023

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements