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?
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
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.
Hey guys,
Let me start by introducing myself. I am Alcanzer, a small time, new to the game, android developer. I'm working on a project that requires Bluetooth Pan to access the internet from a raspberry pi. I tried a few code snippets from Stack Overflow but I keep running into exceptions regarding tethering permissions. I set the permissions in manifest but I think I need to request them programmatically. Does anyone know how to request tethering permissions programmatically?(I would've posted my StackOverflow link here, but I'm not sure about the XDA rules regarding outgoing links).
I'm happy to provide any other information regarding this problem of mine.
Thank you!
Tethering
Since Android 6.0 (API 23) users grant permissions to apps while the app is running, not when they install the app.
You need at least two permissions:
CHANGE_NETWORK_STATE
WRITE_SETTINGS
Example code to ask permissions:
Code:
ActivityCompat.requestPermissions(new String[]{Manifest.permission.WRITE_SETTINGS}, MY_PERMISSIONS_REQUEST_WRITE_SETTINGS);
// override onRequestPermissionResult method in your activity
@Override
public void onRequestPermissionsResult(int requestCode,
String permissions[], int[] grantResults) {
switch (requestCode) {
case MY_PERMISSIONS_REQUEST_WRITE_SETTINGS: {
if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// permission granted
} else {
// permission denied
}
}
}
}
FSP Dev said:
Since Android 6.0 (API 23) users grant permissions to apps while the app is running, not when they install the app.
You need at least two permissions:
CHANGE_NETWORK_STATE
WRITE_SETTINGS
Example code to ask permissions:
Code:
ActivityCompat.requestPermissions(new String[]{Manifest.permission.WRITE_SETTINGS}, MY_PERMISSIONS_REQUEST_WRITE_SETTINGS);
// override onRequestPermissionResult method in your activity
@Override
public void onRequestPermissionsResult(int requestCode,
String permissions[], int[] grantResults) {
switch (requestCode) {
case MY_PERMISSIONS_REQUEST_WRITE_SETTINGS: {
if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// permission granted
} else {
// permission denied
}
}
}
}
Click to expand...
Click to collapse
Thanks for your fast reply, and sorry for my late one.
Just one question, would these 2 permissions enable me to use the Bluetooth pan/tethering? Forgive my naivety but camera has permission.camera, internet has permission.internet and so on.
Thank you!
Bump!
Its first time i make xposed module,there is no error on log of xposed.
However ,it didn't work, color didn't change.
What should i do?
Code:
public class Tutorial implements IXposedHookZygoteInit {
@Override
public void initZygote(IXposedHookZygoteInit.StartupParam startupParam) throws Throwable {
XResources.setSystemWideReplacement("android", "color", "system_bar_background_semi_transparent",Color.parseColor("#FF000000"));
XposedBridge.log("Things gone right");
}
}
Sorry i made some mistake ,please delete the thread,thanks