Make button reboot phone (Rooted Phone) - android

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.

Related

adb shell settings put secure sysui_nav_bar commands on Android Oreo 8.0 not works without root

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
}
}
});

SDK Android : How to open Shutdown/Reboot dialog for the device

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);

how to work with sockets, activities and views?

I'm new to programming in android and I need help to build an application. I want two phones to connect to each other, one being the client and the other being the server. I want the client to have 2 stages. In the first the user would input the ip of the server y click on a button to stablish the connection. In the second one the user would input a message and click on a button to send it to the server.
The code shown bellow is to send the string "message" to the server but as I was saying I want the user to be able to input the string. I don't know how to tackle his issue, do I need a second activity to be called once the connection is established in the activity I show bellow? In that case I wouldn't know how to pass a socket to another activity, I only know how to pass strings. Besides, I would need another button and therefore a new OnClickListener and I would still need to pass the socket to that function.
Without using a second activity I don't know how to make the second input field (the one where the user would input the message to send to the server) to show up once the connection is established. The views (layout.xml) for this activity are already associated to the same, I can't just clear the screen and create a new EditText field on the fly.
I hope I made myself clear.
Thanks in advance
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_client);
serverIp = (EditText) findViewById(R.id.server_ip);
connectPhones = (Button) findViewById(R.id.connect_phones);
connectPhones.setOnClickListener(connectListener);
}
private OnClickListener connectListener = new OnClickListener() {
#Override
public void onClick(View v) {
if (!connected) {
serverIpAddress = serverIp.getText().toString();
if (!serverIpAddress.equals("")) {
Thread cThread = new Thread(new ClientThread());
cThread.start();
}
}
}
};
public class ClientThread implements Runnable {
public void run() {
try {
InetAddress serverAddr = InetAddress.getByName(serverIpAddress);
Socket socket = new Socket(serverAddr, ServerActivity.SERVERPORT);
connected = true;
while (connected) {
try {
PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket
.getOutputStream())), true);
**out.println("messageToSend");**
} catch (Exception e) {
Log.e("ClientActivity", "S: Error", e);
}
}
socket.close();
} catch (Exception e) {
Log.e("ClientActivity", "C: Error", e);
connected = false;
}
}
}
}
I don't think , you need a second activity to send message over socket.
Anyways , It depends on you design --
you can have two editTexts at a same but their visibility would be different.
EditText serverIpAddressET;
EditText messageET ; // Deafult visibility Gone
serverIpAddressET can be visible when user has not provided socket IP.
Once connection is established , you can change the visibility of serverIpAddressET to the Gone and make Visible the messageET .
Well a quick fix would be to hide the currently Displayed EditText and Button via
view.setVisiblility(View.GONE)
and display the other editText and button via
view.setVisibility(View.Visible)
and use same activity...
But a better solution would be to open the socket for communication in a service look in to android Service with Sockets... start the service in first activity and when connection is established shift too second activity and bind the activity with service so there can be communication btw activity and service...
Not entirely sure what you're looking for here but if I understand correctly you are woundering how to send a message. Simply add
message2send= (EditText) findViewById(R.id.user_message);
and then where you have
**out.println("messageToSend");**
replace with
**out.println(message2send);**
and add an extra edit text field to your layout.
It should be noted that the way you've gone about this is not ideal as the other phone (server) would have to be listening to the socket in order to receive the message which would cause additional data charges as well as wasting battery power (traditonal in mobile a server is between the 2 phones to store the message in case user if offline or phone is dead)
Hope this helps non the less!

Restart android phone (emulator)?

I want to restart the mobile when I press a button.For that i wrote these..
PowerManager pm;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
Button test = (Button) findViewById(R.id.button1);
test.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
try {
Process proc = Runtime.getRuntime().exec(new String[] {"su", "-c", "reboot" });
int result = proc.waitFor();
Toast.makeText(ForTestActivity.this, "INt:"+result, Toast.LENGTH_SHORT).show();
} catch (Exception ex) {
Log.i("XXXXXXXXXXXX", "Could not reboot", ex);
}
}
});
}
This is not working in emulator.I'm using android2.3.3 for develop this application.
It does not give any errors and nothing happened after I press the button.
The toast was displaying result as 1.
After that I tried this code also..
Process p = Runtime.getRuntime().exec("su");
DataOutputStream os = new DataOutputStream(p.getOutputStream());
os.writeBytes("reboot"+"\n");
os.writeBytes("exit\n");
os.flush();
Same result.I added necessary permissions also.Is someone having any idea ?
[Somehow I want to restart (or Shutdown) the phone after press a button.]
Have you tried the above command without the "su -c"?
When I type "adb shell reboot" my phone reboots (but my phone is rooted which is needed in this case) so I think effect should be similar if you just execute "reboot" from code. I think "su -c" is unnecessary as you have root rights already on rooted phone (IIRC emulator is rooted by default).
As other have mentioned, this will not work on unrooted phone.
A similar question was asked and answered here. You can't reboot an unrooted phone until you have the firmware-key to sign it.

VPN connection from Android emulator fails

I am new to Android development. I am trying to join an existing VPN from my Android app. I want to integrate the VPN in my app; my app then is supposed to query a remote database.
I got some code and tried to use it to create a VPN. It emulates the built-in VPN manager on the Android phone. The code compiles and the manager is launched but the connection to the VPN does not succeed when I try to connect after all configuration. The protocol is PPTP. A VPN exists and has been tested.
I tried connecting from an android phone with the same settings and it was successful.
I thought maybe I am passing the parameters in the wrong way. I have put the code for the vpn part below. The url is not the actual but in same format.
Any help to identify what I am doing wrong would be appreciated. Also if there is a way I can directly call the VPN manager from my app.
Thanks very much for any help,
final Button button1 = (Button)findViewById(R.id.button1);
final Button button2 = (Button)findViewById(R.id.button2);
final Button button3 = (Button)findViewById(R.id.button3);
button1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
startActivity(new Intent("android.net.vpn.SETTINGS"));
}
});
button2.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
URL url = null;
try {
String registrationUrl = String.format("daffy.zune.org");
url = new URL(registrationUrl);
URLConnection connection = url.openConnection();
HttpURLConnection httpConnection = (HttpURLConnection) connection;
int responseCode = httpConnection.getResponseCode();
if (responseCode == HttpURLConnection.HTTP_OK) {
Log.d("MyApp", "Registration success");
} else {
Log.w("MyApp", "Registration failed for: " + registrationUrl);
}
} catch (Exception ex) {
ex.printStackTrace();
}
}
});
button3.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
setContentView(R.layout.register);
}
});
}
}
I suggest you look at the simplevpn project and browse the source here and view ShowAllVPNsActivity.java.
Hope this helps! We need more good VPN apps in the Market!

Categories

Resources