How to fix crashing application while getting superuser permission in android [duplicate] - java

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

Related

How do I open a URL on the client machine using java

I am trying to open a static url from a web application when user clicks a button on a screen. Our application is deployed on a linux box and using the below program its trying to open a browser. Can you please advise how I can get it to to open it on the client instead ?
All our users access this application from windows.
try {
Desktop desktop = java.awt.Desktop.getDesktop();
URI oURL = new URI("http://www.google.com");
desktop.browse(oURL);
} catch (Exception e) {
e.printStackTrace();
}
I'm 99% sure above solution works only for windows, for unix I believe you should try something like this:
Runtime runtime = Runtime.getRuntime();
runtime.exec("/usr/bin/firefox -new-window " + url);

.msi file launched with java closes after a few seconds.

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.

Using InetAddress to get own IP

I have an issue were if I try to InetAddress.getLocalHost() or even InetAddress.getByName(String host) it throws an exception every time even for a known website like nba.com I am a bit confused FYI the target device is an android 4.1.1 GS3 and wifi and mobile network are on. Code below
try{
InetAddress ownIP=InetAddress.getLocalHost();
System.out.println("IP of my Android := "+ownIP.getHostAddress());
}catch (Exception e){
System.out.println("Exception caught ="+e.getMessage());
String t = e.getMessage() + "yes";
}
Below is the System.out
03-12 18:59:52.636: I/System.out(18996): Exception caught =null
Thanks in advance
I use a tricky method to get my own IP. you can see whether it helps you
String getIP() {
try {
Socket socket = new Socket("google.com", 80);
return socket.getLocalAddress().getHostAddress();
} catch (Exception e) {
}
}
I believe I have found out what my issue was basically I guess you are not allowed to perform any network operations in the Main thread for android this was optional before and is now required for Honeycomb (API 11) and up below is the comment as per google specs.
"To prevent blocking the main user interface thread, Google recommends using the AsyncTask class from the android.os package:"
So all I was create a new class NetTask which extends AsyncTask and perform all network applications so now my code is working. IDK if everybody else knew that but I figured I would post this in case any newbs like me were still looking for a solution :) !!!
Thanks

How to disable system dialog window or pop ups in android?

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.

How to control the handset using AT commands in java

I know that by using AT commands we can control the handset.As example unlocking screen we can give a specific AT command or moving right to the menu or left or bottom or up we can give specific AT commands. What all are the AT commands for doing this kind of control.
Thank you.
From what I understand, the AT commands are more used for phone-type functions (making calls, or sending SMS, etc), rather than menu navigation, etc.
I'm not entirely sure if that was your end-goal after menu navigation, but you can find more details here: http://en.wikipedia.org/wiki/Hayes_command_set (the original +AT command set)
If you wanted to send SMS from a handset connected to your computer you might want to take a peek at this page: http://www.developershome.com/sms/atCommandsIntro.asp
If you wanted more control when performing functions, like sending SMS, etc, you might want to investigate "PDU Mode."
It is entirely possible that some handset manufacturers may have implemented additional +AT commands to allow other functions to be performed, so you might do better by specifically searching for the commands related to the handset you are using.
(Of course, if you're having issues connecting to the handset hardware itself, you need to ensure you have either the javax.comm extension or some favoured Java USB API installed)
If post doesn't help, perhaps you could provide more details in your question? (eg. what you are ultimately trying to do, if you think it would help)
List of AT commands
sample java code to use AT command
public void servicesDiscovered(int transID, ServiceRecord serviceRecord[])
{
String url = serviceRecord[0].getConnectionURL(1, false);
try
{
//ClientSession conn= (ClientSession)Connector.open(url);
StreamConnection meineVerbindung = (StreamConnection) Connector.open(url);
if(conn== null)
System.out.println("Kann Service URL nicht oeffnen\n");
else
{
OutputStream out = conn.openOutputStream();
InputStream in = conn.openInputStream();
String message = "AT+CGMI\r\n";
// send AT-command
System.out.println("send AT Comand request: "+message);
out.write(message.getBytes());
out.flush();
out.close();
byte buffer[] = new byte[10000];
// read the response from mobile phone
in.read(buffer);
System.out.println("AT Comand response: "+buffer.toString());}
}
catch(IOException e)
{
System.out.println("Service Error(3): "+e.getMessage());
}
}

Categories

Resources