zoukankan      html  css  js  c++  java
  • Android之与当前连接的wifi进行文件夹的浏览与传输

    先上传jar文件;ConnectWifi.jar.zip

    上传源文件:org.zip

    使用实例及相应的注释;

    import java.io.File;
    import java.net.InetAddress;
    import org.swiftp.Defaults;
    import org.swiftp.FTPServerService;
    import org.swiftp.Globals;
    import org.swiftp.UiUpdater;
    import android.app.Activity;
    import android.content.Context;
    import android.content.Intent;
    import android.os.Bundle;
    import android.os.Environment;
    import android.os.Handler;
    import android.os.Message;
    import android.view.View;
    
    public class MainActivity extends Activity {
    
        private Context context;
        
        // 处理
        public Handler handler = new Handler() {
            public void handleMessage(Message msg) {
                switch (msg.what) {
                    case 0: // We are being told to do a UI update
                        // If more than one UI update is queued up, we only need to
                        // do one.
                        removeMessages(0);
                        updateUi();
                        break;
                    case 1: // We are being told to display an error message
                        removeMessages(1);
                }
            }
        };
        
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            
            context = this;
            
            // 实例化Globals
            Globals.setContext(context);
        }
    
        public void start(View view){
            
            Globals.setLastError(null);
            
            // 注册更新界面的handler
            UiUpdater.registerClient(handler);
            
            // 需要打开的对应目录
            File chrootDir = new File(Defaults.chrootDir);
            if (!chrootDir.isDirectory())
                return;
    
            // 打开FTP服务
            Intent intent = new Intent(context, FTPServerService.class);
    
            Globals.setChrootDir(chrootDir);
            if (!FTPServerService.isRunning()) {
                if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) {
                    context.startService(intent);
                    
                }
            } else {
                context.stopService(intent);
            }
        }
        
        // 绑定后的界面回调更新
        public void updateUi() {
             InetAddress address = FTPServerService.getWifiIp();
             if (address != null) {
                 String port = ":" + FTPServerService.getPort();
                 System.out.println("ftp://" + address.getHostAddress() + (FTPServerService.getPort() == 21 ? "" : port));
    
             } 
        }
        
        
        @Override
        protected void onStop() {
            super.onStop();
            // 反注册更新界面
            UiUpdater.unregisterClient(handler);
        }
    }
  • 相关阅读:
    PHP学习笔记十二【数组排序】
    PHP学习笔记十一【数组】
    PHP学习笔记十【数组】
    PHP学习笔记九【数组二】
    PHP学习笔记八【数组】
    Codeforces 612E
    Codeforces 616E
    codeforce #339(div2)C Peter and Snow Blower
    poj 1113 Mall
    poj 2187 Beauty Contest
  • 原文地址:https://www.cnblogs.com/lee0oo0/p/3390933.html
Copyright © 2011-2022 走看看