第一步:
Win10系统搭建FTP服务器 https://jingyan.baidu.com/article/0bc808fc408fa91bd585b94f.html
第二步:
下载服务器管理工具 FileZilla http://dl.pconline.com.cn/html_2/1/89/id=5826&pn=0.html
搭建好服务器之后其实就可以直接去访问服务器了,服务器管理工具可以对服务器中的文件进行添加或是删除。
浏览器访问服务器可以这样:
ftp://配置的IP地址:端口
上代码
权限:
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.INTERNET" />
依赖:
compile 'org.greenrobot:eventbus:3.1.1'
compile files('libs/commons-net-3.0.1.jar')
项目地址:https://download.csdn.net/download/qq_40116418/10651845
---------------------MainActivity---------------------
package com.example.pc.duandianxuchuan;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.ConnectivityManager;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import com.example.pc.duandianxuchuan.ftp.ContinueFTP;
import org.greenrobot.eventbus.EventBus;
import java.io.IOException;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private Button upFile_btn;
private Button downFile_btn;
private Button deleteFilebtn;
private static final String TAG = "MainActivity";
public static final String FTP_CONNECT_SUCCESSS = "ftp连接成功";
public static final String FTP_CONNECT_FAIL = "ftp连接失败";
public static final String FTP_DISCONNECT_SUCCESS = "ftp断开连接";
public static final String FTP_FILE_NOTEXISTS = "ftp上文件不存在";
public static final String FTP_DELETEFILE_SUCCESS = "ftp文件删除成功";
public static final String FTP_DELETEFILE_FAIL = "ftp文件删除失败";
public long MAX_PROGRESS = 100;
private boolean connect;
private NetReceiver mReceiver;
private boolean isConnected;
private String remoteAddress = "192.168.0.122";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initReceive();
upFile_btn = (Button) findViewById(R.id.btn);
upFile_btn.setOnClickListener(this);
downFile_btn = (Button) findViewById(R.id.btn2);
downFile_btn.setOnClickListener(this);
deleteFilebtn = (Button) findViewById(R.id.btn3);
deleteFilebtn.setOnClickListener(this);
}
private boolean isNet = true;
@Override
public void onClick(View v) {
switch (v.getId()) {
//上传文件
case R.id.btn:
new Thread(new Runnable() {
private ContinueFTP continueFTP;
@Override
public void run() {
if (isNet){
try {
if (!NetUtils.isNetworkConnected(MainActivity.this))
{
return;
}
ContinueFTP continueFTP = new ContinueFTP();
connect = continueFTP.connect(remoteAddress,
Integer.parseInt("5068"), "pc", "pc123456");
Log.d(TAG, "run: " + connect);
if (connect) {
ContinueFTP.UploadStatus upload = continueFTP.upload(
"/mnt/sdcard/fff/good.mp4", "/fff/good.mp4");
Log.d(TAG, "run: " + "upload:" + upload);
}
continueFTP.disconnect();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}).start();
ContinueFTP.setOnDisplayRefreshListener(new ContinueFTP.refreshOnDisplayListener() {
@Override
public void returnRefresh(final long progress) {
Log.d(TAG, "returnRefresh: 进度:" + progress);
Log.d(TAG, "run: " + connect);
MainActivity.this.runOnUiThread(new Runnable() {
@Override
public void run() {
if (progress == MAX_PROGRESS) {
Toast.makeText(MainActivity.this, "上传成功", Toast.LENGTH_SHORT).show();
}
}
});
}
});
break;
//下载文件
case R.id.btn2:
new Thread(new Runnable() {
private ContinueFTP continueFTP;
@Override
public void run() {
try {
continueFTP = new ContinueFTP();
boolean connect = continueFTP.connect(remoteAddress,
Integer.parseInt("5068"), "pc", "pc123456");
if (connect) {
ContinueFTP.DownloadStatus download = continueFTP.download(
"/fff/good.mp4", "/sdcard/downloads/good.mp4", "p");
Log.d(TAG, "run: " + "download:" + download);
}
continueFTP.disconnect();
} catch (IOException e) {
e.printStackTrace();
}
}
}).start();
break;
case R.id.btn3:
new Thread(new Runnable() {
@Override
public void run() {
// 删除
try {
new ContinueFTP().deleteSingleFile("/fff/good.mp4", remoteAddress, 5068, "pc", "pc123456", new ContinueFTP.DeleteFileProgressListener() {
@Override
public void onDeleteProgress(String currentStep) {
Log.d(TAG, currentStep);
if (currentStep.equals(MainActivity.FTP_DELETEFILE_SUCCESS)) {
Log.d(TAG, "文件已删除");
} else if (currentStep.equals(MainActivity.FTP_DELETEFILE_FAIL)) {
Log.d(TAG, "文件删除失败");
}
}
});
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}).start();
break;
}
}
private void initReceive() {
mReceiver = new NetReceiver();
IntentFilter mFilter = new IntentFilter();
mFilter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
registerReceiver(mReceiver, mFilter);
}
@Override
protected void onDestroy() {
unregisterReceiver(mReceiver);
EventBus.getDefault().unregister(this);
super.onDestroy();
}
public class NetReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (ConnectivityManager.CONNECTIVITY_ACTION.equals(action)) {
isConnected = NetUtils.isNetworkConnected(context);
System.out.println("网络状态:" + isConnected);
System.out.println("wifi状态:" + NetUtils.isWifiConnected(context));
System.out.println("移动网络状态:" + NetUtils.isMobileConnected(context));
System.out.println("网络连接类型:" + NetUtils.getConnectedType(context));
if (isConnected == true) {
isNet = true;
Toast.makeText(context, "已经连接网络", Toast.LENGTH_LONG).show();
EventBus.getDefault().post(new NetEvent(true));
} else {
EventBus.getDefault().post(new NetEvent(false));
isNet = false;
Toast.makeText(context, "已经断开网络", Toast.LENGTH_LONG).show();
}
}
}
}
}
---------------------ContinueFTP--------------------
package com.example.pc.duandianxuchuan.ftp;
import android.util.Log;
import com.example.pc.duandianxuchuan.MainActivity;
import com.example.pc.duandianxuchuan.NetUtils;
import org.apache.commons.net.PrintCommandListener;
import org.apache.commons.net.ftp.FTP;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPClientConfig;
import org.apache.commons.net.ftp.FTPFile;
import org.apache.commons.net.ftp.FTPReply;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.RandomAccessFile;
public class ContinueFTP {
private static final String TAG = "ContinueFTP";
private static refreshOnDisplayListener listener;
private OutputStream out;
private RandomAccessFile raf;
// 枚举类UploadStatus代码
public enum UploadStatus {
Create_Directory_Fail, // 远程服务器相应目录创建失败
Create_Directory_Success, // 远程服务器闯将目录成功
Upload_New_File_Success, // 上传新文件成功
Upload_New_File_Failed, // 上传新文件失败
File_Exits, // 文件已经存在
Remote_Bigger_Local, // 远程文件大于本地文件
Upload_From_Break_Success, // 断点续传成功
Upload_From_Break_Failed, // 断点续传失败
Delete_Remote_Faild; // 删除远程文件失败
}
// 枚举类DownloadStatus代码
public enum DownloadStatus {
Remote_File_Noexist, // 远程文件不存在
Local_Bigger_Remote, // 本地文件大于远程文件
Download_From_Break_Success, // 断点下载文件成功
Download_From_Break_Failed, // 断点下载文件失败
Download_New_Success, // 全新下载文件成功
Download_New_Failed; // 全新下载文件失败
}
// 枚举类DeleteStatus代码
public enum DeleteStatus {
Delete_Remote_Faild, // 删除远程文件失败
Delete_Remote_Success, // 删除远程文件成功
Remote_File_Noexist; // 远程文件不存在
}
private FTPClient ftpClient;
public ContinueFTP() {
ftpClient = new FTPClient();
// 设置将过程中使用到的命令输出到控制台
this.ftpClient.addProtocolCommandListener(new PrintCommandListener(
new PrintWriter(System.out)));
}
public boolean connect(String hostname, int port, String username,
String password) throws IOException {
// 连接到FTP服务器
ftpClient.connect(hostname, port);
// 如果采用默认端口,可以使用ftp.connect(url)的方式直接连接FTP服务器
if (FTPReply.isPositiveCompletion(ftpClient.getReplyCode())) {
if (ftpClient.login(username, password)) {
return true;
}
}
disconnect();
return false;
}
public DownloadStatus download(String remote, String local, String mode)
throws IOException {
// 设置ftp传输方式
if (mode.equalsIgnoreCase("P")) {
// PassiveMode传输
ftpClient.enterLocalPassiveMode();
} else {
// ActiveMode传输
ftpClient.enterLocalActiveMode();
}
// 设置以二进制流的方式传输
ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
// 下载状态
DownloadStatus result;
// 本地文件列表
File f = new File(local);
// 检查远程文件是否存在
FTPFile[] files = ftpClient.listFiles(new String(
remote.getBytes("GBK"), "iso-8859-1"));
if (files.length != 1) {
Log.d(TAG, "download: " + "远程文件不存在");
return DownloadStatus.Remote_File_Noexist;
}
// 获得远端文件大小
long lRemoteSize = files[0].getSize();
// 构建输出对象
OutputStream out = null;
// 本地存在文件,进行断点下载 ;不存在则新下载
if (f.exists()) {
// 构建输出对象
out = new FileOutputStream(f, true);
// 本地文件大小
long localSize = f.length();
Log.d(TAG, "download: " + "本地文件大小为:" + localSize);
// 判定本地文件大小是否大于远程文件大小
if (localSize >= lRemoteSize) {
Log.d(TAG, "download: " + "本地文件大于远程文件,下载中止");
return DownloadStatus.Local_Bigger_Remote;
}
// 否则进行断点续传,并记录状态
ftpClient.setRestartOffset(localSize); // 该方法尝试把指针移动到远端文件的指定字节位置
InputStream in = ftpClient.retrieveFileStream(new String(remote
.getBytes("GBK"), "iso-8859-1"));
byte[] bytes = new byte[1024];
long step = lRemoteSize / 100;
// 存放下载进度
long process = localSize / step;
int c;
while ((c = in.read(bytes)) != -1) {
out.write(bytes, 0, c);
localSize += c;
long nowProcess = localSize / step;
if (nowProcess > process) {
process = nowProcess;
if (process % 2 == 0)
Log.d(TAG, "download: " + "下载进度: " + process);
// TODO更新文件下载进度,值存放在process变量中
}
}
// 下载完成关闭输入输出流对象
in.close();
out.close();
boolean isDo = ftpClient.completePendingCommand();
if (isDo) {
result = DownloadStatus.Download_From_Break_Success;
} else {
result = DownloadStatus.Download_From_Break_Failed;
}
} else {
out = new FileOutputStream(f);
InputStream in = ftpClient.retrieveFileStream(new String(remote
.getBytes("GBK"), "iso-8859-1"));
byte[] bytes = new byte[1024];
long step = lRemoteSize / 100;
long process = 0;
long localSize = 0L;
int c;
while ((c = in.read(bytes)) != -1) {
out.write(bytes, 0, c);
localSize += c;
long nowProcess = localSize / step;
if (nowProcess > process) {
process = nowProcess;
if (process % 10 == 0)
Log.d(TAG, "download: " + "下载进度:" + process);
// TODO更新文件下载进度,值存放在process变量中
}
}
in.close();
out.close();
boolean upNewStatus = ftpClient.completePendingCommand();
if (upNewStatus) {
result = DownloadStatus.Download_New_Success;
} else {
result = DownloadStatus.Download_New_Failed;
}
}
return result;
}
/** */
/**
* 上传文件到FTP服务器,支持断点续传
*
* @param local 本地文件名称,绝对路径
* @param remote 远程文件路径,使用/home/directory1/subdirectory/file.ext或是 http://www.guihua.org /subdirectory/file.ext 按照Linux上的路径指定方式,支持多级目录嵌套,支持递归创建不存在的目录结构
* @return 上传结果
* @throws IOException
*/
public UploadStatus upload(String local, String remote) throws IOException {
//设置PassiveMode传输
ftpClient.enterLocalPassiveMode();
//设置以二进制流的方式传输
ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
ftpClient.setControlEncoding("GBK");
UploadStatus result = null;
//对远程目录的处理
String remoteFileName = remote;
if (remote.contains("/")) {
remoteFileName = remote.substring(remote.lastIndexOf("/") + 1);
//创建服务器远程目录结构,创建失败直接返回
if (CreateDirecroty(remote, ftpClient) == UploadStatus.Create_Directory_Fail) {
return UploadStatus.Create_Directory_Fail;
}
}
//检查远程是否存在文件
FTPFile[] files = ftpClient.listFiles(new String(remoteFileName.getBytes("GBK"), "iso-8859-1"));
if (files.length == 1) {
long remoteSize = files[0].getSize();
File f = new File(local);
long localSize = f.length();
Log.d(TAG, "upload:----------dierci ");
Log.d(TAG, "upload: localSize:" + localSize);
Log.d(TAG, "upload: remoteSize:" + remoteSize);
if (remoteSize == localSize) {
return UploadStatus.File_Exits;
} else if (remoteSize > localSize) {
return UploadStatus.Remote_Bigger_Local;
}
//尝试移动文件内读取指针,实现断点续传
result = uploadFile(remoteFileName, f, ftpClient, remoteSize);
//如果断点续传没有成功,则删除服务器上文件,重新上传
if (result == UploadStatus.Upload_From_Break_Failed) {
if (!ftpClient.deleteFile(remoteFileName)) {
return UploadStatus.Delete_Remote_Faild;
}
result = uploadFile(remoteFileName, f, ftpClient, 0);
}
} else {
Log.d(TAG, "upload: ------------diyici");
if (NetUtils.isNetworkConnected())
{
result = uploadFile(remoteFileName, new File(local), ftpClient, 0);
}
}
return result;
}
/**
* 上传文件到服务器,新上传和断点续传
*
* @param remoteFile 远程文件名,在上传之前已经将服务器工作目录做了改变
* @param localFile 本地文件 File句柄,绝对路径
* @param
* @param ftpClient FTPClient 引用
* @return
* @throws IOException
*/
public UploadStatus uploadFile(String remoteFile, File localFile, FTPClient ftpClient, long remoteSize) throws IOException {
UploadStatus status;
//显示进度的上传
long step = localFile.length() / 100;
long process = 0;
long localreadbytes = 0L;
raf = new RandomAccessFile(localFile, "r");
out = ftpClient.appendFileStream(new String(remoteFile.getBytes("GBK"), "iso-8859-1"));
Log.d(TAG, "uploadFile: -------out:"+ out +"---ftpClient:"+ftpClient+"----remoteFile:"+remoteFile);
//断点续传
if (remoteSize >= 0) {
ftpClient.setRestartOffset(remoteSize);
process = remoteSize / step;
raf.seek(remoteSize);
localreadbytes = remoteSize;
}
byte[] bytes = new byte[1024];
Log.d(TAG, "uploadFile: "+bytes);
int c;
while ((c = raf.read(bytes)) != -1) {
if (!NetUtils.isNetworkConnected()){
break;
}
if (out!=null){
out.write(bytes, 0, c);
}
localreadbytes += c;
if (localreadbytes / step != process) {
process = localreadbytes / step;
Log.d(TAG, "uploadFile=====================: "+remoteSize);
Log.d(TAG, "uploadFile: " + "上传进度:" + process);
listener.returnRefresh(process);
//TODO 汇报上传状态
}
}
raf.close();
if (out!=null){
out.flush();
out.close();
}
boolean result = ftpClient.completePendingCommand();
if (remoteSize > 0) {
status = result ? UploadStatus.Upload_From_Break_Success : UploadStatus.Upload_From_Break_Failed;
} else {
status = result ? UploadStatus.Upload_New_File_Success : UploadStatus.Upload_New_File_Failed;
}
return status;
}
/**
* 递归创建远程服务器目录
*
* @param remote 远程服务器文件绝对路径
* @param ftpClient FTPClient 对象
* @return 目录创建是否成功
* @throws IOException
*/
public UploadStatus CreateDirecroty(String remote, FTPClient ftpClient) throws IOException {
UploadStatus status = UploadStatus.Create_Directory_Success;
String directory = remote.substring(0, remote.lastIndexOf("/") + 1);
if (!directory.equalsIgnoreCase("/") && !ftpClient.changeWorkingDirectory(new String(directory.getBytes("GBK"), "iso-8859-1"))) {
//如果远程目录不存在,则递归创建远程服务器目录
int start = 0;
int end = 0;
if (directory.startsWith("/")) {
start = 1;
} else {
start = 0;
}
end = directory.indexOf("/", start);
while (true) {
String subDirectory = new String(remote.substring(start, end).getBytes("GBK"), "iso-8859-1");
if (!ftpClient.changeWorkingDirectory(subDirectory)) {
if (ftpClient.makeDirectory(subDirectory)) {
ftpClient.changeWorkingDirectory(subDirectory);
} else {
Log.d(TAG, "CreateDirecroty: " + "创建目录失败");
return UploadStatus.Create_Directory_Fail;
}
}
start = end + 1;
end = directory.indexOf("/", start);
//检查所有目录是否创建完毕
if (end <= start) {
break;
}
}
}
return status;
}
/**
* 删除Ftp下的文件.
*
* @param serverPath Ftp目录及文件路径
* @param listener 监听器
* @throws IOException
*/
public void deleteSingleFile(String serverPath, String hostName, int serverPort, String username, String password, DeleteFileProgressListener listener)
throws Exception {
// 打开FTP服务
try {
this.openConnect(hostName, serverPort, username, password);
listener.onDeleteProgress(MainActivity.FTP_CONNECT_SUCCESSS);
} catch (IOException e1) {
e1.printStackTrace();
listener.onDeleteProgress(MainActivity.FTP_CONNECT_FAIL);
return;
}
// 先判断服务器文件是否存在
FTPFile[] files = ftpClient.listFiles(serverPath);
if (files.length == 0) {
listener.onDeleteProgress(MainActivity.FTP_FILE_NOTEXISTS);
return;
}
//进行删除操作
boolean flag = true;
flag = ftpClient.deleteFile(serverPath);
if (flag) {
listener.onDeleteProgress(MainActivity.FTP_DELETEFILE_SUCCESS);
} else {
listener.onDeleteProgress(MainActivity.FTP_DELETEFILE_FAIL);
}
// 删除完成之后关闭连接
this.closeConnect();
listener.onDeleteProgress(MainActivity.FTP_DISCONNECT_SUCCESS);
return;
}
/**
* 打开FTP服务.
*
* @throws IOException
*/
public void openConnect(String hostName, int serverPort, String userName, String password) throws IOException {
// 中文转码
ftpClient.setControlEncoding("UTF-8");
int reply; // 服务器响应值
// 连接至服务器
ftpClient.connect(hostName, serverPort);
// 获取响应值
reply = ftpClient.getReplyCode();
if (!FTPReply.isPositiveCompletion(reply)) {
// 断开连接
ftpClient.disconnect();
throw new IOException("connect fail: " + reply);
}
// 登录到服务器
ftpClient.login(userName, password);
// 获取响应值
reply = ftpClient.getReplyCode();
if (!FTPReply.isPositiveCompletion(reply)) {
// 断开连接
ftpClient.disconnect();
throw new IOException("connect fail: " + reply);
} else {
// 获取登录信息
FTPClientConfig config = new FTPClientConfig(ftpClient
.getSystemType().split(" ")[0]);
config.setServerLanguageCode("zh");
ftpClient.configure(config);
// 使用被动模式设为默认
// ftpClient.enterLocalPassiveMode();
// 二进制文件支持
ftpClient
.setFileType(org.apache.commons.net.ftp.FTP.BINARY_FILE_TYPE);
}
}
/**
* 关闭FTP服务.
*
* @throws IOException
*/
public void closeConnect() throws IOException {
if (ftpClient != null) {
Log.d(TAG, "closeConnect: --------------------");
// 退出FTP
ftpClient.logout();
// 断开连接
ftpClient.disconnect();
out.flush();
raf.close();
out.close();
}
}
public void disconnect() throws IOException {
if (ftpClient.isConnected()) {
ftpClient.disconnect();
}
}
/*
* 文件删除监听
*/
public interface DeleteFileProgressListener {
public void onDeleteProgress(String currentStep);
}
public interface refreshOnDisplayListener {
public void returnRefresh(long progress);
}
public static void setOnDisplayRefreshListener(refreshOnDisplayListener myListener) {
listener = myListener;
}
}
------------------------App-----------------------
package com.example.pc.duandianxuchuan;
import android.app.Application;
import android.content.Context;
public class App extends Application {
public static Context context;
@Override
public void onCreate() {
super.onCreate();
context = getApplicationContext();
}
}
-----------------------------NetEvent------------------------
package com.example.pc.duandianxuchuan;
public class NetEvent {
public boolean isNet;
public NetEvent(boolean isNet) {
this.isNet = isNet;
}
public boolean isNet() {
return isNet;
}
}
----------------------NetUtils------------------------
package com.example.pc.duandianxuchuan;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Build;
public class NetUtils {
// 判断网络连接状态
public static boolean isNetworkConnected(Context context) {
if (context != null) {
ConnectivityManager mConnectivityManager = (ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo mNetworkInfo = mConnectivityManager.getActiveNetworkInfo();
if (mNetworkInfo != null) {
return mNetworkInfo.isAvailable();
}
}
return false;
}
// 判断网络连接状态
public static boolean isNetworkConnected() {
ConnectivityManager mConnectivityManager = (ConnectivityManager) App.context
.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo mNetworkInfo = mConnectivityManager.getActiveNetworkInfo();
if (mNetworkInfo != null) {
return mNetworkInfo.isAvailable();
}
return false;
}
// 判断wifi状态
public static boolean isWifiConnected(Context context) {
if (context != null) {
ConnectivityManager mConnectivityManager = (ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo mWiFiNetworkInfo = mConnectivityManager
.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
if (mWiFiNetworkInfo != null) {
return mWiFiNetworkInfo.isAvailable();
}
}
return false;
}
// 判断移动网络
public static boolean isMobileConnected(Context context) {
if (context != null) {
ConnectivityManager mConnectivityManager = (ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo mMobileNetworkInfo = mConnectivityManager
.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
if (mMobileNetworkInfo != null) {
return mMobileNetworkInfo.isAvailable();
}
}
return false;
}
// 获取连接类型
public static int getConnectedType(Context context) {
if (context != null) {
ConnectivityManager mConnectivityManager = (ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo mNetworkInfo = mConnectivityManager
.getActiveNetworkInfo();
if (mNetworkInfo != null && mNetworkInfo.isAvailable()) {
return mNetworkInfo.getType();
}
}
return -1;
}
/**
* 设置网络
* @param paramContext
*/
public static void startToSettings(Context paramContext) {
if (paramContext == null)
return;
try {
if (Build.VERSION.SDK_INT > 10) {
paramContext.startActivity(new Intent(
"android.settings.SETTINGS"));
return;
}
} catch (Exception localException) {
localException.printStackTrace();
return;
}
paramContext.startActivity(new Intent(
"android.settings.WIRELESS_SETTINGS"));
}
}
布局文件
---------------------------activity_main.xml------------------
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.pc.duandianxuchuan.MainActivity"
android:orientation="vertical"
android:gravity="center">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="上传文件"
android:id="@+id/btn"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="下载文件"
android:id="@+id/btn2"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="删除文件"
android:id="@+id/btn3"/>
</LinearLayout>