先上传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); } }