कुछ मामलों में, हमें पेमेंट गेटवे के लिए रूटेड डिवाइस पर एप्लिकेशन चलाने की अनुमति नहीं देनी चाहिए। यह उदाहरण दर्शाता है कि डिवाइस रूटेड है या नहीं, इसका पता कैसे लगाया जाए।
रूट डिटेक्शन का उदाहरण
चरण 1 - नया प्रोजेक्ट बनाएं
Android Studio में एक नया प्रोजेक्ट बनाएं: File → New Project पर जाएं और आवश्यक विवरण भरें।
चरण 2 - लेआउट XML में कोड जोड़ें
res/layout/activity_main.xml में निम्नलिखित कोड जोड़ें:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="https://schemas.android.com/apk/res/android"
android:id="@+id/parent"
xmlns:tools="https://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:gravity="center"
android:orientation="vertical">
<TextView
android:id="@+id/rootFinder"
android:layout_margin="20dp"
android:textAlignment="center"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
उपरोक्त कोड में, हमने एक TextView लिया है जिसमें रूट के बारे में जानकारी होगी।
चरण 3 - MainActivity.java में कोड जोड़ें
src/MainActivity.java में निम्नलिखित कोड जोड़ें:
package com.example.andy.myapplication;
import android.os.Build;
import android.os.Bundle;
import android.widget.TextView;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
private TextView rootFinder;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
rootFinder = findViewById(R.id.rootFinder);
executeShellCommand("su");
}
private void executeShellCommand(String su) {
Process process = null;
try {
process = Runtime.getRuntime().exec(su);
rootFinder.setText("It is rooted device");
Toast.makeText(MainActivity.this, "It is rooted device", Toast.LENGTH_LONG).show();
} catch (Exception e) {
rootFinder.setText("It is not rooted device");
} finally {
if (process != null) {
try {
process.destroy();
} catch (Exception e) { }
}
}
}
}
उपरोक्त कोड में, हम जांच रहे हैं कि डिवाइस रूटेड है या नहीं और TextView में टेक्स्ट जोड़ रहे हैं। Android डिवाइस रूटेड है या नहीं जांचने के लिए निम्नलिखित कोड उपयोग करें:
executeShellCommand("su");
private void executeShellCommand(String su) {
Process process = null;
try {
process = Runtime.getRuntime().exec(su);
rootFinder.setText("It is rooted device");
Toast.makeText(MainActivity.this, "It is rooted device", Toast.LENGTH_LONG).show();
} catch (Exception e) {
rootFinder.setText("It is not rooted device");
} finally {
if (process != null) {
try {
process.destroy();
} catch (Exception e) { }
}
}
}
ऐप चलाएं
अपने Android डिवाइस को कंप्यूटर से कनेक्ट करें और ऐप चलाएं। परिणाम में दिखाया गया है कि डिवाइस रूटेड नहीं है।
नोट: यह विधि "su" बाइनरी की उपस्थिति की जांच करती है। आधुनिक Android संस्करणों में रूट डिटेक्शन के लिए अधिक मजबूत तरीकों (जैसे SafetyNet/Play Integrity API) का उपयोग करने की सिफारिश की जाती है।