Okay, I am beyond confused here. I'm new to Java/Android programming, and I'm teaching myself, so bear with me.
Basically, my entire onCreate
is made up of a giant if/else statement. I have a couple methods that check if various applications are installed on the device, and then I have a method called RunAsRoot
that simply executes a shell command as su. Here's the full onCreate
:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
CharSequence message = null;
Button button = (Button) findViewById(R.id.button1);
boolean installed = appInstalledOrNot("x");
if(installed) {
message = "whateverhere";
button.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v) {
RunAsRoot(commands);
}
});
}
else {
message = "whateverhere";
button.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v) {
Toast.makeText(getApplicationContext(), "You don't have packagenamehere installed, sorry!", Toast.LENGTH_LONG).show();
}
});
}
TextView textView = (TextView )findViewById(R.id.textView);
textView.setTextSize(20);
textView.setText(message);
}
The whole thing is pretty basic... If the package is installed, okay cool, do the command. If it isn't installed, too bad, you don't get to run the commands. And it works great. But here's my problem... I would like to check if a device is rooted at the start of the app. If it is rooted, proceed to the if/else that I posted above. If not rooted, setContentView
to a different layout. I can't figure anything out! Literally nothing is working.
I have a method called isRooted()
that checks if su is present in /system/xbin
. But I can't figure out how to incorporate that into my if/else statement. I'd like it to check for root before anything else happens, and if root isn't found, just switch the bloody layout and that's it. But everything crashes due to a NullPointerException.
Here is isRooted()
:
public boolean isRooted() {
try {
Process process = new ProcessBuilder()
.command("/system/xbin/su")
.redirectErrorStream(false)
.start();
} catch (IOException e) {
return true;
}
return false;
}
I guess I'm just confused as to where to go from here. Should I just wrap my entire if/else in another if/else that checks for root? And I guess another thing is, how do I call isRooted()
? I think I'm doing it wrong:
if (isRooted()) {
//everything here
}
else {
setContentView(R.layout.notrooted);
}
Logcat output shows that the NullPointerException is occurring in my onCreate, which I assume is directly due to the if (isRooted())
Any suggestions on where I should go from here?
TL;DR: I want to check if I can run something as root, and if I can, continue program, if I can't, change the layout and just stop.
EDIT: Here is all I'm getting out of LogCat:
07-24 09:00:51.257: E/AndroidRuntime(16282): Process: com.test.app, PID: 16282
07-24 09:00:51.257: E/AndroidRuntime(16282): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.test.app/com.test.app.MainActivity}: java.lang.NullPointerException
07-24 09:00:51.257: E/AndroidRuntime(16282): at com.test.app.MainActivity.onCreate(MainActivity.java:126)