3

I'm trying to execute ping command in my Android application and be able to cancel it / send break signal. Basically I want to get ping statistics after like when you send ctrl+c to ping in any normal linux. I have read Send Ctrl-C to process open by Java but it aims for Windows platform and tbh seems a little bit like an overkill. My code for executing the command (I'm using rxjava):

public rx.Observable<String> getPingOutput(String address)
{
    return rx.Observable.create(new rx.Observable.OnSubscribe<String>()
    {
        @Override
        public void call(Subscriber<? super String> subscriber)
        {
            try
            {
                process = Runtime.getRuntime().exec("ping -c 4 " + address);
                BufferedReader stdInput = new BufferedReader(new InputStreamReader(process.getInputStream()));
                String s;
                while ((s = stdInput.readLine()) != null)
                {
                    subscriber.onNext(s);
                }
                subscriber.onCompleted();
                process.destroy();
                stdInput.close();
            }
            catch (IOException e)
            {
                subscriber.onError(e);
                if(process != null)
                {
                    process.destroy();
                }
            }
        }
    });
}

And starting / canceling:

RxView.clicks(pingButton)
                .subscribe(new Action1<Void>()
                {
                    @Override
                    public void call(Void aVoid)
                    {
                        if (isPingInProgress())
                        {
                            process.destroy();
                            subscription.unsubscribe();
                            isPing = false;
                            pingButton.setText("Ping");
                        }
                        else
                        {
                            if(adapter!= null)
                            {
                                adapter.clear();
                                adapter.notifyDataSetChanged();
                            }
                            String address = addressInput.getText().toString();
                            subscription = getPingOutput(address)
                                    .subscribeOn(Schedulers.newThread())
                                    .observeOn(AndroidSchedulers.mainThread())
                                    .subscribe(myObserver);
                            isPing = true;
                            pingButton.setText("Stop");
                        }
                    }
                });

So far I tried just killing the process but that immediately stops all output. And ideas how can I "gracefully" stop the ping command started by Java in Android?

EDIT

So I managed to get PID of my ping process, verified its correct via adb console. I tried to kill it using: Runtime.getRuntime().exec("kill -INT " + getPid(process));, there is no ErrorStream output but nothing happens. Same for Process.sendSignal(getPid(process),Process.SIGNAL_QUIT);. Anyone?

Community
  • 1
  • 1
Loki
  • 77
  • 1
  • 5

1 Answers1

0

Problem solved, turns out it didn't react to signals because I ran ping on separate thread.

Loki
  • 77
  • 1
  • 5