在后台运行Android任务或方法?

时间:2014-12-31 00:39:44

标签: android background ping

我要做的是,当Activity启动时,应用程序会ping服务器以查看它们是否在线。当前的问题是,应用程序将冻结并变得无法使用,直到它已经ping通所有服务器。所以我现在拥有的是:

Thread t = new Thread(new Runnable() {
            public void run() {
       //ping stuff
}};
t.start();

这消除了冻结问题,但它似乎并没有实际上是在ping任何东西。在pining之后它将文本视图从“服务器状态”更改为“服务器状态联机”或“服务器状态脱机”并且没有发生。这是我正在使用的ping方法:

Runtime runtime = Runtime.getRuntime();
                try{
                    Process  mIpAddrProcess = runtime.exec("/system/bin/ping -c 1 sfb.noip.me");
                    int mExitValue = mIpAddrProcess.waitFor();
                    Process  mIpAddrProcess2 = runtime.exec("/system/bin/ping -c 1 sg.lbsg.net");
                    int mExitValue2 = mIpAddrProcess2.waitFor();
                    Process  mIpAddrProcess3 = runtime.exec("/system/bin/ping -c 1 peepzcraft.zapto.org");
                    int mExitValue3 = mIpAddrProcess3.waitFor();
                    Process  mIpAddrProcess4 = runtime.exec("/system/bin/ping -c 1 71.226.128.188");
                    int mExitValue4 = mIpAddrProcess4.waitFor();
                    Process  mIpAddrProcess5 = runtime.exec("/system/bin/ping -c 1 playmcpe.com");
                    int mExitValue5 = mIpAddrProcess5.waitFor();
                    Process  mIpAddrProcess6 = runtime.exec("/system/bin/ping -c 1 survival.dgpocket.us");
                    int mExitValue6 = mIpAddrProcess6.waitFor();
                    Process  mIpAddrProcess7 = runtime.exec("/system/bin/ping -c 1 minecraft.blocksandgold.com");
                    int mExitValue7 = mIpAddrProcess7.waitFor();
                    Process  mIpAddrProcess8 = runtime.exec("/system/bin/ping -c 1 pe.cookie-build.com");
                    int mExitValue8 = mIpAddrProcess8.waitFor();
                    Process  mIpAddrProcess9 = runtime.exec("/system/bin/ping -c 1 96.8.119.195");
                    int mExitValue9 = mIpAddrProcess9.waitFor();
                    Process  mIpAddrProcess10 = runtime.exec("/system/bin/ping -c 1 Play.mcpe-ba.info");
                    int mExitValue10 = mIpAddrProcess10.waitFor();
                    Process  mIpAddrProcess12 = runtime.exec("/system/bin/ping -c 1 leet.cc");
                    int mExitValue12 = mIpAddrProcess12.waitFor();
                    Process  mIpAddrProcess13 = runtime.exec("/system/bin/ping -c 1 ru.24serv.pro");
                    int mExitValue13 = mIpAddrProcess13.waitFor();

                    if(mExitValue==0){

                        text.setText("Server Status: Online");              
                    }else{
                        text.setText("Server Status: Offline");              
                    }
                    if(mExitValue2==0){
                        text2.setText("Server Status: Online");
                    }else{
                        text2.setText("Server Status: Offline");
                    }
                    if(mExitValue3==0){
                        text3.setText("Server Status: Online");
                    }else{
                        text3.setText("Server Status: Offline");
                    }
                    if(mExitValue4==0){
                        text4.setText("Server Status: Online");
                    }else{
                        text4.setText("Server Status: Offline");
                    }
                    if(mExitValue5==0){
                        text5.setText("Server Status: Online");
                    }else{
                        text5.setText("Server Statis: Offline");
                    }
                    if(mExitValue6==0){
                        text6.setText("Server Status: Online");
                    }else{
                        text6.setText("Server Status: Offline");
                    }
                    if(mExitValue7==0){
                        text7.setText("Server Status: Online");
                    }else{
                        text7.setText("Server Status: Offline");
                    }
                    if(mExitValue8==0){
                        text8.setText("Server Status: Online");
                    }else{
                        text8.setText("Server Status: Offline");
                    }
                    if(mExitValue9==0){
                        text9.setText("Server Status: Online");
                    }else{
                        text9.setText("Server Status: Offline");
                    }
                    if(mExitValue10==0){
                        text10.setText("Server Status: Online");
                    }else{
                        text10.setText("Server Status: Offline");
                    }
                    if(mExitValue12==0){
                        text12.setText("Server Status: Online");
                    }else{
                        text12.setText("Server Status: Offline");
                    }
                    if(mExitValue13==0){
                        text13.setText("Server Status: Online");
                    }else{
                        text13.setText("Server Status: Offline");
                    }
                }catch(Exception e){

                }
                return;

1 个答案:

答案 0 :(得分:1)

您可以使用Android的AsyncTask来完成任务。

doInBackground()。

中ping服务器

将结果返回onPostExecute()&设置TextViews。

您无法将背景中的视图更新为当前代码。