实例介绍
【实例简介】
Android简洁版FTP服务器 Android平台FTP服务器,在开源项目的基础上优化,修复一些常见的bug,比如,复制中文类型文件到手机中时会出错。
【实例截图】【核心代码】
package com.way.app; import java.io.File; import java.net.InetAddress; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.content.IntentFilter; import android.graphics.Color; import android.net.wifi.WifiInfo; import android.net.wifi.WifiManager; import android.os.Bundle; import android.os.Environment; import android.view.Gravity; import android.view.KeyEvent; import android.view.View; import android.view.View.OnClickListener; import android.widget.CompoundButton; import android.widget.CompoundButton.OnCheckedChangeListener; import android.widget.ImageView; import android.widget.TextView; import android.widget.Toast; import com.way.ftp.Defaults; import com.way.ftp.FtpServerService; import com.way.ftp.Globals; import com.way.ftp.R; import com.way.slidingmenu.BaseSlidingFragmentActivity; import com.way.slidingmenu.SlidingMenu; public class MainActivity extends BaseSlidingFragmentActivity implements OnClickListener, OnCheckedChangeListener { private SlidingMenu mSlidingMenu; private TextView mIpText; private TextView mInstructionText; private TextView mInstructionTextPre; private View mStartStopButton; private Toast mToast; private long mFirstPressTime; BroadcastReceiver mWifiChangeReceiver = new BroadcastReceiver() { public void onReceive(Context context, Intent intent) { updateUi(); } }; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); initSlidingMenu(); setContentView(R.layout.above_slidingmenu); initViews(); } @Override protected void onResume() { super.onResume(); updateUi(); IntentFilter filter = new IntentFilter(); filter.addAction(WifiManager.WIFI_STATE_CHANGED_ACTION); filter.addAction(FtpServerService.ACTION_STARTED); filter.addAction(FtpServerService.ACTION_STOPPED); registerReceiver(mWifiChangeReceiver, filter); } @Override protected void onPause() { super.onPause(); unregisterReceiver(mWifiChangeReceiver); } private void initSlidingMenu() { setBehindContentView(R.layout.behind_slidingmenu); // customize the SlidingMenu mSlidingMenu = getSlidingMenu(); mSlidingMenu.setShadowWidthRes(R.dimen.shadow_width); mSlidingMenu.setBehindOffsetRes(R.dimen.slidingmenu_offset); mSlidingMenu.setFadeDegree(0.35f); mSlidingMenu.setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN); mSlidingMenu.setShadowDrawable(R.drawable.slidingmenu_shadow); mSlidingMenu.setShadowWidth(20); mSlidingMenu.setBehindScrollScale(0.333f); } private void initViews() { mIpText = (TextView) findViewById(R.id.ip_address); mInstructionText = (TextView) findViewById(R.id.instruction); mInstructionTextPre = (TextView) findViewById(R.id.instruction_pre); mStartStopButton = findViewById(R.id.start_stop_button); mStartStopButton.setOnClickListener(this); // quickly turn on or off wifi. findViewById(R.id.wifi_state_image).setOnClickListener(this); findViewById(R.id.ivTitleBtnLeft).setOnClickListener(this); updateUi(); } private void updateUi() { WifiManager wifiMgr = (WifiManager) getSystemService(Context.WIFI_SERVICE); // int wifiState = wifiMgr.getWifiState(); WifiInfo info = wifiMgr.getConnectionInfo(); String wifiId = info != null ? info.getSSID() : null; boolean isWifiReady = FtpServerService.isConnectedUsingWifi(); setText(R.id.wifi_state, isWifiReady ? wifiId : getString(R.string.no_wifi_hint)); ImageView wifiImg = (ImageView) findViewById(R.id.wifi_state_image); wifiImg.setImageResource(isWifiReady ? R.drawable.wifi_state4 : R.drawable.wifi_state0); boolean running = FtpServerService.isRunning(); if (running) { // Put correct text in start/stop button // Fill in wifi status and address InetAddress address = FtpServerService.getLocalInetAddress(); if (address != null) { String port = ":" FtpServerService.getPort(); mIpText.setText("ftp://" address.getHostAddress() (FtpServerService.getPort() == 21 ? "" : port)); } else { // could not get IP address, stop the service Context context = getApplicationContext(); Intent intent = new Intent(context, FtpServerService.class); context.stopService(intent); mIpText.setText(""); mIpText.setVisibility(View.GONE); } } mStartStopButton.setEnabled(isWifiReady); TextView startStopButtonText = (TextView) findViewById(R.id.start_stop_button_text); if (isWifiReady) { startStopButtonText.setText(running ? R.string.stop_server : R.string.start_server); startStopButtonText.setCompoundDrawablesWithIntrinsicBounds( running ? R.drawable.disconnect : R.drawable.connect, 0, 0, 0); startStopButtonText.setTextColor(running ? getResources().getColor( R.color.remote_disconnect_text) : getResources().getColor( R.color.remote_connect_text)); } else { if (FtpServerService.isRunning()) { Context context = getApplicationContext(); Intent intent = new Intent(context, FtpServerService.class); context.stopService(intent); } startStopButtonText.setText(R.string.no_wifi); startStopButtonText.setCompoundDrawablesWithIntrinsicBounds(0, 0, 0, 0); startStopButtonText.setTextColor(Color.GRAY); } mIpText.setVisibility(running ? View.VISIBLE : View.GONE); mInstructionText.setVisibility(running ? View.VISIBLE : View.GONE); mInstructionTextPre.setVisibility(running ? View.GONE : View.VISIBLE); } private void setText(int id, String text) { TextView tv = (TextView) findViewById(id); tv.setText(text); } private void showToast(int message) { if (null == mToast) { mToast = Toast.makeText(this, message, Toast.LENGTH_SHORT); mToast.setGravity(Gravity.CENTER, 0, 0); } else { mToast.setText(message); } mToast.show(); } /** * 连续按两次返回键就退出 */ @Override public boolean onKeyDown(int keyCode, KeyEvent event) { if (keyCode == KeyEvent.KEYCODE_BACK) { if (System.currentTimeMillis() - mFirstPressTime < 3000) { finish(); } else { mFirstPressTime = System.currentTimeMillis(); Toast.makeText(this, getResources().getString(R.string.press_again_exit), Toast.LENGTH_SHORT).show(); } return true; } else if (keyCode == KeyEvent.KEYCODE_MENU) { if (mSlidingMenu.isMenuShowing()) { mSlidingMenu.showContent(true); } else { mSlidingMenu.showMenu(true); } } return super.onKeyDown(keyCode, event); } @Override public void onClick(View v) { switch (v.getId()) { case R.id.ivTitleBtnLeft: mSlidingMenu.showMenu(true); break; case R.id.start_stop_button: Globals.setLastError(null); File chrootDir = new File(Defaults.chrootDir); if (!chrootDir.isDirectory()) { return; } Intent intent = new Intent(this, FtpServerService.class); Globals.setChrootDir(chrootDir); if (!FtpServerService.isRunning()) { if (Environment.MEDIA_MOUNTED.equals(Environment .getExternalStorageState())) { startService(intent); } else { showToast(R.string.storage_warning); } } else { stopService(intent); } break; case R.id.wifi_state_image: Intent intentWifi = new Intent( android.provider.Settings.ACTION_WIFI_SETTINGS); startActivity(intentWifi); break; default: break; } } @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { // TODO Auto-generated method stub } }
小贴士
感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。
- 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
- 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
- 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
- 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。
关于好例子网
本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明
网友评论
我要评论