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....

OpenCV - Drawing a Circle with python

In this tutorial, I will show you how to draw a circle in OpenCV. OpenCV comes with lots of built-in functions that make our life easier especially when we are working on image processing .  And there is a function called circle in OpenCV which is used to draw a circle. This function takes following parameters: 1. Image : Takes an image object 2. Center : Center point coordinates 3. Radius : Radius of the circle 4. Color : Takes color in BGR format 5. Thickness : By default set to 1 (optional) 6. Line Type : By default set to 8-connected.It can also be  LINE_AA  or  4-connected  (Optional) 7. Shift:  Shifts fractional bits in the point coordinates of center and radius (Optional) Especially when we are creating closed shapes. Thickness plays an important role. Here if the negative thickness is passed in this function or any other function which creates a closed shape(rectangle, polygon, etc). Then it creates a filled shape (filled with color). Negative Thickness Filled Circle Positive ...

PS3 - Dragon Ball Z Battle Of Z - RPCS3 Setting

Hi GuyZ, Today, I am going to show you, how to play PS3 games on PC using the RPCS3 Emulator. It is very easy to set up and play PS3 Games. I got to know about this emulator some time ago and I thought I should give it a try . Initially I was not sure that whether it would work or not. But at last, it worked. I found it very easy to set up and play PS3 games . Although this emulator is still in the development stage so you may encounter some errors. I haven’t got any errors. In this tutorial, I am using the PS3 PKG file. This emulator also supports dumping ps3 disc and ISO Images. Prerequisites: RPCS3 Emulator PS3 Update File Microsoft Visual C++ 2019 Redistributable PS3 .pkg and Rap File Important Tip Before downloading any game make sure it comes under the playable section of rpcs3 compatibility. To check which game you can play using RPCS3 click HERE. Games that are showing in the Playable Section are actually the games that are in running state and can be played using RPCS3 E...