添加到Google Play后,Ping应用无效

时间:2017-05-17 19:02:08

标签: java android ping

我在Google Play上有一个可以在我的手机上使用它作为远程测试设备的应用程序,但是当我将其上传到Play商店然后将其下载到我的手机上时它无法正常传输任何数据包。< / p>

请参阅下面的代码,我不知道问题是什么,我整天都在摸不着头脑,也许是权限问题?

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);

        }
    }

}

2 个答案:

答案 0 :(得分:1)

问题排序。

代码本身很好,但android上的省电功能会停止ping功能,因为它会禁用后台网络使用。

答案 1 :(得分:0)

好的,这是一个建议,但你是通过电脑测试手机吗?是通过USB连接到电脑的手机吗?

你可以打印一个祝酒词,看看doInBackground

中发生了什么

我在代码中找不到任何错误。