Ping app - Android Q&A, Help & Troubleshooting

hey guys,
Building an app that runs a ping command at the moment and I can't quite get it to work. If I modify the command to something that isn't a terminal command then it'll output my error statement but i can't get it to display the ping output. any help would be awesome. I know my outputs for my error are bad but it's an easy way to determine what path it's outputting.
package com.mycompany.myapp;
import android.app.*;
import android.os.*;
import android.view.*;
import android.widget.*;
import java.io.*;
import java.lang.Process;
public class MainActivity extends Activity
{
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
TextView Text = new TextView(this);
Runtime runtime = Runtime.getRuntime();
try
{
Process proc = runtime.getRuntime().exec("system/bin/ping 192.168.1.1");
BufferedReader in = new BufferedReader(
new InputStreamReader(proc.getInputStream()));
String line = "null";
while ((line = in.readLine()) != null) {
Text.setText(in.readLine());
}
}
catch (Exception e)
{
String line="55";
Text.setText(line);
}
setContentView(Text);
}
}
Thanks for any help you guys can give me,
Adam

Hey,
Try using "system/bin/ping -c 1 192.168.1.1" instead.
And then add this line just after that:
Code:
proc.waitFor();
and while reading the output of the ping you might want to do it like this maybe?
Code:
String line = "";
String result = "";
while (line != null)
{
result = result + "\n" + line;
line = in.readLine();
}
Text.setText(result);
If you want to ping more than 1 packet I think it would be better to make a new thread and do proc.waitFor() in that thread. Then send a message using a handler to set the output of the ping to the TextView

k i have done that and that does make more sense but Im still getting a black screen on the output. i am testing on a Sony tab s and using AIDE (on the device) cause my eclipse is broken. this is how my code looks now,
package com.mycompany.myapp;
import android.app.*;
import android.os.*;
import android.view.*;
import android.widget.*;
import java.io.*;
import java.lang.Process;
public class MainActivity extends Activity
{
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
TextView Text = new TextView(this);
Runtime runtime = Runtime.getRuntime();
try
{
Process proc = runtime.getRuntime().exec("system/bin/ping 192.168.1.1");
BufferedReader in = new BufferedReader(
new InputStreamReader(proc.getInputStream()));
String line = "null";
while ((line = in.readLine()) != null) {
Text.setText(in.readLine());
}
}
catch (Exception e)
{
String line="55";
Text.setText(line);
}
setContentView(Text);
}
}
Is there anything else I could be missing??
Thanks,
Adam

Hey, I think you posted the same code again.
I am not sure if you can read the process stream before it is complete and ping does take a long time to complete. So your main thread is blocking on it and it doesn't get to executing the setContentView.
Thats why I think going for a seperate thread is a better option.

so it would be better to put the ping into a new class and call on it when i need it??

Not a seperate class, a seperate thread to be more specific.
Even if you do put the ping code in a seperate class' method and call that method in onCreate it will still run on the your applications main thread.
What I was trying to say is something along the lines of the following code:
Code:
private TextView textView;
private Process process;
private Handler handler;
private Thread pingThread;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
textView = new TextView(this);
textView.setText("Pinging...");
setContentView(textView);
try
{
process = Runtime.getRuntime().exec("system/bin/ping -c 5 192.168.1.1");
handler = new Handler()
{
@Override
public void handleMessage(Message msg)
{
try
{
BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
String line = "";
String result = "";
while(line != null)
{
result = result + "\n" + line;
line = reader.readLine();
}
reader.close();
textView.setText(result);
}
catch(Exception e)
{
e.printStackTrace();
textView.setText("Error");
}
}
};
pingThread = new Thread()
{
@Override
public void run()
{
try
{
process.waitFor();
}
catch(Exception e)
{
e.printStackTrace();
}
handler.sendEmptyMessage(0);
}
};
pingThread.start();
} catch (Exception e)
{
e.printStackTrace();
}
}
I am not sure if this is exactly how you want your app to behave. There might be a better way of doing what you want than what I have suggested but I tested the above code and it works for me.

thats wicked, basically what want my program to do is to run a ping command through a usb to rj45 adapter. at the moment I just need it to do the ping command through wifi and once that was working I was going to set it up to go through usb.

I wonder if there's something I'm doing wrong like I'm build the apk in AIDE on the tablet or something, cause I'm still getting a black screen when booting the app.
If I error the code up a bit, Like put "system/bin/pi ....." instead I do get pinging on the app but no error output and if the code is fine then nothing displays

That is very strange :S.
If you use the incorrect command it works fine but when you use the correct command it doesn't?
Can you post your code? or provide more details if possible?

the code is just the code you posted cause I thought if a I could get it working with that code then modify it to what it needs to do, when the command is incorrect it displays "ping....." but doesn't display an error, I'm going to install eclipse and build the package with that and see how it goes. did you test on a tablet and what did you use to deploy the package?

Adz117 said:
the code is just the code you posted cause I thought if a I could get it working with that code then modify it to what it needs to do, when the command is incorrect it displays "ping....." but doesn't display an error, I'm going to install eclipse and build the package with that and see how it goes. did you test on a tablet and what did you use to deploy the package?
Click to expand...
Click to collapse
I think i know whats happening. It isnt the Ide and using eclipse wont make much of a difference.
Are you usIng something like -c 5 in the ping command? Cause if you aren't i think ping is Going to take a really long time and all you'll see on the screen is "Pinging..."

Yea I have got that in the command, when the code is correct and working I dont get anything all I get is a black screen. Its when I error the command up a bit that I get pinging
Sent from my Sony Tablet S using XDA

hey guys,
just got eclipse running and tested it on an avd emulator and it runs perfect, so the question is what would cause it not to run on my tablet?

I have no idea! I don't have access to an Android Tablet, so I can't test it out! I tested it out on my phone too and it works just fine.
One thing I can suggest though is:
Put log statements throughout the program to signify where the the control has reached. Then run it on your tablet. That should shed some light on where the code is failing on your tablet.

tested it on my phone which is running 2.3 and it failed on there as well, "ping......" would pop up for about a second then disappear, also if I use a command like ls instead of ping on my tablet it will work perfectly fine so I'm guessing for some reason past android 2.1 it doesn't like the ping command. any ideas?

I tested it out on phone with 2.2. Did you try putting log statements and checking where it is failing.
You could also add breakpoints in your code and run it?

Related

Need help troubleshooting my code.

Ok so for these last three days i have been trying to get into the android game. I did the hello android tutorial and yea. that was boring lol, so i decided to try and create a program to temporarily fix the keyboard backlight issue being experienced by some ICS port users. i have only part of the code done but it does not execute at all. I am not sure whats the problem. I have writted additional pieces to this code but have not put them in the program as i want to figure out why it doesnt run before i add more and then clean it up.
Code:
package com.dri94.led;
import java.util.*;
import java.io.*;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
public class LEDLightActivity extends Activity {
/** Called when the activity is first created. */
@SuppressWarnings("null")
@Override
public void onCreate(Bundle savedInstanceState) {
final int SDK_INT;
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Scanner input = new Scanner(System.in);
DataOutputStream os = null;
TextView tv = new TextView(this);
tv.setText("Enter 'y' to turn on keyboard light or 'n' to turn it off");
String yOrN = input.next();
if (yOrN == "y") {
tv.setText("Enter SDK number 7 for GB devices or 14 for ICS devices. No other devices are supported at this time");
SDK_INT = input.nextInt();
if (SDK_INT == '7') {
try {
os.writeBytes("echo 255 > /sys/class/leds/keyboard-backlight/brightness\n"
+ "chmod 444 /sys/class/leds/keyboard-backlight/brightness\n");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
else {
try {
os.writeBytes("echo 255 > /sys/class/leds/kpd_backlight_en\n"
+ "chmod 444 /sys/class/leds/kpd_backlight_en\n");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
Any logcat output? I don't have ICS so those paths aren't available on my device, but similar paths are symlinks and directories. Does your app have write permissions to kpd_backlight_en (or whatever the symlink points to)?
You have a lot of problems there, lets point some of them out,
Code:
DataOutputStream os = null;
Youre initializating your outputStream as null, wich is a problem consideering you use it for writing a file. Also there are so many easier ways to write files, for example, I propose this simple writing method
Code:
public static void WriteFile(String text, String file) {
try{
FileWriter fstream = new FileWriter(file);
BufferedWriter out = new BufferedWriter(fstream);
out.write(text);
out.close();
}catch (Exception e){
//Deal exception ;D
}
}
simple code usage will be then
Code:
WriteFile("255", "/sys/class/leds/keyboard-backlight/brightness");
Second error I found, comparing strings with "==", this
Code:
if (yOrN == "y")
is wrong, you should try with
Code:
if (yOrN.equals("y")) {
Another thing i dont understand is this
Code:
final int SDK_INT;
Why do you declare a variable as final, if you are gonna assign some value later, right here
Code:
SDK_INT = input.nextInt();
Last thing I found is also comparing an integer with string? I dont understand what you do there
Code:
if (SDK_INT == '7')
Also your way to manage user inputs would be better with a simple button (or toggleButton) for turn on/off lights, or even use SensorEventListener.
I hope I have helped you in some , just tell me if you need something. Good luck!
How should i initialize it? And thank you alot. Ima play with my code tomorrow. The last one though is comparing it with a character value. but this post helped alot. I appreciate it... Especially cause i made soooo many beginner mistakes. My professor would be disappointed
Sent from my XT862 using T
Questions or Problems Should Not Be Posted in the Development Forum
Please Post in the Correct Forums
Moving to Q&A
thanks i wasnt sure where to post this! ill remember that from now on

[Q] Ping from Android Code Help

Hi,
Im trying to get an ARM binary of netcat running on my droid. I have compiled this already and it is contained in the data/local/tmp folder on the device. The netcat commands also work correctly via adb shell.
However, im trying to execute commands from code, netcat wasnt working so I decided to start with a basic command like ping. My code below is an attempt to ping my laptop from the phone.
Again this worked from adb shell but doesnt seem to work from the code. I captured in wireshark and no packet came from the phone when the code was ran.
The code enters the try block and displays the toast message but it seems that the ping is not working.
Can anyone tell me why the ping is not working? Once I fix that part I can move onto other commands.
Also, I have tried the .waitFor command to wait for the command to run but this gives an error in eclipse before compiling.
Thanks
Code:
package com.mossy.netcat;
import java.io.IOException;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
import android.widget.Toast;
public class NetcatActivity extends Activity {
/** Called when the activity is first created. */
 [user=439709]@override[/user]
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
TextView text = new TextView(this);
text.setText("Netcat");
setContentView(text);
try
{
String ping = "system/bin/ping -c 1 192.168.0.13";
Runtime.getRuntime().exec(ping);
Toast.makeText(getApplicationContext(), "In Netcat Section", Toast.LENGTH_SHORT).show();
}
catch(IOException e) {
System.out.println("Error Executing Netcat");
Toast.makeText(getApplicationContext(), "In Exception Section", Toast.LENGTH_SHORT).show();
e.printStackTrace();
}
}
}
Bump!!
Anyone at all have any suggestions on this?
Thanks

[Q] How to change date text in dropdown statusbar on LP

on 4.4 and earlier android. I use
Code:
findAndHookMethod("com.android.systemui.statusbar.policy.DateView", lpparam.classLoader, "updateClock" new XC_MethodHook() {
@Override
protected void afterHookedMethod(MethodHookParam param){
mDateView = (TextView) param.thisObject;
mDateView.setText("Hello");
}
});
to change the date text(e.g "December 1" to "Hello").
The class name and function name should be hooked are not changed, but this method not works on LP.
I'm trying to find another way to make it but failed. I'm not good at programming, so I need your help!

[Q] Access to actual visible app resources

Hello,
I'm new in xposed and I want get R.color from actual visible application.
So I read I can try XposedHelpers.findAndHookMethod with onResume
But I don't have any idea how do that.
Now I have:
Code:
XposedHelpers.findAndHookMethod(Class.forName(packageName), "onResume", new XC_MethodHook() {
@Override
protected void afterHookedMethod(MethodHookParam param) throws Throwable {
Object mPhoneStatusBar = param.thisObject;
Context mContext = (Context) XposedHelpers.getObjectField(mPhoneStatusBar, "mContext");
Resources res = mContext.getResources();
int resId = mContext.getResources().getIdentifier("accent", "color", packageName);
Log.d("ColorDetector", "color: " + res.getColor(resId));
}
}
);
But I know it can't work :-/
Any ideas how I can get it?
Kris Groove said:
Hello,
I'm new in xposed and I want get R.color from actual visible application.
So I read I can try XposedHelpers.findAndHookMethod with onResume
But I don't have any idea how do that.
Now I have:
Code:
XposedHelpers.findAndHookMethod(Class.forName(packageName), "onResume", new XC_MethodHook() {
@Override
protected void afterHookedMethod(MethodHookParam param) throws Throwable {
Object mPhoneStatusBar = param.thisObject;
Context mContext = (Context) XposedHelpers.getObjectField(mPhoneStatusBar, "mContext");
Resources res = mContext.getResources();
int resId = mContext.getResources().getIdentifier("accent", "color", packageName);
Log.d("ColorDetector", "color: " + res.getColor(resId));
}
}
);
But I know it can't work :-/
Any ideas how I can get it?
Click to expand...
Click to collapse
Mm, ehm... This class doesn't exists in runtime, I think... Anyway, you aren't able to reference it. What are you trying to do?
Andre1299 said:
Mm, ehm... This class doesn't exists in runtime, I think... Anyway, you aren't able to reference it. What are you trying to do?
Click to expand...
Click to collapse
I want get color of it or status bar in 5.0 android and up to recolor PIE
Hello, did you get this to work? I believe I am in a similar situation right now and this might help!

[Q] How can I access my preference?

I created a Xposed module with a SettingsActivity. Users can set what they want in SettingActicity.
Now I want to read my preference in hooking process.So I wrote these in SettingActicity:
Code:
getPreferenceManager().setSharedPreferencesMode(MODE_WORLD_READABLE);
getPreferenceManager().setSharedPreferencesName("com.me.myapp");
addPreferencesFromResource(R.xml.pref_general);
And these in a class which used to hook method:
Code:
@Override
public void initZygote(IXposedHookZygoteInit.StartupParam startupParam) throws Throwable {
prefs = new XSharedPreferences("com.me.myapp");
prefs.makeWorldReadable();
}
@Override
protected void afterHookedMethod(MethodHookParam param) throws Throwable {
prefs.reload;
if (prefs.getInt("policy", -1) == 1) { // policy is a keyname which is defined in PreferenceScreen
// do something
}
But it can't read anything.So it always return default value.
And I got prefs.getAll().size() is 0.
I checked /data/data/com.me.myapp/shared_prefs, there is a xml file named com.me.myapp.xml .Its permission is 664.But I still can't read my preference.
Any ideas to solve it? Thanks in advance.
PS:I'm using Android Studio to build this module.

Categories

Resources