Skip to main content

Android - Create Root Checker App in Just 10 minutes

Today, I will show you how easily we can create a root checker app in android. It only takes 10 minutes to create this kind of application.

activity_main.xml
<?xml version="1.0" encoding="utf-8"?><androidx.constraintlayout.widget.ConstraintLayout 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="match_parent"    android:layout_height="match_parent"    tools:context=".MainActivity">    <TextView        android:id="@+id/textView2"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_marginTop="60dp"        android:text="This app checks the rooting status of your phone"        android:textAlignment="center"        android:textSize="18sp"        android:textStyle="bold"        app:layout_constraintEnd_toEndOf="parent"        app:layout_constraintStart_toStartOf="parent"        app:layout_constraintTop_toTopOf="parent" />    <TextView        android:id="@+id/status"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:textAlignment="center"        android:textSize="18sp"        android:textStyle="bold"        android:padding="10dp"        app:layout_constraintBottom_toBottomOf="parent"        app:layout_constraintEnd_toEndOf="parent"        app:layout_constraintRight_toRightOf="parent"        app:layout_constraintStart_toStartOf="parent"        app:layout_constraintTop_toTopOf="parent" />    <Button        android:id="@+id/check"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_marginBottom="96dp"        android:background="#009688"        android:text="Check"        android:textAlignment="center"        android:textColor="#FAFFFFFF"        android:textSize="14sp"        android:textStyle="bold"        android:typeface="sans"        app:layout_constraintBottom_toTopOf="@+id/status"        app:layout_constraintEnd_toEndOf="parent"        app:layout_constraintStart_toStartOf="parent"        app:layout_constraintTop_toBottomOf="@+id/textView2"        app:layout_constraintVertical_bias="1.0" /></androidx.constraintlayout.widget.ConstraintLayout>

MainActivity.xml

package com.app.rootcheckerapp;import androidx.appcompat.app.AppCompatActivity;import android.graphics.Color;import android.os.Bundle;import android.view.View;import android.widget.Button;import android.widget.TextView;public class MainActivity extends AppCompatActivity implements View.OnClickListener {    private Button check;    private TextView status;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        check  = findViewById(R.id.check);        status = findViewById(R.id.status);        check.setOnClickListener(this);    }    @Override    public void onClick(View v) {        if (Helper.isRooted())        {            status.setBackgroundColor(Color.parseColor("#228B22"));            status.setTextColor(Color.parseColor("#FFFFFF"));            status.setText("Device is Rooted");        }        else        {            status.setTextColor(Color.parseColor("#FFFFFF"));            status.setBackgroundColor(Color.parseColor("#B22222"));            status.setText("Device is Not Rooted");        }    }}

The above code is pretty easy. So in MainActivity we have a Check button. On clicking that button, I am calling a helper class method to check the rooting status of the device. This method returns true/false value. If true that means Device is Rooted otherwise not rooted.

And also I am setting the color of the background based on the returned value. If the device is rooted the text background color will be shown in green color otherwise background color will be red.


Helper.java

package com.app.rootcheckerapp;import android.util.Log;public class Helper {public static boolean isRooted(){    Process process = null;    try {        process = Runtime.getRuntime().exec("su");        return true;    }    catch (Exception e)    {        Log.d("Exception", e.toString());         return false;    }    finally {        // Destroy process        if (process != null)        {            process.destroy();        }    }}}

So in the above code, I have written the actual logic. So let’s understand it.

Runtime.getRuntime().exec(“su”);

The above line will execute the shell command su
It is similar to when we execute commands in terminal in Linux


The su stands for superuser. So basically when your device is rooted and you execute su command then it tells the android system and requests for the root access.

But in case if your device is not rooted then it throws an exception
java.io.IOException: Cannot run program “su”: error=13, Permission denied

The exec method returns the object of Process class. So if your android device is not rooted then Runtime.getRuntime().exec(“su”); method will return null . But in case if your android device is rooted then it will return the process object. So it is your duty to destroy the object of process.

That’s why in finally block we are destroying the process by calling process.destroy() method.

How to test the application


Well you can test it in android real devices. But if you don’t have two devices (where one is rooted and the other one is not – rooted). Then we can test it in android emulators too.

Many people say that android emulators are not rooted. But in reality android emulators before lollipop or API Level 21 are actually rooted. So from API level 21 onwards they are not rooted.

Android App Rooted
Android KitKat Emulator
Android App Non rooted
Android Oreo Emulator

Source Code

https://github.com/vaibhavsharma316/TechieVaibhavTutorials/tree/master/RootCheckerApp

Thanks for reading 🙂 🙂

Comments

Popular posts from this blog

Automation - Update Naukri Profile Using Selenium

Recently one of my friend came to me with a problem. He is looking out for new job but he feels quite boring to update  his profile on daily basis. As some people says updating profile in the morning gives you more calls as it keeps the newly updated profile on top (Although i don’t know whether naukri works this way or not 😀 ). As i was more interested to solve his problem.  After listening his problem i came to solution that instead of updating it manually lets make this job automatic. And it is quite interesting how we can automate our daily boring task with automation. Another day i came with the solution . And the solution was to make it automatic using selenium (Those who are not aware about selenium do check this link) In short, Selenium is a Testing automation Framework. And it is for automating web applications for testing purposes, but is certainly not limited to just that. Boring web-based administration tasks can (and should) also be automated as well....

Mi Selfie Stick Tripod (Bluetooth Remote) Review

Hello Readers, Today in this article, I am going to review Mi Selfie Stick Tripod (Bluetooth Remote) . I have purchased this selfie stick 4 months back. So I thought I should share my experience with this selfie stick. Before getting into details let me list down some of its features and specification. Features Tripod Detachable Bluetooth remote Light and compact Aluminum rod 360-degree rotating phone holder Specs Color: Black & White Weight: 155 grams Version: Android 4.3 or higher, iOS 5.0 or higher Bluetooth version: 3.0 Battery life: up to 50,000 clicks and charging time 1 to 2 hours Review Looks: The product looks premium and also lightweight. The best thing it can hold any smartphone easily. I have tested this with mi and oneplus5 and this works great. Tripod: It also comes with built-in tripod stand so you fix your smartphone and click pictures using Bluetooth remote. You can also use this tripod to record youtube videos etc. Quality & Weight: Overall I am impressed with...

Shut Down the Computer using notepad

Open notepad >> Write @echo off msg * I am tired. shutdown -c “So, Bye Bye dear” -s >> Save the File as AnyName.bat When you will open this file, your Computer will Shut down on its own after showing the message in it. NOTE: FOR EDUCATIONAL PURPOSE ONLY.I AM NOT RESPONSIBLE FOR IF ANYTHING GOES WRONG….