【问题标题】:How to execute a functionality without button in android?如何在android中执行没有按钮的功能?
【发布时间】:2013-04-03 11:56:42
【问题描述】:

现在这段代码不起作用

public class MainActivity extends Activity {


String X = "Music"; 
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);


 new AsyncTask<Void, Void, Void>() {
        @Override
        protected Void doInBackground(Void... voids) {
            Socket socket = null;
             DataOutputStream dataOutputStream = null;
             DataInputStream dataInputStream = null;

             try {
              socket = new Socket("172.16.82.131", 8888);
              dataOutputStream = new DataOutputStream(socket.getOutputStream());
              dataInputStream = new DataInputStream(socket.getInputStream());
              dataOutputStream.writeUTF(X);
              //textIn.setText(dataInputStream.readUTF());
             } catch (UnknownHostException e) {
              // TODO Auto-generated catch block
              e.printStackTrace();
             } catch (IOException e) {
              // TODO Auto-generated catch block
              e.printStackTrace();
             }
             finally{
              if (socket != null){
               try {
                socket.close();
               } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
               }
              }

              if (dataOutputStream != null){
               try {
                dataOutputStream.close();
               } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
               }
              }

              if (dataInputStream != null){
               try {
                dataInputStream.close();
               } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
               }
              }
             }


            return null;
        }
    }.execute();




   }


    }

在我之前的帖子中,我发布了一个代码,其中在单击按钮时正在执行一项功能,但现在我已将我的代码更改为这个。 我在启动应用程序时尝试了这种方法在后台运行我的代码..但没有任何反应。我无法找到我哪里出错了..请帮助:(

【问题讨论】:

  • 在oncreate中写你的方法
  • 在onCreate()方法中添加点击事件代码。它将在启动时执行。
  • 我不明白你尝试了什么,什么不起作用。
  • 请不要在中途修改您的问题。它会引起混乱。请提供任何错误的 LogCat 输出。摆脱 e.printStackTrace() 并使用 Log.e("someString", "unknownhost", e); 将错误实际记录到 LogCat;等

标签: java android networking android-activity client


【解决方案1】:

试试这个

public class AndroidClient extends Activity {  

        String x = "Music";
             /** Called when the activity is first created. */
        @Override
        public void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         setContentView(R.layout.activity_main);

         textIn = (TextView)findViewById(R.id.textin);
      doThis();

    }                
       public void doThis() {
     // TODO Auto-generated method stub
     Socket socket = null;
     DataOutputStream dataOutputStream = null;
     DataInputStream dataInputStream = null;

     try {
     socket = new Socket("112.13.835.187", 8183);
     dataOutputStream = new DataOutputStream(socket.getOutputStream());
     dataInputStream = new DataInputStream(socket.getInputStream());
     dataOutputStream.writeUTF(x);
     textIn.setText(dataInputStream.readUTF());
     } catch (UnknownHostException e) {
     // TODO Auto-generated catch block
     e.printStackTrace();
     } catch (IOException e) {
     // TODO Auto-generated catch block
     e.printStackTrace();
     }
     finally{
     if (socket != null){
     try {
     socket.close();
     } catch (IOException e) {
     // TODO Auto-generated catch block
     e.printStackTrace();
      }
     }

     if (dataOutputStream != null){
     try {
     dataOutputStream.close();
     } catch (IOException e) {
      // TODO Auto-generated catch block
     e.printStackTrace();
     }
     }

     if (dataInputStream != null){
      try {
       dataInputStream.close();
      } catch (IOException e) {
       // TODO Auto-generated catch block
       e.printStackTrace();
       }
       }
       } 
       }};
    }

【讨论】:

  • 当我这样做时什么都不会发生 :(
  • 此代码仅适用于较旧的 Android 设备。您不能在 UI 线程上执行网络任务。打勾似乎有点太快了。
  • 它可能“不起作用”,因为您正在捕获异常并且它不会在 UI 线程上执行。请参阅下面的答案以了解 AsyncTask() 的使用。
【解决方案2】:

在 onCreate 中编写您的代码,或者您也可以使用Asynctask 进行后台进程

【讨论】:

    【解决方案3】:

    使用 AsyncTask 处理 DataOutputStream 和 Socket

    参见下面的示例代码

    从 onCreate() 方法调用下面一行

     new SignInService().execute();
    

    然后像下面这样创建类,并在 onPostExecute 方法 textIn.setText(dataInputStream.readUTF());

    中写入这一行
    private class SignInService extends AsyncTask<Void, Void, Void> {
          Socket socket = null;
          DataOutputStream dataOutputStream = null;
          DataInputStream dataInputStream = null;
    
         @Override
        protected void onPreExecute() {
            super.onPreExecute();
            try {
                progressDilaog = ProgressDialog.show(MainActivity.this, "",
                        "Loading", true, false);
    
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    
        @Override
        protected Void doInBackground(Void... params) {
    
         try {
            socket = new Socket("112.13.835.187", 8183);
            dataOutputStream = new DataOutputStream(socket.getOutputStream());
              dataInputStream = new DataInputStream(socket.getInputStream());
              dataOutputStream.writeUTF(x);
    
          } catch (UnknownHostException e) {
    
            e.printStackTrace();
          } catch (IOException e) {
               // TODO Auto-generated catch block
             e.printStackTrace();
          }
          finally{
             if (socket != null){
             try {
                socket.close();
              } catch (IOException e) {
                // TODO Auto-generated catch block
                 e.printStackTrace();
               }
              }
    
            if (dataOutputStream != null){
              try {
                dataOutputStream.close();
              } catch (IOException e) {
               // TODO Auto-generated catch block
                     e.printStackTrace();
              }
            }
    
            if (dataInputStream != null){
               try {
                   dataInputStream.close();
                } catch (IOException e) {
                  e.printStackTrace();
                 }
            }
          }
            return null;
        }
    
        @Override
        protected void onPostExecute(Void result) {
            super.onPostExecute(result);
            progressDilaog.dismiss();
    
            textIn.setText(dataInputStream.readUTF());
        }
    }
    

    【讨论】:

    • 我不知道如何在我的代码中使用它。请您详细说明一下。谢谢
    • 不幸的是,应用程序在显示加载对话框后停止工作... :(
    【解决方案4】:

    你必须在应用的onCreate()函数中编写onClick()函数的代码。并且不需要实现按钮的onClickListener功能。

    【讨论】:

      【解决方案5】:

      试试这个代码... 这里不需要实现按钮的onclick监听器

      public class AndroidClient extends Activity {
      
      String x = "Music";
      
      /** Called when the activity is first created. */
      @Override
      public void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.activity_main);
      
       Button buttonSend = (Button)findViewById(R.id.send);
       textIn = (TextView)findViewById(R.id.textin);
       startonCreate(); 
       }
       public void startonCreate(){
       Socket socket = null;
       DataOutputStream dataOutputStream = null;
       DataInputStream dataInputStream = null;
      
       try {
        socket = new Socket("112.13.835.187", 8183);
        dataOutputStream = new DataOutputStream(socket.getOutputStream());
        dataInputStream = new DataInputStream(socket.getInputStream());
       dataOutputStream.writeUTF(x);
       textIn.setText(dataInputStream.readUTF());
       } catch (UnknownHostException e) {
       // TODO Auto-generated catch block
       e.printStackTrace();
       } catch (IOException e) {
       // TODO Auto-generated catch block
        e.printStackTrace();
        }
        finally{
        if (socket != null){
       try {
       socket.close();
       } catch (IOException e) {
       // TODO Auto-generated catch block
       e.printStackTrace();
       }
      

      }

      if (dataOutputStream != null){
       try {
       dataOutputStream.close();
       } catch (IOException e) {
        // TODO Auto-generated catch block
       e.printStackTrace();
       }
       }
      
       if (dataInputStream != null){
       try {
          dataInputStream.close();
         } catch (IOException e) {
         // TODO Auto-generated catch block
         e.printStackTrace();
         }
       }
       } 
      
       }
      }
      

      【讨论】:

        【解决方案6】:

        将 onClick() 重命名为其他名称,移除按钮和点击监听器。然后它将失败,因为您正在 UI 线程上进行网络调用。您将需要使用 AsyncTask() (或类似的)。

        如果你只是想让它运行而不关心答案......

        public void onCreate(Bundle savedInstanceState) {
            // ...
            new AsyncTask<Void, Void, Void>() {
                @Override
                protected Void doInBackground(Void... voids) {
                    try {
                        // your code here
                    } catch (Exception e) {
                        // catch any errors
                    }
                    return null;
                }
            }.execute();
            // ...
        }
        

        【讨论】:

        • 你能解释一下如何在我的代码中实现 AsyncTask..谢谢
        猜你喜欢
        • 2020-03-31
        • 2020-07-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-12-18
        相关资源
        最近更新 更多