1、LoginWindow.java --登录窗口 - package com.hemi.rhet;
-
- import com.hemi.rhet.R;
-
- import android.app.Activity;
- import android.content.Intent;
- import android.os.Bundle;
- import android.view.KeyEvent;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.widget.Button;
- import android.widget.CheckBox;
- import android.widget.EditText;
-
- public class LoginWindow extends Activity {
-
- @Override
- public void onCreate(Bundle savedInstanceState) {
- System.out.println("enter LoginWindow.onCreate()!");
-
- super.onCreate(savedInstanceState);
-
- setContentView(R.layout.login_window);
-
- mUserName = (EditText)findViewById(R.id.username);
- mUserPasswd = (EditText)findViewById(R.id.userpasswd);
-
- cbx_cmwap = (CheckBox) findViewById(R.id.cbx_cmwap);
-
- loginButton = (Button) findViewById(R.id.login_button);
- exitButton = (Button) findViewById(R.id.exit_button);
-
- loginBtnListener = new View.OnClickListener() {
- public void onClick(View view) {
- LoginWindow.isCmwap = cbx_cmwap.isChecked();
-
- if (view == loginButton) {
- launchFetion();
- } else if(view == exitButton) {
- finish();
- }
- }
- };
-
- loginButton.setOnClickListener(loginBtnListener);
- exitButton.setOnClickListener(loginBtnListener);
- }
-
- private void launchFetion() {
- Intent i = new Intent(this, FuncSelector.class);
- i.putExtra(KEY_LOGIN_NAME, mUserName.getText().toString());
- i.putExtra(KEY_LOGIN_PASSWD, mUserPasswd.getText().toString());
- i.putExtra(KEY_LOGIN_TYPE, cbx_cmwap.isChecked());
-
- startActivity(i);
- }
-
- @Override
- public boolean onKeyDown(int keyCode, KeyEvent msg) {
-
-
-
-
-
-
- return false;
- }
-
- private Button loginButton, exitButton;
- private EditText mUserName;
- private EditText mUserPasswd;
- private CheckBox cbx_cmwap;
-
- private OnClickListener loginBtnListener;
-
- public static final String KEY_LOGIN_NAME = "login_name";
- public static final String KEY_LOGIN_PASSWD = "login_passwd";
- public static final String KEY_LOGIN_TYPE = "login_type";
-
- public static boolean isCmwap = false;
- }
2. FuncSelector.java -- 功能模块选择窗口 - package com.hemi.rhet;
-
- import java.util.ArrayList;
- import java.util.HashMap;
-
- import com.hemi.rhet.R;
-
- import android.app.Activity;
- import android.content.Intent;
- import android.os.Bundle;
- import android.util.Log;
- import android.view.Menu;
- import android.view.MenuItem;
- import android.view.View;
- import android.widget.AdapterView;
- import android.widget.GridView;
- import android.widget.SimpleAdapter;
- import android.widget.AdapterView.OnItemClickListener;
-
- public class FuncSelector extends Activity {
-
- @Override
- public void onCreate(Bundle savedInstanceState) {
- Log.i("info", "enter LoginWindow.onCreate()!");
- super.onCreate(savedInstanceState);
-
- setContentView(R.layout.func_selector);
-
- initFuncGrids();
- }
-
- private void initFuncGrids() {
- GridView funcSeleView = (GridView) findViewById(R.id.func_selector);
-
-
- ArrayList<HashMap<String, Object>> lstImageItem = new ArrayList<HashMap<String, Object>>();
-
- HashMap<String, Object> map = new HashMap<String, Object>();
- map.put("ItemImage", R.drawable.photo_upload);
- map.put("ItemText", getString(R.string.photo_upload));
- lstImageItem.add(map);
-
- map = new HashMap<String, Object>();
- map.put("ItemImage", R.drawable.icon);
- map.put("ItemText", getString(R.string.sys_config));
- lstImageItem.add(map);
-
- for (int i = 1; i <= 10; i++) {
- map = new HashMap<String, Object>();
- map.put("ItemImage", R.drawable.icon);
- map.put("ItemText", "NO." + String.valueOf(i));
- lstImageItem.add(map);
- }
-
-
- SimpleAdapter saImageItems = new SimpleAdapter(this,
- lstImageItem,
- R.layout.night_item,
-
-
- new String[] { "ItemImage", "ItemText" },
-
-
- new int[] {R.id.ItemImage,R.id.ItemText});
-
-
-
- funcSeleView.setAdapter(saImageItems);
-
-
-
- funcSeleView.setOnItemClickListener(new ItemClickListener());
- }
-
- public boolean onCreateOptionsMenu(Menu menu) {
- super.onCreateOptionsMenu(menu);
- menu.add(0, EXIT_ID,0, R.string.back_button);
-
- return true;
- }
-
-
- public boolean onMenuItemSelected(int featureId, MenuItem item) {
- boolean result = true;
-
- switch(item.getItemId()) {
- case EXIT_ID:
- this.finish();
- break;
-
- default:
- result = super.onMenuItemSelected(featureId, item);
- break;
- }
-
- return result;
- }
-
-
- class ItemClickListener implements OnItemClickListener {
- public void onItemClick(AdapterView<?> arg0,
-
- View arg1,
- int arg2,
- long arg3
- ) {
-
- HashMap<String, Object> item = (HashMap<String, Object>) arg0
- .getItemAtPosition(arg2);
-
- String tmpStr = (String) item.get("ItemText");
-
-
-
-
- Log.i("info", (String) item.get("ItemText"));
-
- ((SimpleAdapter) arg0.getAdapter()).notifyDataSetChanged();
-
- Intent i;
- switch (arg2) {
- case 0:
- i = new Intent();
- i.setClass(FuncSelector.this, PhotoUpload.class);
- startActivity(i);
- break;
-
- case 1:
- i = new Intent();
- i.setClass(FuncSelector.this, ConfigWindow.class);
- startActivity(i);
- break;
-
- default:
- break;
- }
- }
-
- }
-
- private static final int TAKE_PHOTO_ID = Menu.FIRST;
- private static final int UPLOAD_PHOTO_ID = Menu.FIRST + 1;
- private static final int EXIT_ID = Menu.FIRST + 3;
-
- }
3. PhotoUpload.java -- 照片上传模块 - package com.hemi.rhet;
-
- import java.io.BufferedReader;
- import java.io.ByteArrayOutputStream;
- import java.io.DataOutputStream;
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.FileNotFoundException;
- import java.io.FileOutputStream;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import java.io.OutputStream;
- import java.io.UnsupportedEncodingException;
- import java.net.HttpURLConnection;
- import java.net.MalformedURLException;
- import java.net.URL;
- import java.text.SimpleDateFormat;
- import java.util.ArrayList;
- import java.util.Date;
- import java.util.HashMap;
- import java.util.List;
-
- import org.apache.http.Header;
- import org.apache.http.HttpResponse;
- import org.apache.http.NameValuePair;
- import org.apache.http.client.ClientProtocolException;
- import org.apache.http.client.HttpClient;
- import org.apache.http.client.entity.UrlEncodedFormEntity;
- import org.apache.http.client.methods.HttpPost;
- import org.apache.http.entity.FileEntity;
- import org.apache.http.impl.client.DefaultHttpClient;
- import org.apache.http.message.BasicNameValuePair;
-
- import com.hemi.rhet.R;
-
- import android.app.Activity;
- import android.content.Intent;
- import android.graphics.Bitmap;
- import android.net.Uri;
- import android.os.Bundle;
- import android.os.Environment;
- import android.util.Log;
- import android.view.Menu;
- import android.view.MenuItem;
- import android.view.View;
- import android.widget.AdapterView;
- import android.widget.GridView;
- import android.widget.ImageView;
- import android.widget.SimpleAdapter;
- import android.widget.Toast;
- import android.widget.AdapterView.OnItemClickListener;
-
- public class PhotoUpload extends Activity {
-
- @Override
- public void onCreate(Bundle savedInstanceState) {
- Log.i("info", "enter LoginWindow.onCreate()!");
- super.onCreate(savedInstanceState);
-
- setContentView(R.layout.func_selector);
- }
-
- @Override
- protected void onActivityResult(int requestCode, int resultCode, Intent data) {
- super.onActivityResult(requestCode, resultCode, data);
-
- if (TAKE_PHOTO_ID == requestCode) {
- if (resultCode != RESULT_OK) return;
- Bundle extras = data.getExtras();
- try {
- Bitmap photoCaptured = (Bitmap) extras.get("data");
- ImageView img = new ImageView(this);
- img.setImageBitmap(photoCaptured);
- setContentView(img);
-
-
- ByteArrayOutputStream baos = new ByteArrayOutputStream();
- photoCaptured.compress(Bitmap.CompressFormat.JPEG, 100, baos);
- byte[] photoBytes = baos.toByteArray();
-
- File aFile = new File(getDatedFName(SD_CARD_TEMP_DIR));
- photoPath = aFile.getAbsolutePath();
-
- boolean b;
- if (aFile.exists()) b = aFile.delete();
-
- aFile.createNewFile();
-
- FileOutputStream fos = new FileOutputStream(aFile);
- fos.write(photoBytes);
- fos.close();
-
- Log.d("info", "onPictureTaken - wrote bytes: "
- + photoBytes.length);
-
- Uri capturedImage = Uri
- .parse(android.provider.MediaStore.Images.Media
- .insertImage(getContentResolver(), aFile
- .getAbsolutePath(), null, null));
- Log.i("camera", "Selected image: " + capturedImage.toString());
-
- } catch (FileNotFoundException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- }
-
- } else if (UPLOAD_PHOTO_ID == requestCode) {
-
- }
- }
-
- public boolean onCreateOptionsMenu(Menu menu) {
- super.onCreateOptionsMenu(menu);
- menu.add(0, TAKE_PHOTO_ID,0, R.string.take_photo);
- menu.add(0, UPLOAD_PHOTO_ID,0, R.string.upload_photo);
- menu.add(0, BACK_ID,0, R.string.back_button);
-
- return true;
- }
-
-
- public boolean onMenuItemSelected(int featureId, MenuItem item) {
- boolean result = true;
-
- switch(item.getItemId()) {
- case TAKE_PHOTO_ID:
- Log.i("info", "ready to take photos!");
-
- Intent i = new Intent("android.media.action.IMAGE_CAPTURE");
- startActivityForResult(i, TAKE_PHOTO_ID);
- result = true;
- break;
-
- case UPLOAD_PHOTO_ID:
- uploadFile2Svr();
- break;
-
- case BACK_ID:
- this.finish();
- break;
-
- default:
- result = super.onMenuItemSelected(featureId, item);
- break;
- }
-
- return result;
- }
-
- public void uploadFile2Svr() {
- HttpClient httpclient = new DefaultHttpClient();
- String urlStr = new StringBuffer().append(HTTP_PROTOCOL)
- .append(ConfigWindow.getServerIp())
- .append(':')
- .append(ConfigWindow.getServerPort())
- .append(FILE_UPLOADER_URL)
- .toString();
- HttpPost httppost = new HttpPost(urlStr);
-
- String uploadMsg = "上传 照片失败!";
- try {
- List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
-
- nameValuePairs.add(new BasicNameValuePair("filename", ("IMAGE.jpg")) );
-
-
-
-
-
- File aFile = new File(photoPath);
- Log.i("info -- photoPath: ", photoPath);
- FileEntity fileEty = new FileEntity(aFile, "binary/octet-stream");
- httppost.setEntity(fileEty);
- httppost.addHeader("filename", aFile.getName());
-
- HttpResponse response;
- response = httpclient.execute(httppost);
-
-
- Header[] headers = response.getAllHeaders();
- headers = response.getHeaders("resultcode");
- if (headers[0].getValue().equals("0")) {
- uploadMsg = "上传照片成功!";
- }
-
- } catch (UnsupportedEncodingException e) {
-
- uploadMsg += e.toString();
- Log.e("exception", e.toString());
- } catch (ClientProtocolException e) {
-
- uploadMsg += e.toString();
- Log.e("exception", e.toString());
- } catch (IOException e) {
-
- uploadMsg += e.toString();
- Log.e("exception", e.toString());
- } finally {
- Toast.makeText(PhotoUpload.this, uploadMsg,
- Toast.LENGTH_LONG).show();
- httpclient.getConnectionManager().shutdown();
- }
- }
-
- public void uploadFile2Svr2() {
- BufferedReader in = null;
-
- HttpClient httpclient = new DefaultHttpClient();
- String urlStr = new StringBuffer().append(HTTP_PROTOCOL)
- .append(ConfigWindow.getServerIp())
- .append(ConfigWindow.getServerPort())
- .append(FILE_UPLOADER_URL)
- .toString();
-
- URL url = null;
- try {
- url = new URL(urlStr);
- } catch (MalformedURLException e1) {
- e1.printStackTrace();
- }
-
- HttpURLConnection conn = null;
- DataOutputStream dos = null;
-
- String lineEnd = "\r\n";
- String twoHyphens = "--";
- String boundary = "*****";
- int maxBufferSize = 16 * 1024;
-
- try {
-
-
-
-
-
-
-
-
-
-
- conn = (HttpURLConnection) url.openConnection();
-
- conn.setConnectTimeout(120000);
-
-
- conn.setDoInput(true);
-
- conn.setDoOutput(true);
-
- conn.setUseCaches(false);
-
-
- conn.setRequestMethod("POST");
-
- conn.setRequestProperty("Connection", "Keep-Alive");
-
- conn.setRequestProperty("Content-Type",
-
- "application/x-www-form-urlencoded");
-
- conn.setRequestProperty("user-agent",
- "Mozilla/5.0 (Windows; U; Windows NT 5.2; zh-CN; rv:1.9.1.6) Gecko/20091201 Firefox/3.5.6 GTB6");
-
-
-
- conn.connect();
-
-
- dos = new DataOutputStream(conn.getOutputStream());
-
- dos.writeBytes(twoHyphens + boundary + lineEnd);
- dos.writeBytes("Content-Disposition: form-data; name=\"uploadedfile\";filename=\""
- + "exsistingFileName" + "\"" + lineEnd);
-
-
- Log.i("info", "Headers are written");
-
-
- FileInputStream fileInputStream = new FileInputStream(photoPath);
-
- int bytesAvailable = fileInputStream.available();
- int bufferSize = Math.min(bytesAvailable, maxBufferSize);
- byte[] buffer = new byte[bufferSize];
-
-
-
- int bytesRead = fileInputStream.read(buffer, 0, bufferSize);
-
- while (bytesRead > 0) {
- dos.write(buffer, 0, bufferSize);
- bytesAvailable = fileInputStream.available();
- bufferSize = Math.min(bytesAvailable, maxBufferSize);
- bytesRead = fileInputStream.read(buffer, 0, bufferSize);
- }
-
-
- dos.writeBytes(lineEnd);
- dos.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);
-
-
- Log.e("info", "File is written");
- fileInputStream.close();
-
- dos.flush();
- dos.close();
- dos = null;
-
-
-
-
-
-
- in = new BufferedReader(
- new InputStreamReader(conn.getInputStream()));
- StringBuffer sb = new StringBuffer("");
- String line = "";
- String NL = System.getProperty("line.separator");
- while ((line = in.readLine()) != null) {
- sb.append(line + NL);
- }
- in.close();
- String result = sb.toString();
- Log.i("info", result);
-
- } catch (ClientProtocolException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- } finally{
- if(in != null){
- try{
- in.close();
- }catch(IOException ioe){
- Log.e("error", ioe.toString());
- }
- }
- }
- }
-
- public static String getDatedFName(String fname) {
- StringBuffer result = new StringBuffer();
-
- SimpleDateFormat df = new SimpleDateFormat("yyMMddHHmmss");
- String dateSfx = "_" + df.format(new Date());
-
- int idx = fname.lastIndexOf('.');
- if (idx != -1) {
- result.append(fname.substring(0, idx));
- result.append(dateSfx);
- result.append(fname.substring(idx));
- } else {
- result.append(fname);
- result.append(dateSfx);
- }
-
- return result.toString();
- }
-
-
-
- private String photoPath = "/sdcard/IMAGE_100225083437.jpg";
-
- private static final int TAKE_PHOTO_ID = Menu.FIRST;
- private static final int UPLOAD_PHOTO_ID = Menu.FIRST + 1;
- private static final int BACK_ID = Menu.FIRST + 3;
-
- private static final String HTTP_PROTOCOL = "http://";
- private static final String FILE_UPLOADER_URL = "/fileuploader/system/fileUpload";
-
- private String SD_CARD_TEMP_DIR = Environment.getExternalStorageDirectory() + File.separator + "IMG.jpg";
-
- }
4. ConfigWindow.java--系统配置窗口 - package com.hemi.rhet;
-
- import com.hemi.rhet.R;
-
- import android.app.Activity;
- import android.content.Intent;
- import android.os.Bundle;
- import android.util.Log;
- import android.view.KeyEvent;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.widget.Button;
- import android.widget.CheckBox;
- import android.widget.EditText;
-
- public class ConfigWindow extends Activity {
-
- @Override
- public void onCreate(Bundle savedInstanceState) {
- System.out.println("enter ConfigWindow.onCreate()!");
-
- super.onCreate(savedInstanceState);
-
- setContentView(R.layout.config_window);
-
- mServerIP = (EditText)findViewById(R.id.serverip);
- mServerPort = (EditText)findViewById(R.id.serverport);
-
- mServerIP.setText(serverIp);
- mServerPort.setText(serverPort);
-
- okButton = (Button) findViewById(R.id.ok_button);
- backButton = (Button) findViewById(R.id.back_button);
-
- loginBtnListener = new View.OnClickListener() {
- public void onClick(View view) {
- if (view == okButton) {
- serverIp = mServerIP.getText().toString();
- serverPort = mServerPort.getText().toString();
- Log.i("info", "IP is: "+serverIp+"\tPort is: "+serverPort);
-
- finish();
-
- } else if(view == backButton) {
- finish();
- }
- }
- };
-
- okButton.setOnClickListener(loginBtnListener);
- backButton.setOnClickListener(loginBtnListener);
- }
-
- private void launchFetion() {
- Intent i = new Intent(this, FuncSelector.class);
- i.putExtra(KEY_LOGIN_NAME, mServerIP.getText().toString());
- i.putExtra(KEY_LOGIN_PASSWD, mServerPort.getText().toString());
-
- startActivity(i);
- }
-
- @Override
- public boolean onKeyDown(int keyCode, KeyEvent msg) {
-
-
-
-
-
-
- return false;
- }
-
- public static String getServerIp() {
- return serverIp;
- }
-
- public static String getServerPort() {
- return serverPort;
- }
-
- private Button okButton, backButton;
- private EditText mServerIP;
- private EditText mServerPort;
-
- private OnClickListener loginBtnListener;
-
- public static final String KEY_LOGIN_NAME = "login_name";
- public static final String KEY_LOGIN_PASSWD = "login_passwd";
- public static final String KEY_LOGIN_TYPE = "login_type";
-
- public static String serverIp = "192.168.0.98";
- public static String serverPort = "8081";
- }
还需要增加bg_logo.jpg、icon.png、photo_upload.png等几个图片。
|