I am finding out a way to switch off my tablet automatically.
Right now when we long press power button, I get a confirmation for shutdown (Cancel or Ok).
Is there a way to programmatically switch off the device without confirmation ?
Is this possible?
No. Suitably rooted phones/tablets often have access to su/reboot commands, but for an off-the-shelf, commercially available device, no: there is no way to programatically shut it down.
This is a dicey one! As an app, you cant do much, but there is one way you can try this. Get a phone which is rooted and grants your application SuperUser permissions. Then you could try to run this piece of code from your APK.
Process mProcess = null;
DataOutputStream osStream;
try {
mProcess = Runtime.getRuntime().exec("su");
} catch (IOException e) {
Log.e("Error","Unable to get SU permissions, quitting");
}
osStream = new DataOutputStream(mProcess.getOutputStream());
try {
osStream.writeBytes("reboot");
Thread.sleep(1000);
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
Not a tested piece of code, but hopefully should give you some idea!
Although you can't really invoke the shutdown programmatically in non-rooted device, there's a way how to disable the confirmation dialog that occurs when you long-press the power button. There's a secret code
*#*#7594#*#*
or
*#7594#
which changes the power button behaviour - enables direct power off once the code enabled. You need to choose this code via default dialpad. Works on most Android phones.
Here's the list of some other secret codes.
Related
I am attempting to automate a few steps on an Android emulator using the espresso driver with appium. There is a pop-up window that appears when trying to allow location permission that the Espresso driver can't see. I'm currently attempting to force a couple enter clicks using the following code:
Robot robot;
try {
robot = new Robot();
robot.keyPress(java.awt.event.KeyEvent.VK_ENTER);
robot.keyRelease(java.awt.event.KeyEvent.VK_ENTER);
} catch (AWTException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
But using this only gets me the following error:
java[61315:813119] pid(61315)/euid(765933194) is calling TIS/TSM in non-main thread environment, ERROR : This is NOT allowed. Please call TIS/TSM in main thread!!!
All of this is on a Macbook, and my test code is in IntelliJ with the emulator launched from Android Studio. I have tried disabling the main thread scanner in xcode, but that doesn't seem to extend to IntelliJ or appium. Is there a way for me to force apple to ignore this check or to force this block of code to the "main thread"?
If your problem is about how to get through the location permission dialog, you can use GrantPermissionRule which allows you to grant permission in your tests.
#Rule public GrantPermissionRule permissionRule = GrantPermissionRule.grant(android.Manifest.permission.ACCESS_FINE_LOCATION);
I am trying to launch MySql server installer which is in my resources folder but it terminates after a few seconds. However if I launch it manually it runs okay until the end. Below is my code.
Thread t = new Thread(new Runnable() {
#Override
public void run() {
// TODO Auto-generated method stub
try {
ClassLoader classloader = Thread.currentThread().getContextClassLoader();
String fileUrl = classloader.getResource("mysql.msi").getFile();
Runtime rf = Runtime.getRuntime();
Process pf = rf.exec("msiexec /i \"\\" + fileUrl + "\"");
} catch (Exception e) {
// System.out.println(e.toString()); // not necessary
e.printStackTrace();
}
}
});
t.start();
Okay, it was just an advice, lets come to your case, Windows OS has certain set of security restrictions which allows only administrator to install or remove any application.
That is why, we see a promt window asking for Administrator password (or Admin's permission as YES/NO type, in case user has logged in as admin), and the promt screen is the heart of it's security, as it don't allow ANY OTHER APPLICATION TO HAVE CONTROL ON IT.
If you do a remote desktop via third party, you will never see the client machines promt screen (this is because of security constraints), so in your case, your java application is third party app which don't have enough permission to continue the operation further.
Hence it closes after few seconds.
How ever, you can start and stop already installed services by allowing permission once in your windows service control. So I was suggesting you to play with service only.
im trying to find the method behind having superuser access on android devices. basically i want my app to be able to remove system files, and so asking the user if its ok after the app is launched like most other apps, and then what code exactly am i looking to use to perform these sorts of actions? cant find much on google
thanks in advance!
If the device is rooted, you can use the following code to gain access to a root shell:
try
{
Process process = Runtime.getRuntime().exec("su");
DataOutputStream outputStream = new DataOutputStream(process.getOutputStream());
DataInputStream inputStream = new DataInputStream(process.getInputStream());
outputStream.writeBytes(command + "\n");
outputStream.flush();
outputStream.writeBytes("exit\n");
outputStream.flush();
process.waitFor();
}
catch (IOException e)
{
throw new Exception(e);
}
catch (InterruptedException e)
{
throw new Exception(e);
}
Where "command" is the command that you want to execute.
If I'm getting it right you want to grant your application root privileges. This is not possible with the standard Android image which is installed on the devices. The su command does not support changing to root.
You can however load a customized rooted ROM on your device other than that I'm afraid there is no solution.
Here you can find a clear illustration about how to gain root permissions
i'm using SPP profile for connect to my device:
Set<BluetoothDevice> devices = ba.getBondedDevices();
for(BluetoothDevice bd : devices)
{
String name = bd.getName();
if(name.equals("CELLMETER"))
{
try
{
BluetoothSocket bs = bd.createRfcommSocketToServiceRecord(UUID.fromString("00001101-0000-1000-8000-00805F9B34FB"));
bs.connect();
} catch (IOException e)
{
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
}
}
}
All seems okay, i created function where i'm closing input output buffers and close socket.
But when application crashes or i'm stopping application when breakpoints arrives socket doesn't closes, even after i kill process manually and it's not avalible for new connection from new instance of app.
What i'm doing wrong? For each crash/debug operation i have to reboot phone :(
It's manifested only to Android 2.3.5 (Samsung 5830i) and on Android 4.0.4 (Freelander P10). On my Android 4.2.1 (Galaxy Nexus) all okay, after app crash connection closes automatically. (it seems because there is new Bluetooth stack)
I can see 2 options to work that out:
1- Add an UncaughtExceptionHandler in your app, best in Application-derived class:
mUEHandler = new Thread.UncaughtExceptionHandler()
{
#Override
public void uncaughtException(Thread t, Throwable e)
{
// Close any opened sockets here
defaultUEH.uncaughtException(t, e);
}
};
Thread.setDefaultUncaughtExceptionHandler(mUEHandler);
But that only takes care of app crashes. If user kills the app, won't get in there at all.
2- Store some socket identification that allow you to close it when app restarts.
It's not perfect, but that could work-around your issue.
I solved this problem by letting my BluetoothSockets be managed by a Service running in its own process. I open, close, read, and write the sockets by passing Messages to and from the Service. If the app crashes, the Service shuts down cleanly, closing the sockets. (It does not shut down cleanly if it's running in the same process as the app.)
I had seen many questions on stack but no one is telling if a Fail to connect to camera service will occur, how to get rid of this RuntimeException.
I have an camera application its working fine, I already take care to release the resources but if somehow user install the other application which not releasing the resources properly, my application facing RuntimeException: Fail to connect to camera Service and hence got crashed, want to avoid this situation.
If i click on original camera application it shows me a AlertDialog
Camera error: Cannot connect to camera.
That's what i exactly want to handle this. I am trying this code to handle it but cant succeed yet.
try {
camera = Camera.open();
camera.setDisplayOrientation(90);
} catch (RuntimeException e) {
// TODO: handle exception
Log.d("Inside RunTime exception", e+"//");
camera.setErrorCallback(errorCallback);
reConnectCameraVideo();
} catch(Exception e) {
finish();
}
but camera object returning null on camera.setErrorCallback because it wont open.
setErrorCallback() cannot be used for the case where the Camera will not open. You appear to be trying to still use the Camera -- AFAIK this is impossible until the user reboots their phone if some other app leaked the Camera. Simply display your own message to that effect.
Also:
Use an error logging service, like ACRA, Flurry, BugSense, etc.
Never blindly finish an activity due to an exception, as in your last catch block. Always do something to let the user and/or you (via the error logging service) know about the exception