I'm trying to use this "set of commands" on Android Oreo but i have some issue. For testing i'm using this command on my Nexus 5x:
adb shell settings put secure sysui_nav_bar "space,recent;home;back,space"
So i decided to implement this command inside my app and try without root help. In the app manifest i added the permission to write secure settings:
<uses-permission android:name="android.permission.WRITE_SECURE_SETTINGS"/>
Then in my MainActivity i added a button to run the command that you read before.
testButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
try {
Process process = Runtime.getRuntime().exec("settings put secure sysui_nav_bar \"space,recent;home;back,space\"");
} catch (Exception e) {Toast.makeText(getApplicationContext(), "" + e, Toast.LENGTH_LONG).show();}
}
});
Once my app was built i ran it on my 5x and via adb i typed this command to allow to write secure settings:adb shell pm grant com.customizer.smart.batterysavercustomizer android.permission.WRITE_SECURE_SETTINGS and this command was excuted without errors. But when i try to tap on my "testButton" nothing happened and 0 erorrs inside androidmonitor.
Last try that i did was with root help. I edited my preview command button:
testButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
try {
Process process = Runtime.getRuntime().exec("su -c settings put secure sysui_nav_bar \"space,recent;home;back,space\"");
} catch (Exception e) {Toast.makeText(getApplicationContext(), "" + e, Toast.LENGTH_LONG).show();}
}
});
When i tapped on my testButton the app asked to garant root permission, and it works. But how is possible that on the same phone the app "Custom navigation bar" app that uses the same adb command works without root ?.
I followed this: guide on XDA
You should use Settings class instead of Runtime.getRuntime().exec()
testButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if (ContextCompat.checkSelfPermission(context, Manifest.permission.WRITE_SECURE_SETTINGS) == PackageManager.PERMISSION_GRANTED) {
Settings.Secure.putString(context.getContentResolver(), "sysui_nav_bar", valueToSave);
} else {
//Write secure Settings permission not granted
//Show instructions about how to grant it via ADB
}
}
});
I'm new here.
I have a problem, i try to shutdown a 4.2.2 android device (not root).
I know that ACTION_SHUTDOWN not works with unroot device.
So i want to open the existing shutdown/reboot/airplane dialog, the same we get when we maintain the on/off button. Then the user just have to click shutdown button.
I try to create by this way, without result...
Intent intent = new Intent(Settings.ACTION_DISPLAY_SETTINGS); // or others settings
startActivity(intent);
Thanks,
The is no public sdk access to open the power button menu programatically.
This link has all the approches Here.Simulating power button press to display switch off dialog box
InputManager.getInstance().injectInputEvent(new InputEvent(KeyEvent.KEYCODE_POWER, keyCode), sync);
'sync' becomes either of these:
InputManager.INJECT_INPUT_EVENT_MODE_WAIT_FOR_FINISH
InputManager.INJECT_INPUT_EVENT_MODE_WAIT_FOR_RESULT
and you need
import android.hardware.input.InputManager;
This is untested, but puts you in the right direction, also bare in mind, functionality like this is NOT recommend.
failing that:
public static void simulateKey(final int KeyCode) {
new Thread() {
#Override
public void run() {
try {
Instrumentation inst = new Instrumentation();
inst.sendKeyDownUpSync(KeyCode);
} catch (Exception e) {
Log.e("Exception when sendKeyDownUpSync", e.toString());
}
}
}.start();
}
and simply call it like
simulateKey(KeyEvent.KEYCODE_POWER);
I know there are already questions about that here, but I tried all the answers given without success.
There's a simple CheckBoxPreference (titled "Root"):
<CheckBoxPreference
android:key="root"
android:title="#string/root"
android:summary="#string/root_summary"
android:defaultValue="false" />
Now I need to set the OnPreferenceChangeListener on it and gain root access. If so the checkbox should be checked, otherwise it should not:
public class Settings extends PreferenceActivity implements OnPreferenceChangeListener {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.settings);
findPreference("root").setOnPreferenceChangeListener(this);
}
#Override
public boolean onPreferenceChange(Preference preference, Object newValue) {
String key = preference.getKey();
if ("root".equals(key) && !((CheckBoxPreference) preference).isChecked()) {
try {
Process p = Runtime.getRuntime().exec("su");
p.waitFor();
Log.d("Settings", Integer.toString(p.exitValue()));
if (p.exitValue() == 255) {
Log.d("Settings", "###NO ROOT###");
return false;
}
} catch (Exception e) {
Log.d("Settings", "###NO ROOT###");
Log.d("Settings", e.getMessage());
return false;
}
Log.d("Settings", "!!!ROOT!!!");
}
return true;
}
}
Superuser prompts correctly for root access. Denying however also returns true, as exitValue is 1 (???) and allowing freezes the whole app (I guess at p.waitFor).
I'm currently running Superuser 3.1.3 with su binary 3.1.1 (newest versions).
Taking a look into logcat I can see the following message: Activity pause timeout for ActivityRecord{42c0ebb8 com.example/.gui.Settings}
The command you're running is just su which will, I suspect, run a shell as superuser. You're waiting (indefinitely) for that shell to finish.
You need to specify su some-command-here-which-needs-to-run-as-root.
Unfortunately, there is no way to achieve superuser permissions for the Java code within your Android project. The root-ness applies only to commands which are spawned by su itself.
Hi I am in need of some help I am working on an app where I want the to user click a button and the phone reboots. My problem is when I click the button it gives a super user request but does not reboot. My code is:
final Button button = (Button) findViewById(R.id.button1);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Perform action on click
try {
Runtime.getRuntime().exec("su");
Runtime.getRuntime().exec("reboot");
} catch (IOException e) {
}
}
});
}
}
Is there anything I am doing wrong? If anyone could help i would really appreciate it.
You create two different shells this way. Assign the process to some variable and grab its IO streams:
Process p = Runtime.getRuntime().exec("su");
InputStream is = p.getInputStream();
// ...
Then write the command directly.
Note that this will not work on unrooted device. Avoid this if possible.
In Android 2.3 and below, you could make an application full screen, and then "hijack" the menu/back/search buttons by just returning false onKeyDown()... and registering the app as a default home launcher application, that way, there's no way to exit the application.
In Android 3.0 (Honeycomb) the navigation buttons (System Bar) is always present, I'd like to hide it. Is it possible?
FYI, I am not publishing this application on the Android Market. This is an internal application for devices that are going to be used internally, I need to secure the device.
Since this is not possible to do using a public API, I have found a way to do it in a very "hack-ish" way that requires a rooted device.
Update: as user864555 pointed below, this is another solution
$ adb remount
$ adb shell mv /system/app/SystemUI.odex /system/app/SystemUI.odexold
$ adb shell mv /system/app/SystemUI.apk /system/app/SystemUI.apkold
$ adb reboot
"That code disable the app SystemUI which is the actually menu bar. Which that modification, you will also gain the space of that system bar. But make sure to have a back button or something to exit."
That works great as well. Please vote for his answer. I will try to keep this one updated as much as I can.
Update: Here's a third method. A way to do it programmatically or using the command line. Found here: http://android.serverbox.ch/?p=306
This method requires root access, but you don't need to change the LCD Density, keeping the same as the original, and you can get the UI nav bar back really quick and easy without having to reboot everytime.
The blog post also shows how to implement it on your Android application, remember it requires root, and it might not be a great idea to do so unless your application is running on a kiosk or your own device, please do not implement this method on an app that's published in the Android market or anywhere public.
To stop/remove/disable the system bar (need to be su before issuing this command):
$ service call activity 79 s16 com.android.systemui
To restore the system bar just simply issue this command:
$ am startservice -n com.android.systemui/.SystemUIService
It's that easy. Hopefully ICS gets released soon along with the source code so that anyone can build Android for our Kiosk tablets.
You cannot hide the system bar on Android 3.0.
If you have access to system file, you can do this (mine is unlocked and rooted, so i'm not sure what you need, I haven't tried with a factory fresh xoom):
adb shell
cd /system/app/
mv SystemUI.odex SystemUI.odexold
mv SystemUI.apk SystemUI.apkold
exit
adb reboot
That code disable the app SystemUI which is the actually menu bar. With that modification, you will also gain the space of that system bar, but make sure that you have a back button or something to exit in your app.
Edit:
If you have problems with read-only file, you mint need to mount the /system directory as read-write. To do so, use this command in adb shell (Source: http://forum.xda-developers.com/showthread.php?t=1159495&page=5)
mount -o remount,rw /dev/block/stl6 /system
You can remount it as read-only using that command:
mount -o remount,ro /dev/block/stl6 /system
Edit:
This methods allow the soft keyboard to be displayed normally when needed.
Here is related code with my previous answer. It automatically hide the status bar and show it back again when finish. Important: to show it back again, the code have to restart system_server which take some time to boot again and during that time, you will see the honeycomb booting animation. That's the only I find for now to show the statusbar again. Restarting SystemUI is not enought. And because of that, it will shutdown your app when restart system_server.
This code need a rooted os with superuser installed on.
package com.projects;
import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.io.InputStreamReader;
import java.io.PrintStream;
import android.app.Activity;
import android.app.AlertDialog;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TableLayout.LayoutParams;
// http://www.stealthcopter.com/blog/2010/01/android-requesting-root-access-in-your-app/
public class FullScreenTestActivity extends Activity implements Button.OnClickListener {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
try
{
Process p;
p = Runtime.getRuntime().exec("su");
// Attempt to write a file to a root-only
DataOutputStream os = new DataOutputStream(p.getOutputStream());
os.writeBytes("mount -o remount,rw /dev/block/stl6 /system\n");
os.writeBytes("mv /system/app/SystemUI.odex /system/app/SystemUI_Old.odex\n");
os.writeBytes("mv /system/app/SystemUI.apk /system/app/SystemUI_Old.apk\n");
os.writeBytes("mount -o remount,ro /dev/block/stl6 /system\n");
// Close the terminal
os.writeBytes("exit\n");
os.flush();
p.waitFor();
new AlertDialog.Builder(this)
.setIconAttribute(android.R.attr.alertDialogIcon)
.setMessage("Android Honeycomb StatusBar removed successfully!")
.show();
// Set action for exiting.
Button cmdExit = new Button(this);
cmdExit.setText("Exit");
cmdExit.setOnClickListener(this);
this.addContentView(cmdExit, new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
}
catch (Exception e)
{
ShowErrorGlobal(e);
}
}
public void onClick(View v) {
try
{
Process p;
p = Runtime.getRuntime().exec("su");
// Attempt to write a file to a root-only
DataOutputStream os = new DataOutputStream(p.getOutputStream());
os.writeBytes("mount -o remount,rw /dev/block/stl6 /system\n");
os.writeBytes("mv /system/app/SystemUI_Old.odex /system/app/SystemUI.odex\n");
os.writeBytes("mv /system/app/SystemUI_Old.apk /system/app/SystemUI.apk\n");
os.writeBytes("mount -o remount,ro /dev/block/stl6 /system\n");
String systemServerPID = GetSystemServerPID();
if (systemServerPID != null)
os.writeBytes("kill " + systemServerPID + "\n");
// else ... manual reboot is required if systemServerPID fail.
// Close the terminal
os.writeBytes("exit\n");
os.flush();
p.waitFor();
}
catch (Exception e)
{
ShowErrorGlobal(e);
}
}
public String GetSystemServerPID()
{
try
{
Process p = Runtime.getRuntime().exec("ps -n system_server");
p.waitFor();
BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream()));
reader.readLine(); // Skip header.
return reader.readLine().substring(10, 16).trim();
}
catch (Exception e)
{
return null;
}
}
protected void ShowErrorGlobal(Exception e)
{
ByteArrayOutputStream baos = new ByteArrayOutputStream();
PrintStream stream = new PrintStream( baos );
e.printStackTrace(stream);
stream.flush();
new AlertDialog.Builder(this)
.setIconAttribute(android.R.attr.alertDialogIcon)
.setTitle("Epic fail")
.setMessage("Error: " + new String( baos.toByteArray() ))
.show();
}
}
Although this doesn't answer the question of 'locking' the screen, you can hide the status bar without being root by using the setSystemUiVisibillity api(API level 11).
Some pseudocode:
public MyActivity extends Activity {
#Override
public void onCreate(Bundle savedInstance) {
//...
final View mainView = findViewById(R.id.you_main_view_id);
mainView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LOW_PROFILE);
//Register a listener for when the status bar is shown/hidden:
final Context context = getApplicationContext();
mainView.setOnSystemUiVisibilityChangeListener(new View.OnSystemUiVisibilityChangeListener () {
#Override
public void onSystemUiVisibilityChange(int visibility) {
if ((visibility == View.SYSTEM_UI_FLAG_VISIBLE)) {
//Do stuff here...pause the video/game?
} else {
//Do other stuff here..resume the video/game?
}
}
});
}
}
This will hide the status bar until the user clicks along the lower edge of the screen, in which case the status bar will get shown (it'll get hidden again after a few seconds).
Make sure you've specified targetSdkVersion="11" or higher in your manifest.
The application HideBar can be used to hide the system bar on android tablets (HC, ICS, JB). It contains an optional kiosk mode that can be used to lock down tablets completely and also other options like a hidden back button. It is GPL software. Contact the developer (me, see email on applications website) if this application has to be installed in large volumes.
For others who have had this problem:
If you haven't set the android:targetSdkVersion properly in your AndroidManaifest.xml file, setSystemUiVisibility has no effect (unlike other advanced APIs which work whether or not the targetSDKVersion has been set properly or not).
I had accidentally left my targetSdkVersion at 8. Bumping it up to 16 immediately caused setSystemUIVisiblity to have the desired effect.
Yes it is possible to do if you have root access on the device.
This code can hide and show the StatusBar by killing it`s proccess and calling it back again.
package com.example.statusbar;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
public class MainActivity extends Activity {
String commandToExecute;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_main);
commandToExecute = "/system/xbin/su";
executeShellCommand(commandToExecute);
Button btHideStatusBar = (Button) findViewById(R.id.buttonHide);
Button btShowStatusBar = (Button) findViewById(R.id.buttonShow);
btHideStatusBar.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
commandToExecute = "/system/xbin/su -c /system/bin/service call activity 42 s16 com.android.systemui";
executeShellCommand(commandToExecute);
}
});
btShowStatusBar.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
commandToExecute = "/system/xbin/su -c /system/bin/am startservice -n com.android.systemui/.SystemUIService";
executeShellCommand(commandToExecute);
}
});
}
private boolean executeShellCommand(String command) {
try {
Runtime.getRuntime().exec(command);
return true;
} catch (Exception e) {
return false;
}
}
}
As of 4.4 you can do this (this question is very old, but always comes up on this topic):
setSystemUiVisibility(View.SYSTEM_UI_FLAG_IMMERSIVE)
https://developer.android.com/training/system-ui/immersive.html
if you want to hide navigation bar through out the application then here is the most simplest way.
just write this code in your application tag in manifest file
> <Application
> android:theme="#android:style/Theme.Black.NoTitleBar.Fullscreen" <!--
> other parameters of application tag-->
> >
android:theme="#android:style/Theme.Black.NoTitleBar.Fullscreen
or
mainView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LOW_PROFILE)
No, the task bar is still there, with three gray dots instead classic icons