添加到 Google 播放时,Ping 应用程序无法正常工作
Ping app not working when added to Google Play
我在 Google Play 上有一个应用程序可以在我的 phone 上运行,当我将它用作远程测试设备时,但是当我将它上传到 Play 商店然后下载到我的 phone 无法传输任何数据包。
请参阅下面的代码,我不知道是什么问题,我整天都在摸不着头脑,也许是权限问题?
package com.example.dale.whatismyip;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import java.io.BufferedReader;
import java.io.InputStreamReader;
/**
* Created by Dale on 22/01/2017.
*/
public class PingActivity extends AppCompatActivity
{
private EditText pingEdit;
private String pingVal;
private TextView finalResult;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.ping);
finalResult = (TextView) findViewById(R.id.result);
pingEdit = (EditText) findViewById(R.id.editText2);
final Button button = (Button) findViewById(R.id.button5);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
finalResult.setText("");
pingVal = pingEdit.getText().toString();
if(pingVal.contains(".") && pingVal.length() > 6)
{
PingTest runner = new PingTest();
runner.execute();
}
else
{
finalResult.setText("Invalid Address");
}
}
});
}
private class PingTest extends AsyncTask<String, String, String>
{
private String res;
@Override
protected String doInBackground(String... strings) {
try {
boolean sudo = false;
String cmd = "/system/bin/ping -c 4 -w 4 " + pingVal;
Process p;
if(!sudo)
p= Runtime.getRuntime().exec(cmd);
else{
p= Runtime.getRuntime().exec(new String[]{"su", "-c", cmd});
}
BufferedReader stdInput = new BufferedReader(new InputStreamReader(p.getInputStream()));
String s;
res = "";
while ((s = stdInput.readLine()) != null) {
// CODE TO DO - create an array and populate it
System.out.println(res += s + "\n");
}
p.destroy();
return res;
} catch (Exception e) {
e.printStackTrace();
}
return "";
}
@Override
protected void onPostExecute(String result) {
// execution of result of Long time consuming operation
// CODE TO DO - pass this method both an array of type string and a string
// then do a while loop through it whilst the array is populated and set the value of the textview to the strings
finalResult.setText(result);
}
}
}
好的,这是这样的建议,但您是通过计算机测试您的 phone 吗? phone 是否通过 USB 连接到计算机?
你能打印一份 toast 看看 doInBackground
中发生了什么吗
我在你的代码中找不到任何错误。
问题已排序。
代码本身没问题,但 android 上的省电功能停止了 ping 功能,因为它禁用了后台网络使用。
我在 Google Play 上有一个应用程序可以在我的 phone 上运行,当我将它用作远程测试设备时,但是当我将它上传到 Play 商店然后下载到我的 phone 无法传输任何数据包。
请参阅下面的代码,我不知道是什么问题,我整天都在摸不着头脑,也许是权限问题?
package com.example.dale.whatismyip;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import java.io.BufferedReader;
import java.io.InputStreamReader;
/**
* Created by Dale on 22/01/2017.
*/
public class PingActivity extends AppCompatActivity
{
private EditText pingEdit;
private String pingVal;
private TextView finalResult;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.ping);
finalResult = (TextView) findViewById(R.id.result);
pingEdit = (EditText) findViewById(R.id.editText2);
final Button button = (Button) findViewById(R.id.button5);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
finalResult.setText("");
pingVal = pingEdit.getText().toString();
if(pingVal.contains(".") && pingVal.length() > 6)
{
PingTest runner = new PingTest();
runner.execute();
}
else
{
finalResult.setText("Invalid Address");
}
}
});
}
private class PingTest extends AsyncTask<String, String, String>
{
private String res;
@Override
protected String doInBackground(String... strings) {
try {
boolean sudo = false;
String cmd = "/system/bin/ping -c 4 -w 4 " + pingVal;
Process p;
if(!sudo)
p= Runtime.getRuntime().exec(cmd);
else{
p= Runtime.getRuntime().exec(new String[]{"su", "-c", cmd});
}
BufferedReader stdInput = new BufferedReader(new InputStreamReader(p.getInputStream()));
String s;
res = "";
while ((s = stdInput.readLine()) != null) {
// CODE TO DO - create an array and populate it
System.out.println(res += s + "\n");
}
p.destroy();
return res;
} catch (Exception e) {
e.printStackTrace();
}
return "";
}
@Override
protected void onPostExecute(String result) {
// execution of result of Long time consuming operation
// CODE TO DO - pass this method both an array of type string and a string
// then do a while loop through it whilst the array is populated and set the value of the textview to the strings
finalResult.setText(result);
}
}
}
好的,这是这样的建议,但您是通过计算机测试您的 phone 吗? phone 是否通过 USB 连接到计算机?
你能打印一份 toast 看看 doInBackground
我在你的代码中找不到任何错误。
问题已排序。
代码本身没问题,但 android 上的省电功能停止了 ping 功能,因为它禁用了后台网络使用。