Android version: 4.1 .
I want to test my apk without pc, so i write android apk to do it.
Process proc = Runtime.getRuntime().exec(command);
try {
if (proc.waitFor() != 0) {
System.err.println("exit value = " + proc.exitValue());
// proc.waitFor();
}
BufferedReader in = new BufferedReader(new InputStreamReader(
proc.getInputStream()));
StringBuffer stringBuffer = new StringBuffer();
String line = null;
while ((line = in.readLine()) != null) {
stringBuffer.append(line+"\n");
}
// proc.waitFor();
System.out.println(stringBuffer.toString());
Log.i("info",stringBuffer.toString());
return stringBuffer.toString();
} catch (InterruptedException e) {
System.err.println(e);
}
and the command is
am instrument -w -e deleteCase no com.calculator.test/com.xx.test.runner.Runner1
but it's not worked.
Related
i try read the log generated from "CallNotifier" and "PhoneUtils" but i can't,
i use two ways for this: both are thread (AsyncTask)
1:
try {
Process process = Runtime.getRuntime().exec("logcat");
BufferedReader bufferedReader = new BufferedReader(
new InputStreamReader(process.getInputStream()));
String line = "";
while ((line = bufferedReader.readLine()) != null) {
if (line.contains("CallNotifier")) {
publishProgress(line);
}
}
}
catch (IOException e) {
}
on this way i read all catlog but i filter if the line contains "CallNotifier"
2:
try {
Process process = Runtime.getRuntime().exec("logcat -s CallNotifier");
BufferedReader bufferedReader = new BufferedReader(
new InputStreamReader(process.getInputStream()));
String line = "";
while ((line = bufferedReader.readLine()) != null) {
publishProgress(line);
}
}
catch (IOException e) {
}
This way i filter in
exec("logcat -s CallNotifier");
Don't work
When I filter tag generated from my app the code works. if i filter from PC in terminal i can see log from CallNotifier the problem its when i filter from Android.
I am trying to read content of /sys/kernel/debug/tracing/trace file from my code but i am unable to do so.I have tried mounting using
mount -o rw,remount -t debugfs nodev /sys/kernel/debug
and have also done chmod -R 777 /sys/kernel/debug
File myFile = new File("/sys/kernel/debug/tracing/trace");
FileInputStream fIn = null;
try {
fIn = new FileInputStream(myFile);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
BufferedReader myReader = new BufferedReader(
new InputStreamReader(fIn));
String aDataRow;
try {
while ((aDataRow = myReader.readLine()) != null) {
// Toast.makeText(getBaseContext(), aDataRow, Toast.LENGTH_LONG).show();
t.setText(aDataRow);
break;
}
} catch (IOException e) {
e.printStackTrace();
}
You need to use a "su" command in your program.
Process process = Runtime.getRuntime().exec(new String[] { "su", "-c", "cat /sys/kernel/debug/tracing/trace" });
BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
String line;
while ((line = reader.readLine()) != null) {
I want to use shell command, 'cat'.
I want to copy the "abc.jpg" file..But It didn't operate.
What is the problem? Thank you.
public void onClick(View v) {
Runtime runtime = Runtime.getRuntime();
Process process;
try {
String cmd = "cat /sdcard/0/Pikicast/abc.jpg>/sdcard/0/apk_backups/abc.jpg";
process = runtime.exec(cmd);
BufferedReader br = new BufferedReader(new InputStreamReader(process.getInputStream()));
String line;
while ((line = br.readLine()) != null) {
Log.i("test",line);
}
} catch (Exception e) {
e.fillInStackTrace();
Log.e("Process Manager", "Unable to execute top command");
}
}
I have modified your code a little. Kindly try this.
String[] command = {"cat","/sdcard/0/Pikicast/abc.jpg>/sdcard/0/apk_backups/abc.jpg"};
StringBuilder cmdReturn = new StringBuilder();
try {
ProcessBuilder processBuilder = new ProcessBuilder(command);
Process process = processBuilder.start();
InputStream inputStream = process.getInputStream();
int c;
while ((c = inputStream.read()) != -1) {
cmdReturn.append((char) c);
}
prompt.setText(cmdReturn.toString());
Log.w("test",cmdReturn);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
Log.e("Process Manager", "Unable to execute top command");
}
This is method to fetch and dump logs in SDCard for Android. This is working fine, but I am wondering if we can put filter to trace logs related to specific tags.
I tried logcat -s *TagIWant; but it didn't work. Any other suggestion?
public static void Log(Context context){
String filename = context.getExternalFilesDir(null).getPath() + File.separator + "my_app.log";
String command = "logcat -d *:V";
Log.d(TAG, "command: " + command);
try{
Process process = Runtime.getRuntime().exec(command);
BufferedReader in = new BufferedReader(new InputStreamReader(process.getInputStream()));
String line = null;
try{
File file = new File(filename);
file.createNewFile();
FileWriter writer = new FileWriter(file);
while((line = in.readLine()) != null){
writer.write(line + "\n");
}
writer.flush();
writer.close();
}
catch(IOException e){
e.printStackTrace();
}
}
catch(IOException e){
e.printStackTrace();
}
}
try {
Process process = Runtime.getRuntime().exec("logcat -d");
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(process.getInputStream()));
String line;
Pattern pattern = Pattern.compile("XXX", 0);
while ((line = bufferedReader.readLine()) != null) {
if (patternu != null
&& !pattern.matcher(line).find()) {
continue;
}
log.append(line);
log.append('\n');
}
} catch (IOException e) {
}
Replace "Your_Tag" with your tag, that will give you the logs with tag as specified in with priority "Debug" and Above.
String command = "logcat Your_Tag:D *:S";
Process process = Runtime.getRuntime().exec(command);
Check more on http://developer.android.com/tools/debugging/debugging-log.html
You can easily filter the line that you want to write like the code below:
while ((line = bufferedReader.readLine()) != null) {
if(line.contains("The String you want to exist in your log")){
writer.write(line + "\n");
}
else{continue;}
}
I am using the following code to call android equipment ping command:
public static String pingServer() {
PingResult result = new PingResult();
String jsonString = null;
try {
String command = "ping -c 3 192.168.8.185";
Process p = Runtime.getRuntime().exec(command);
int status = p.waitFor();
InputStream input = p.getInputStream();
BufferedReader in = new BufferedReader(new InputStreamReader(input));
StringBuffer buffer = new StringBuffer();
String line = "";
while ((line = in.readLine()) != null) {
buffer.append(line);
buffer.append("\n");
}
String bufferStr = buffer.toString();
System.out.println(bufferStr);
} catch (Exception e) {
System.out.println("---------------exception-----------ping");
System.out.println(e.getMessage());
// e.printStackTrace();
}
}
BufferStr always obtain the last line data.
3 packets transmitted, 3 received, 0% packet loss, time 2000ms
The details result can be get.
It may be simpler and easier to use InetAddress.isReachable:
if (InetAddress.getByName("192.168.8.185").isReachable(6000))
...