Determining if an Android device is rooted programmatically?
Rooting detection is a cat and mouse game and it is hard to make rooting detection that will work on all devices for all cases.
See Android Root Beer https://github.com/scottyab/rootbeer for advanced root detection which also uses JNI and native CPP code compiled into .so native library.
If you need some simple and basic rooting detection check the code below:
/**
* Checks if the device is rooted.
*
* @return <code>true</code> if the device is rooted, <code>false</code> otherwise.
*/
public static boolean isRooted() {
// get from build info
String buildTags = android.os.Build.TAGS;
if (buildTags != null && buildTags.contains("test-keys")) {
return true;
}
// check if /system/app/Superuser.apk is present
try {
File file = new File("/system/app/Superuser.apk");
if (file.exists()) {
return true;
}
} catch (Exception e1) {
// ignore
}
// try executing commands
return canExecuteCommand("/system/xbin/which su")
|| canExecuteCommand("/system/bin/which su") || canExecuteCommand("which su");
}
// executes a command on the system
private static boolean canExecuteCommand(String command) {
boolean executedSuccesfully;
try {
Runtime.getRuntime().exec(command);
executedSuccesfully = true;
} catch (Exception e) {
executedSuccesfully = false;
}
return executedSuccesfully;
}
Probably not always correct. Tested on ~10 devices in 2014.
How can you detect if the device is rooted in the app?
At the end of the day, you can't. A rooted device may be modified in any way, and thus can completely hide whatever it wants from you. In practice you could look at some of the standard root builds to find features they have or characteristics you can look at... but there is no way to guarantee that whatever you do will actually detect a "rooted" device.
How to programmatically turn on (Power-On) the android device at a certain time?
Here is a solution(tested on Lenovo A516, API LEVEL 17, but it should work similar for other devices with "Scheduled power On/Off feature" like described here):
Uninstall /system/app/SchedulePowerOnOff.apk
Compile and install this code
enableAlertPowerOn(...) is the function where the magic happens:
private static void enableAlertPowerOn(Context context, long atTimeInMillis){
AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(context, SchPwrOnReceiver.class);
PendingIntent sender = PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);
am.set(7, atTimeInMillis, sender);
}
SchPwrOnReceiver.java:
package com.mediatek.schpwronoff.receivers;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
public class SchPwrOnReceiver extends BroadcastReceiver {
private static final int STALE_WINDOW = 1800;
private static final String TAG = "SchPwrOnReceiver";
@Override
public void onReceive(Context context, Intent intent) {
//TODO optional
}
}
build.gradle(:app), applicationId has to be "com.mediatek.schpwronoff":
apply plugin: 'com.android.application'
android {
compileSdkVersion 29
defaultConfig {
applicationId "com.mediatek.schpwronoff"
minSdkVersion 17
targetSdkVersion 29
versionCode 17
versionName "4.2.2"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
implementation 'com.android.support:support-compat:28.0.0'
}
AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" android:versionCode="17" android:versionName="4.2.2" package="com.mediatek.schpwronoff">
<application
android:name=".MYApplication"
android:allowBackup="false"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity"
android:launchMode="singleTop">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name=".receivers.SchPwrOnReceiver">
<intent-filter>
<action android:name="com.android.settings.schpwronoff.PWR_ON_ALERT"/>
</intent-filter>
</receiver>
</application>
</manifest>
Example how to call enableAlertPowerOn(Context context, long atTimeInMillis) from Activity:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
showDateTimePickerPowerOn(this);
}
public void showDateTimePickerPowerOn(Context context) {
final Calendar currentDate = Calendar.getInstance();
date = Calendar.getInstance();
new DatePickerDialog(context, new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
date.set(year, monthOfYear, dayOfMonth);
new TimePickerDialog(context, new TimePickerDialog.OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
date.set(Calendar.HOUR_OF_DAY, hourOfDay);
date.set(Calendar.MINUTE, minute);
date.set(Calendar.SECOND, 0);
long cTimeInMillis = date.getTimeInMillis();
enableAlertPowerOn(context, cTimeInMillis);
}
}, currentDate.get(Calendar.HOUR_OF_DAY), currentDate.get(Calendar.MINUTE), true).show();
}
}, currentDate.get(Calendar.YEAR), currentDate.get(Calendar.MONTH), currentDate.get(Calendar.DATE)).show();
}
Related Topics
Android Listview with Onclick Items
Android:Fill Spinner from Java Code Programmatically
How to Get Retrofit Success Response Status Codes
Mobile Vision API - Concatenate New Detector Object to Continue Frame Processing
How to Join Png with Alpha/Transparency in a Frame in Realtime
Get the Distance Between Two Geo Points
How to Pass Multiple Primitive Parameters to Asynctask
What Are the Differences Between Activity and Fragment
How to Know That My Webview Is Loaded 100%
Dagger 2.10 Android Subcomponents and Builders
Why Are These Permissions Being Refused
What Causes Android's Contentresolver.Query() to Return Null
Android Toggle Button Custom Look
Gcm Service_Not_Available on Android 2.2
How to Get App's Permission for Each App? How to Do It Programmatically on Android
Android: Test Push Notification Online (Google Cloud Messaging)
Android: How to Check If a Particular Accessibilityservice Is Enabled
How to Know When the Recyclerview Has Finished Laying Down the Items