文章标签 ‘源代码’
2011七月15

Android 开发:APN网络切换之CMNET

最近被Android系统的APN自动切换网络问题折腾死了,软件使用CMNET网络,而系统自带的一些软件必须使用CMWAP,或者手机厂家搞的一些后台服务或者流氓软件总是在切换网络。没办法,只好想个解决之道了。

我的解决方案是:
1、在程序启动时,注册 Receiver 监视网络状态,当网络发生变化判断不是CMNET时则切换网络;
2、为了保险起见,在每个HTTP链接请求前加上网络判断。

本软件主要实现了功能如下:
拍照、定位、表单文件上传、查询、短信拦截(用于通过短信指令获得手机当前位置)、拨打电话、定时自动上传定位数据、版本更新等。

如下粘贴APN网络判断网站代码:
1、NetworkChangeReceiver  网络状态监视

package com.wiz.receiver;

import com.wiz.tools.NetCheck;
import com.wiz.tools.StringUtils;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.util.Log;

public class NetworkChangeReceiver  extends BroadcastReceiver {
	NetCheck netCheck=new NetCheck();
	 public void onReceive(Context context, Intent intent) {
		 Log.e("NetworkChangeReceiver", "onReceive");
		 ConnectivityManager conManager= (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE); 

        if (ConnectivityManager.CONNECTIVITY_ACTION.equals(intent.getAction())) {
            NetworkInfo info = conManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
            String apn = StringUtils.null2String(info.getExtraInfo());
            if (!"cmnet".equals(apn.toLowerCase())) {
            	netCheck.checkNetworkInfo(context);
            }
        }
    }
}

2、Activity 中注册 NetworkChangeReceiver

NetworkChangeReceiver ncr = new NetworkChangeReceiver();
IntentFilter upIntentFilter = new IntentFilter( ConnectivityManager.CONNECTIVITY_ACTION);
this.registerReceiver(ncr, upIntentFilter);// 网络状态监控

3、APN判断及网络切换

package com.wiz.tools;
import android.content.ContentResolver;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.Uri;
import android.util.Log;

public class NetCheck {
	public static final Uri APN_URI = Uri.parse("content://telephony/carriers");
	public static final Uri CURRENT_APN_URI = Uri
			.parse("content://telephony/carriers/preferapn");
	public static String getCurrentAPNFromSetting(ContentResolver resolver) {
        Cursor cursor = null;
        try {
            cursor = resolver.query(CURRENT_APN_URI, null, null, null, null);
            String curApnId = null;
            String apnName1=null;
            if (cursor != null && cursor.moveToFirst()) {
                curApnId = cursor.getString(cursor.getColumnIndex("_id"));
                apnName1 = cursor.getString(cursor.getColumnIndex("apn"));
            }
            cursor.close();
            Log.e("NetCheck getCurrentAPNFromSetting","curApnId:"+curApnId+" apnName1:"+apnName1);
            //find apn name from apn list
            if (curApnId != null) {
                cursor = resolver.query(APN_URI, null, " _id = ?", new String[]{curApnId}, null);
                if (cursor != null && cursor.moveToFirst()) {
                    String apnName = cursor.getString(cursor.getColumnIndex("apn"));
                    return apnName;
                }
            } 

        } catch (SQLException e) {
            Log.e("NetCheck getCurrentAPNFromSetting",e.getMessage());
        } finally {
            if (cursor != null) {
                cursor.close();
            }
        } 

        return null;
}
	public static int updateCurrentAPN(ContentResolver resolver, String newAPN) {
        Cursor cursor = null;
        try {
            //get new apn id from list
            cursor = resolver.query(APN_URI, null, " apn = ? and current = 1", new String[]{newAPN.toLowerCase()}, null);
            String apnId = null;
            if (cursor != null && cursor.moveToFirst()) {
                apnId = cursor.getString(cursor.getColumnIndex("_id"));
            }
            cursor.close();
            Log.e("NetCheck updateCurrentAPN","apnId:"+apnId);
            //set new apn id as chosen one
            if (apnId != null) {
                ContentValues values = new ContentValues();
                values.put("apn_id", apnId);
                resolver.update(CURRENT_APN_URI, values, null, null);
            } else {
                //apn id not found, return 0.
                return 0;
            }
        } catch (SQLException e) {
        	Log.e("NetCheck updateCurrentAPN",e.getMessage());
        } finally {
            if (cursor != null) {
                cursor.close();
            }
        } 

        //update success
        return 1;
} 

	public boolean checkNetworkInfo(Context c) {
		boolean ret=false;
		ConnectivityManager conManager= (ConnectivityManager) c.getSystemService(Context.CONNECTIVITY_SERVICE);
		NetworkInfo info = conManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
		boolean internet=conManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE).isConnectedOrConnecting();
        String oldAPN = StringUtils.null2String(info.getExtraInfo());
        String oldSQLAPN=StringUtils.null2String(getCurrentAPNFromSetting(c.getContentResolver()));

        Log.e("NetCheck checkNetworkInfo","oldAPN:"+oldAPN+" oldSQLAPN:"+oldSQLAPN);
        if (internet==false||!"cmnet".equals(oldAPN.toLowerCase())||!"cmnet".equals(oldSQLAPN.toLowerCase())) {
            if("cmwap".equals(oldAPN.toLowerCase())&&"cmnet".equals(oldSQLAPN.toLowerCase())){
            	updateCurrentAPN(c.getContentResolver(), "cmwap");
            	try {
					Thread.sleep(200);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
            }
            updateCurrentAPN(c.getContentResolver(), "cmnet");
            try {
				Thread.sleep(200);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
            ret=true;
        }
        return ret; 

	}
}

4、HTTP请求前的判断

if (nc.checkNetworkInfo(LoginActivity.this)) {
				Thread.sleep(5000);// 设置cmnet网络
			}

本文原创,转载请注明来源 wizzer.cn。

2011五月28

Android:指定分辨率和清晰度的图片压缩方法源码

public void transImage(String fromFile, String toFile, int width, int height, int quality)
	{
		try
		{
			Bitmap bitmap = BitmapFactory.decodeFile(fromFile);
			int bitmapWidth = bitmap.getWidth();
			int bitmapHeight = bitmap.getHeight();
			// 缩放图片的尺寸
			float scaleWidth = (float) width / bitmapWidth;
			float scaleHeight = (float) height / bitmapHeight; 
			Matrix matrix = new Matrix();
			matrix.postScale(scaleWidth, scaleHeight);
			// 产生缩放后的Bitmap对象
			Bitmap resizeBitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmapWidth, bitmapHeight, matrix, false);
			// save file
			File myCaptureFile = new File(toFile);
			FileOutputStream out = new FileOutputStream(myCaptureFile);
			if(resizeBitmap.compress(Bitmap.CompressFormat.JPEG, quality, out)){
				out.flush();
				out.close();
			}
			if(!bitmap.isRecycled()){
				bitmap.recycle();//记得释放资源,否则会内存溢出
			}
			if(!resizeBitmap.isRecycled()){
				resizeBitmap.recycle();
			}

		}
		catch (FileNotFoundException e)
		{
			e.printStackTrace();
		}
		catch (IOException ex)
		{
			ex.printStackTrace();
		}
	}
2011五月19

Android:解决ProgressDialog提示框不转动

ProgressDialog 解决“第一次执行图标转动,第二次执行不转动”代码:

@Override
	protected Dialog onCreateDialog(int id) {
		switch (id) {
		case PROGRESS_DIALOG:
			progressDialog = new ProgressDialog(LoginActivity.this);
			progressDialog.setMessage("正在登陆,请稍等...");
			progressDialog.setCancelable(true);
			// 设置ProgressDialog 是否可以按退回按键取消
			return progressDialog;
		default:
			return null;
		}
	}

	@Override
	protected void onPrepareDialog(int id, Dialog dialog) {
		switch (id) {
		case PROGRESS_DIALOG:
			dialog
					.setOnDismissListener(new DialogInterface.OnDismissListener() {
						@Override
						public void onDismiss(DialogInterface dialog) {
							removeDialog(PROGRESS_DIALOG);//这个起作用
							if (progressThread != null) {
								progressThread = null;
							}
						}
					});
		}
	}
2011五月19

Android:设置APN为cmnet源码

public class APNActivity extends Activity {

        public static final Uri APN_URI = Uri.parse("content://telephony/carriers");
        public static final Uri CURRENT_APN_URI = Uri.parse("content://telephony/carriers/preferapn");

        @Override
        public void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.main);
                int _cmnetId = addAPN();
                SetAPN(_cmnetId);
        }
       public void checkAPN(){
      // 检查当前连接的APN
              Cursor cr = getContentResolver().query(CURRENT_APN_URI, null, null,
              null, null);
              while (cr != null && cr.moveToNext()) {
                  // APN id
                  String id = cr.getString(cr.getColumnIndex("_id"));
                  // APN name
                  String apn = StringUtils.null2String(cr
                  .getString(cr.getColumnIndex("apn")));
                  // Toast.makeText(getApplicationContext(),
                  // "当前 id:" + id + " apn:" + apn, Toast.LENGTH_LONG).show();

       }

        //新增一个cmnet接入点
        public int addAPN() {
                int id = -1;
                ContentResolver resolver = this.getContentResolver();
                ContentValues values = new ContentValues();
                values.put("name", "cmnet");
                values.put("apn", "cmnet");
                Cursor c = null;
                Uri newRow = resolver.insert(APN_URI, values);
                if (newRow != null) {
                        c = resolver.query(newRow, null, null, null, null);
                        int idIndex = c.getColumnIndex("_id");
                        c.moveToFirst();
                        id = c.getShort(idIndex);
                }
                if (c != null)
                        c.close();
                return id;
        }
        //设置接入点
        public void SetAPN(int id) {
                ContentResolver resolver = this.getContentResolver();
                ContentValues values = new ContentValues();
                values.put("apn_id", id);
                resolver.update(CURRENT_APN_URI, values, null, null);
        }
}
2011五月4

Android 手机端与服务端POST数据交互类

 

package com.wizzer.tools;

import java.io.*;
import java.net.URLEncoder;

import java.util.*;

import org.apache.http.HttpResponse;

import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HTTP;
import org.apache.http.NameValuePair;
import org.json.JSONArray;
import org.json.JSONException;

import android.app.Activity;
import android.app.Dialog;
import android.app.ProgressDialog;

public class BaseProtocol 
{
	private StringBuilder sb = new StringBuilder();
	private HttpClient httpClient;
	private HttpPost httpRequest;
	private HttpResponse response;
	private List<NameValuePair> nameValuePair = new ArrayList<NameValuePair>();
	private static final int DIALOG1_KEY = 0;
	private static final int DIALOG2_KEY = 1;

	public BaseProtocol()
	{
		httpClient = new DefaultHttpClient();
	}

	/**
	 * *向服务器端发送请求 * *@paramurl *@throwsException
	 * 
	 * @throws UnsupportedEncodingException
	 */
	public void pack(String url) throws Exception
	{
		httpClient = new DefaultHttpClient();
		httpRequest = new HttpPost(url);
		httpRequest.setEntity(new UrlEncodedFormEntity(nameValuePair));
		response = httpClient.execute(httpRequest);
	}

	/** *得到返回数据 * *@paramurl *@return *@throwsException */

	public String parse() throws Exception
	{
		// TODO状态处理500200
		if (response.getStatusLine().getStatusCode() == 200)
		{
			BufferedReader bufferedReader2 = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
			for (String s = bufferedReader2.readLine(); s != null; s = bufferedReader2.readLine())
			{
				sb.append(s);
			}
		}
		return sb.toString();
	}

	/***
	 * 向服务器发送信息 * *@paramkey *@paramvalue
	 * 
	 * @throws UnsupportedEncodingException
	 */
	public void addNameValuePair(String key, String value) throws UnsupportedEncodingException
	{
		nameValuePair.add(new BasicNameValuePair(key, URLEncoder.encode(value, HTTP.UTF_8)));
	}

	/** *返回JSONArray对象数据模型 * *@return *@throwsJSONException */

	public JSONArray getJSON() throws JSONException
	{
		return new JSONArray(sb.toString());
	}

}
2010十二月31

JAVA发送EMAIL的例子

import javax.mail.*;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.InternetAddress;
import java.io.UnsupportedEncodingException;
import java.util.Properties;

/**
 * Created by IntelliJ IDEA.
 * User: Wizzer
 * Date: 2010-12-29
 * Time: 16:39:50
 * To change this template use File | Settings | File Templates.
 */
public class Mail {
    public static void main(String args[]) throws MessagingException, UnsupportedEncodingException {
    Properties props = new Properties();
    props.put("mail.smtp.host","smtp.qq.com");
    props.put("mail.smtp.auth","true");
    PopupAuthenticator auth = new PopupAuthenticator(); 
    Session session = Session.getInstance(props, auth);
    MimeMessage message = new MimeMessage(session);
    Address addressFrom = new InternetAddress(PopupAuthenticator.mailuser+"@qq.com", "George Bush");
    Address addressTo = new InternetAddress("116****@qq.com", "George Bush");//收件人
    message.setText("邮件发送成功");
    message.setSubject("Javamal最终测试");
    message.setFrom(addressFrom);
    message.addRecipient(Message.RecipientType.TO,addressTo);
    message.saveChanges();
    Transport transport = session.getTransport("smtp");
    transport.connect("smtp.qq.com", PopupAuthenticator.mailuser,PopupAuthenticator.password);
    transport.send(message);
    transport.close();
    }

}
class PopupAuthenticator extends Authenticator {
public static final String mailuser="wizzer"; 
public static final String password="********";
public PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(mailuser,password);
}
}
2010十一月17

新浪微博Android 客户端通过HTTP POST发布图片和文字源代码(作废)

1、发送图片+文字

要特别注意,图片的文件名要为 pic 才会被新浪接收。

              Map map = new HashMap();
	   map.put("source", "appkey");//改成自己的key
	   map.put("status", txt);
	   postImg("http://api.t.sina.com.cn/statuses/upload.json",map,Environment.getExternalStorageDirectory()+ "/temp.jpg"
								,"帐号名字","密码");
              /**
	 * 直接通过HTTP协议提交数据到服务器,实现表单提交功能
	 * @param actionUrl 上传路径
	 * @param params 请求参数 key为参数名,value为参数值
	 * @param filename 上传文件
	 * @param username 用户名
	 * @param password 密码
	 */
	private void postImg(String actionUrl,Map<String, String> params, String  filename,String username,String password) {
		try {
			String BOUNDARY = "--------------et567z"; //数据分隔线
			String MULTIPART_FORM_DATA = "Multipart/form-data";  

			URL url = new URL(actionUrl);
			HttpURLConnection conn = (HttpURLConnection) url.openConnection(); 

			conn.setDoInput(true);//允许输入
			conn.setDoOutput(true);//允许输出
			conn.setUseCaches(false);//不使用Cache
			conn.setRequestMethod("POST");
			conn.setRequestProperty("Connection", "Keep-Alive");
			conn.setRequestProperty("Charset", "UTF-8");
			conn.setRequestProperty("Content-Type", MULTIPART_FORM_DATA + ";boundary=" + BOUNDARY);
			String usernamePassword=username+":"+password;
			conn.setRequestProperty("Authorization","Basic "+new String(SecBase64.encode(usernamePassword.getBytes())));

			StringBuilder sb = new StringBuilder();  

			//上传的表单参数部分,格式请参考文章
			for (Map.Entry<String, String> entry : params.entrySet()) {//构建表单字段内容
				sb.append("--");
				sb.append(BOUNDARY);
				sb.append("\r\n");
				sb.append("Content-Disposition: form-data; name=\""+ entry.getKey() + "\"\r\n\r\n");
				sb.append(entry.getValue());
				sb.append("\r\n");
			}
			//            System.out.println(sb.toString());
			DataOutputStream outStream = new DataOutputStream(conn.getOutputStream());
			outStream.write(sb.toString().getBytes());//发送表单字段数据
			byte[] content = readFileImage(filename);
			//上传的文件部分,格式请参考文章
			//System.out.println("content:"+content.toString());
			StringBuilder split = new StringBuilder();
			split.append("--");
			split.append(BOUNDARY);
			split.append("\r\n");
			split.append("Content-Disposition: form-data;name=\"pic\";filename=\"temp.jpg\"\r\n");
			split.append("Content-Type: image/jpg\r\n\r\n");
			System.out.println(split.toString());
			outStream.write(split.toString().getBytes());
			outStream.write(content, 0, content.length);
			outStream.write("\r\n".getBytes());  

			byte[] end_data = ("--" + BOUNDARY + "--\r\n").getBytes();//数据结束标志
			outStream.write(end_data);
			outStream.flush();
			int cah = conn.getResponseCode();
			//            if (cah != 200) throw new RuntimeException("请求url失败:"+cah);
			if(cah == 200)//如果发布成功则提示成功
			{
				/*读返回数据*/
				//String strResult = EntityUtils.toString(httpResponse.getEntity()); 

				new AlertDialog.Builder(Main.this)
				// Main.this视情况而定,这个一般是指当前显示的Activity对应的XML视窗。
				.setTitle("")// 设置对话框的标题
				.setPositiveButton("确定",// 设置对话框的确认按钮
						new DialogInterface.OnClickListener() {// 设置确认按钮的事件
					public void onClick(DialogInterface dialog, int which) {

					}})
					.setMessage(" 发布成功 ")// 设置对话框的内容
					.show();
			}
			else if(cah == 400)
			{
				new AlertDialog.Builder(Main.this)
				// Main.this视情况而定,这个一般是指当前显示的Activity对应的XML视窗。
				.setTitle("")// 设置对话框的标题
				.setPositiveButton("确定",// 设置对话框的确认按钮
						new DialogInterface.OnClickListener() {// 设置确认按钮的事件
					public void onClick(DialogInterface dialog, int which) {

					}})
					.setMessage(" 发布失败  \n 不可连续发布相同内容 ")// 设置对话框的内容
					.show();
			}else{
				throw new RuntimeException("请求url失败:"+cah);
			} 

			//            InputStream is = conn.getInputStream();
			//            int ch;
			//            StringBuilder b = new StringBuilder();
			//            while( (ch = is.read()) != -1 ){
			//                b.append((char)ch);
			//            }
			outStream.close();
			conn.disconnect();
		}
		catch (IOException e)
		{
			e.printStackTrace(); 

		}
		catch (Exception e)
		{
			e.printStackTrace();  

		}  

	}

             public static byte[] readFileImage(String filename) throws IOException {
		BufferedInputStream bufferedInputStream = new BufferedInputStream(
				new FileInputStream(filename));
		int len = bufferedInputStream.available();
		byte[] bytes = new byte[len];
		int r = bufferedInputStream.read(bytes);
		if (len != r) {
			bytes = null;
			throw new IOException("读取文件不正确");
		}
		bufferedInputStream.close();
		return bytes;
	}

2、只发文字

  //POST发布文本信息
	    public  void sendMsg(String status,String username,String password){
			HttpClient httpclient = new DefaultHttpClient();
	        HttpPost httppost = new HttpPost("http://api.t.sina.com.cn/statuses/update.json");
		        //NameValuePair实现请求参数的封装

		        List  params = new ArrayList ();
		        params.add(new BasicNameValuePair("source", "4016954419"));
		        params.add(new BasicNameValuePair("status", status));
		        try
		        { 

		          //添加请求参数到请求对象

		        	httppost.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8));
		        	httppost.getParams().setBooleanParameter(CoreProtocolPNames.USE_EXPECT_CONTINUE, false); 

		        	String data=username+":"+password;
		        	httppost.addHeader("Authorization","Basic "+new String(SecBase64.encode(data.getBytes())));
		        	httppost.addHeader("Content-Type", "application/x-www-form-urlencoded");

		          //发送请求并等待响应
		          HttpResponse httpResponse = new DefaultHttpClient().execute(httppost);
		          //若状态码为200 ok
		          if(httpResponse.getStatusLine().getStatusCode() == 200)
		          {
		            //读返回数据
		            //String strResult = EntityUtils.toString(httpResponse.getEntity()); 

		            new AlertDialog.Builder(Main.this)
					// Main.this视情况而定,这个一般是指当前显示的Activity对应的XML视窗。
					.setTitle("")// 设置对话框的标题
					.setPositiveButton("确定",// 设置对话框的确认按钮
				    new DialogInterface.OnClickListener() {// 设置确认按钮的事件
				        public void onClick(DialogInterface dialog, int which) {

				    }})
					.setMessage(" 发布成功 ")// 设置对话框的内容
					.show();
		          }
		          else if(httpResponse.getStatusLine().getStatusCode() == 400)
		          {
		        	  new AlertDialog.Builder(Main.this)
						// Main.this视情况而定,这个一般是指当前显示的Activity对应的XML视窗。
						.setTitle("")// 设置对话框的标题
						.setPositiveButton("确定",// 设置对话框的确认按钮
				    new DialogInterface.OnClickListener() {// 设置确认按钮的事件
				        public void onClick(DialogInterface dialog, int which) {

				    }})
						.setMessage(" 发布失败  \n 不可连续发布相同内容 ")// 设置对话框的内容
						.show();
		          } 

		        }
		        catch (ClientProtocolException e)
		        {
		          e.printStackTrace();
		          et.setText(et.getText()+" Error1:"+e.getMessage());
		        }
		        catch (IOException e)
		        {
		          e.printStackTrace();
		          et.setText(et.getText()+" Error2:"+e.getMessage());
		        }
		        catch (Exception e)
		        {
		          e.printStackTrace();
		          et.setText(et.getText()+" Error3:"+e.getMessage());
		        }  

		 }

3、加密类 SecBase64.java

package wizzer.cn.app;

public class SecBase64 {
private static final byte[] encodingTable = { (byte) 'A', (byte) 'B',
    (byte) 'C', (byte) 'D', (byte) 'E', (byte) 'F', (byte) 'G',
    (byte) 'H', (byte) 'I', (byte) 'J', (byte) 'K', (byte) 'L',
    (byte) 'M', (byte) 'N', (byte) 'O', (byte) 'P', (byte) 'Q',
    (byte) 'R', (byte) 'S', (byte) 'T', (byte) 'U', (byte) 'V',
    (byte) 'W', (byte) 'X', (byte) 'Y', (byte) 'Z', (byte) 'a',
    (byte) 'b', (byte) 'c', (byte) 'd', (byte) 'e', (byte) 'f',
    (byte) 'g', (byte) 'h', (byte) 'i', (byte) 'j', (byte) 'k',
    (byte) 'l', (byte) 'm', (byte) 'n', (byte) 'o', (byte) 'p',
    (byte) 'q', (byte) 'r', (byte) 's', (byte) 't', (byte) 'u',
    (byte) 'v', (byte) 'w', (byte) 'x', (byte) 'y', (byte) 'z',
    (byte) '0', (byte) '1', (byte) '2', (byte) '3', (byte) '4',
    (byte) '5', (byte) '6', (byte) '7', (byte) '8', (byte) '9',
    (byte) '+', (byte) '/' };
private static final byte[] decodingTable;
static {
   decodingTable = new byte[128];
   for (int i = 0; i < 128; i++) {
    decodingTable[i] = (byte) -1;
   }
   for (int i = 'A'; i <= 'Z'; i++) {
    decodingTable[i] = (byte) (i - 'A');
   }
   for (int i = 'a'; i <= 'z'; i++) {
    decodingTable[i] = (byte) (i - 'a' + 26);
   }
   for (int i = '0'; i <= '9'; i++) {
    decodingTable[i] = (byte) (i - '0' + 52);
   }
   decodingTable['+'] = 62;
   decodingTable['/'] = 63;
}

//加密

public static byte[] encode(byte[] data) {
   byte[] bytes;
   int modulus = data.length % 3;
   if (modulus == 0) {
    bytes = new byte[(4 * data.length) / 3];
   } else {
    bytes = new byte[4 * ((data.length / 3) + 1)];
   }
   int dataLength = (data.length - modulus);
   int a1;
   int a2;
   int a3;
   for (int i = 0, j = 0; i < dataLength; i += 3, j += 4) {
    a1 = data[i] & 0xff;
    a2 = data[i + 1] & 0xff;
    a3 = data[i + 2] & 0xff;
    bytes[j] = encodingTable[(a1 >>> 2) & 0x3f];
    bytes[j + 1] = encodingTable[((a1 << 4) | (a2 >>> 4)) & 0x3f];
    bytes[j + 2] = encodingTable[((a2 << 2) | (a3 >>> 6)) & 0x3f];
    bytes[j + 3] = encodingTable[a3 & 0x3f];
   }
   int b1;
   int b2;
   int b3;
   int d1;
   int d2;
   switch (modulus) {
   case 0: 
    break;
   case 1:
    d1 = data[data.length - 1] & 0xff;
    b1 = (d1 >>> 2) & 0x3f;
    b2 = (d1 << 4) & 0x3f;
    bytes[bytes.length - 4] = encodingTable[b1];
    bytes[bytes.length - 3] = encodingTable[b2];
    bytes[bytes.length - 2] = (byte) '=';
    bytes[bytes.length - 1] = (byte) '=';
    break;
   case 2:
    d1 = data[data.length - 2] & 0xff;
    d2 = data[data.length - 1] & 0xff;
    b1 = (d1 >>> 2) & 0x3f;
    b2 = ((d1 << 4) | (d2 >>> 4)) & 0x3f;
    b3 = (d2 << 2) & 0x3f;
    bytes[bytes.length - 4] = encodingTable[b1];
    bytes[bytes.length - 3] = encodingTable[b2];
    bytes[bytes.length - 2] = encodingTable[b3];
    bytes[bytes.length - 1] = (byte) '=';
    break;
   }
   return bytes;
}

//解密

public static byte[] decode(byte[] data) {
   byte[] bytes;
   byte b1;
   byte b2;
   byte b3;
   byte b4;
   data = discardNonBase64Bytes(data);
   if (data[data.length - 2] == '=') {
    bytes = new byte[(((data.length / 4) - 1) * 3) + 1];
   } else if (data[data.length - 1] == '=') {
    bytes = new byte[(((data.length / 4) - 1) * 3) + 2];
   } else {
    bytes = new byte[((data.length / 4) * 3)];
   }
   for (int i = 0, j = 0; i < (data.length - 4); i += 4, j += 3) {
    b1 = decodingTable[data[i]];
    b2 = decodingTable[data[i + 1]];
    b3 = decodingTable[data[i + 2]];
    b4 = decodingTable[data[i + 3]];
    bytes[j] = (byte) ((b1 << 2) | (b2 >> 4));
    bytes[j + 1] = (byte) ((b2 << 4) | (b3 >> 2));
    bytes[j + 2] = (byte) ((b3 << 6) | b4);
   }
   if (data[data.length - 2] == '=') {
    b1 = decodingTable[data[data.length - 4]];
    b2 = decodingTable[data[data.length - 3]];
    bytes[bytes.length - 1] = (byte) ((b1 << 2) | (b2 >> 4));
   } else if (data[data.length - 1] == '=') {
    b1 = decodingTable[data[data.length - 4]];
    b2 = decodingTable[data[data.length - 3]];
    b3 = decodingTable[data[data.length - 2]];
    bytes[bytes.length - 2] = (byte) ((b1 << 2) | (b2 >> 4));
    bytes[bytes.length - 1] = (byte) ((b2 << 4) | (b3 >> 2));
   } else {
    b1 = decodingTable[data[data.length - 4]];
    b2 = decodingTable[data[data.length - 3]];
    b3 = decodingTable[data[data.length - 2]];
    b4 = decodingTable[data[data.length - 1]];
    bytes[bytes.length - 3] = (byte) ((b1 << 2) | (b2 >> 4));
    bytes[bytes.length - 2] = (byte) ((b2 << 4) | (b3 >> 2));
    bytes[bytes.length - 1] = (byte) ((b3 << 6) | b4);
   }
   return bytes;
}

//解密

public static byte[] decode(String data) {
   byte[] bytes;
   byte b1;
   byte b2;
   byte b3;
   byte b4;
   data = discardNonBase64Chars(data);
   if (data.charAt(data.length() - 2) == '=') {
    bytes = new byte[(((data.length() / 4) - 1) * 3) + 1];
   } else if (data.charAt(data.length() - 1) == '=') {
    bytes = new byte[(((data.length() / 4) - 1) * 3) + 2];
   } else {
    bytes = new byte[((data.length() / 4) * 3)];
   }
   for (int i = 0, j = 0; i < (data.length() - 4); i += 4, j += 3) {
    b1 = decodingTable[data.charAt(i)];
    b2 = decodingTable[data.charAt(i + 1)];
    b3 = decodingTable[data.charAt(i + 2)];
    b4 = decodingTable[data.charAt(i + 3)];
    bytes[j] = (byte) ((b1 << 2) | (b2 >> 4));
    bytes[j + 1] = (byte) ((b2 << 4) | (b3 >> 2));
    bytes[j + 2] = (byte) ((b3 << 6) | b4);
   }
   if (data.charAt(data.length() - 2) == '=') {
    b1 = decodingTable[data.charAt(data.length() - 4)];
    b2 = decodingTable[data.charAt(data.length() - 3)];
    bytes[bytes.length - 1] = (byte) ((b1 << 2) | (b2 >> 4));
   } else if (data.charAt(data.length() - 1) == '=') {
    b1 = decodingTable[data.charAt(data.length() - 4)];
    b2 = decodingTable[data.charAt(data.length() - 3)];
    b3 = decodingTable[data.charAt(data.length() - 2)];
    bytes[bytes.length - 2] = (byte) ((b1 << 2) | (b2 >> 4));
    bytes[bytes.length - 1] = (byte) ((b2 << 4) | (b3 >> 2));
   } else {
    b1 = decodingTable[data.charAt(data.length() - 4)];
    b2 = decodingTable[data.charAt(data.length() - 3)];
    b3 = decodingTable[data.charAt(data.length() - 2)];
    b4 = decodingTable[data.charAt(data.length() - 1)];
    bytes[bytes.length - 3] = (byte) ((b1 << 2) | (b2 >> 4));
    bytes[bytes.length - 2] = (byte) ((b2 << 4) | (b3 >> 2));
    bytes[bytes.length - 1] = (byte) ((b3 << 6) | b4);
   }
   return bytes;
}

private static byte[] discardNonBase64Bytes(byte[] data) {
   byte[] temp = new byte[data.length];
   int bytesCopied = 0;
   for (int i = 0; i < data.length; i++) {
    if (isValidBase64Byte(data[i])) {
     temp[bytesCopied++] = data[i];
    }
   }
   byte[] newData = new byte[bytesCopied];
   System.arraycopy(temp, 0, newData, 0, bytesCopied);
   return newData;
}

private static String discardNonBase64Chars(String data) {
   StringBuffer sb = new StringBuffer();
   int length = data.length();
   for (int i = 0; i < length; i++) {
    if (isValidBase64Byte((byte) (data.charAt(i)))) {
     sb.append(data.charAt(i));
    }
   }
   return sb.toString();
}

private static boolean isValidBase64Byte(byte b) {
   if (b == '=') {
    return true;
   } else if ((b < 0) || (b >= 128)) {
    return false;
   } else if (decodingTable[b] == -1) {
    return false;
   }
   return true;
}

//测试类
public static void main(String[] args) {
   String data = "wizzer@qq.com:etpass";
   byte[] result = SecBase64.encode(data.getBytes());// 加密
   System.out.println("Basic "+data);
   System.out.println("Basic "+new String(result));
   System.out.println(new String(SecBase64.decode(new String(result))));// 解密
   }
}
2010十一月16

Android 2.1 GPS定位和拍照功能代码

1、GPS功能代码

private void getLocation()
	{
		LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
		locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,
				 200, 0, locationListener);

	}
	private final LocationListener locationListener = new LocationListener() {
	    public void onLocationChanged(Location location) { //当坐标改变时触发此函数,如果Provider传进相同的坐标,它就不会被触发
	        // log it when the location changes
	        if (location != null) {
	        	Lat.setText(String.valueOf(location.getLatitude()));
	        	Lon.setText(String.valueOf(location.getLongitude()));

	        }
	    }

	    public void onProviderDisabled(String provider) {
	    // Provider被disable时触发此函数,比如GPS被关闭
	    }

	    public void onProviderEnabled(String provider) {
	    //  Provider被enable时触发此函数,比如GPS被打开
	    }

	    public void onStatusChanged(String provider, int status, Bundle extras) {
	    // Provider的转态在可用、暂时不可用和无服务三个状态直接切换时触发此函数
	    }
	};

2、拍照功能代码

public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
     // Hide the window title.
		requestWindowFeature(Window.FEATURE_NO_TITLE);

        setContentView(R.layout.main);
        imageView = (ImageView) this.findViewById(R.id.iv1);
		Button button = (Button) this.findViewById(R.id.bt1);
		button.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
				intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri
						.fromFile(new File(Environment
								.getExternalStorageDirectory(), "temp.jpg")));
				intent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 0);
				startActivityForResult(intent, 0);
			}
		});

    }
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		if (requestCode == 0 && resultCode == Activity.RESULT_OK) {
			this.imageView.setImageDrawable(Drawable.createFromPath(new File(
					Environment.getExternalStorageDirectory(), "temp.jpg")
					.getAbsolutePath()));

		}
	}

3、退出程序确认

public boolean onKeyDown(int keyCode, KeyEvent event) {

		//按下键盘上返回按钮
		if(keyCode == KeyEvent.KEYCODE_BACK){
			new AlertDialog.Builder(Main.this)
			// Main.this视情况而定,这个一般是指当前显示的Activity对应的XML视窗。
			.setTitle("")// 设置对话框的标题
			.setMessage(" 确定退出? ")// 设置对话框的内容
			.setPositiveButton("确定",// 设置对话框的确认按钮
			    new DialogInterface.OnClickListener() {// 设置确认按钮的事件
			        public void onClick(DialogInterface dialog, int which) {
			            //退出程序
			            android.os.Process.killProcess(android.os.Process.myPid());
			    }})
			.setNegativeButton("取消",// 设置对话框的取消按钮
			    new DialogInterface.OnClickListener() {// 设置取消按钮的事件
			        public void onClick(DialogInterface dialog, int which) {
			            // 如果你什么操作都不做,可以选择不写入任何代码
			            dialog.cancel();
			    }}
			).show();

			return true;
		}else{		
			return super.onKeyDown(keyCode, event);
		}
	}
2010九月22

WM windows mobile 6.1 C#网络开发,程序自动升级等

终于的终于,还是用C#开发了,仿照了一些M8上软件界面。。

分享一些经验,由于时间太紧,有些功能是比较土的方法暂时实现的,之后还需升级。

1、网络传输

 public static string Login(string userName, string password)
        {
            string LOGIN_RES = "";
            try
            {

                string url = com.LOGIN_URL ;//url
                url = url + "&username=" + userName;
                url = url + "&password=" + password;

                HttpWebRequest objRequest = (HttpWebRequest)WebRequest.Create(url);
                objRequest.Method = "GET";
                objRequest.Timeout = 60 * 1000;
                //WebProxy proxy = new WebProxy("192.168.0.2:80", true);
                // proxy.Address = new Uri("");//按配置文件创建Proxy 地置
                //proxy.Credentials = new NetworkCredential("用户名", "密码");//从配置封装参数中创建
                //objRequest.Proxy = proxy; 

                HttpWebResponse objResponse = (HttpWebResponse)objRequest.GetResponse();

                Stream objStream = objResponse.GetResponseStream();
                StreamReader objReader = new StreamReader(objStream, Encoding.GetEncoding(com.PageEnCode));
                LOGIN_RES=objReader.ReadToEnd();
                if (LOGIN_RES != null) LOGIN_RES = LOGIN_RES.Trim();
                objReader.Close();
                objStream.Close();

                return LOGIN_RES;

            }
            catch (Exception ex){
                Console.Write(ex.Message);
                return null;

            }

        }

2、程序升级

利用1里面的方法,读取服务器某网页文件,获取版本号和当前程序版本进行比较,若有最新版,

则在WM里打开浏览器进行下载:

private void checkUpdate()
        {
            int k = comHTTP.AutoUpdate();
            if (k > com.COPYRIGHT)
            {
                MessageBox.Show("\r\n系统监测到新版本,程序将自动打开下载,请安装后继续使用.\r\n\r\n", "提示");
                System.Diagnostics.Process.Start("IEXPLORE.EXE", "/autoupdate.cab");//打开IE,
                Application.Exit();

            }
        }
2010九月17

Java ME/J2ME 环境搭建、WM6.1运行JBED虚拟机手机实现JSR179定位

接触Java ME,到现在已经过去五天了。利用一款带GPS模块的WM6.1系统的手机实现定位和数据上报项目,周期为18天,公司没有一个人搞过手机开发,囧,身为研发部负责人责无旁贷(目前是光杆司令)……

废话不多讲,刚开始用MyEclispe + MyEclispeMe + WTK + JDK 搭建了开发环境(这也是费了九牛二虎之力)

一、Java ME/J2ME环境的搭建

1、安装 MyEclipse

MyEclipse_6.5.1GA_E3.3.2_Installer.exe、myEclipse6.5汉化包.rar、MyEclipse注册码.txt

2、安装 MyEclispeMe 开发插件

运行MyEclispe –>帮助–>Software Updates–>Find and Install –>搜索新部件

新建站点:http://eclipseme.org/updates/ (可能要翻翻哦),确定,全部勾选,安装,完毕。

3、安装 JDK

JDK6.0.21版本,如果找不到就先安装 jdk-6u20-windows-i586.exe 再打21补丁 JavaSetup6u21.exe。

4、安装 JavaME SDK

sun_java_me_sdk-3_0-win.exe

5、安装 WTK

WTK2.5.2_01版本,sun_java_wireless_toolkit-2.5.2_01-win.exe

MyEclispe–>首选项–>J2ME–>设备管理–>选择WTK目录–>refresh

(以上这些软件你就百度吧,不行就google,再不行必应)

注意:MyEclipse 编译的JAR包,很可能在虚拟机上安装不了。提示什么907啊,30的错误。

解决办法就是打开,JAR,编辑META-INF文件夹下的 MANIFEST.MF 文件,增加一行:

MIDlet-1: Test,,demo.LocationMIDlet  名字为Test,入口类为demo.LocationMIDlet

有的环境可能需要JAD包,相应的里面也要加上这一行。

二、支持JSR179的虚拟机

WM6.1上安装4EsmertecJbed_v20090217.5.1a_Chs.cab,狗狗搜,搜不到就找我要吧。

三、测试代码

package demo;

import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.List;
import javax.microedition.location.Coordinates;
import javax.microedition.location.Criteria;
import javax.microedition.location.Location;
import javax.microedition.location.LocationException;
import javax.microedition.location.LocationProvider;
//import javax.microedition.location.QualifiedCoordinates;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;

public class LocationMIDlet extends MIDlet implements CommandListener, Runnable
{
    private Display myDisplay;
    private Command okcmd;
    private Command ecmd;
    private Form form;
    private List myList;
    private boolean yes;

    public LocationMIDlet()
    {
        myDisplay = Display.getDisplay(this);
        myList = new List("测试真机是否支持 JSR179", List.IMPLICIT);
        okcmd = new Command("测试", Command.OK, 1);
        ecmd = new Command("退出", Command.OK, 1);
        form = new Form("GPS");
        String cellid=System.getProperty("CellID");
        if(cellid==null||"".equals(cellid)){
        	cellid=System.getProperty("Cell-ID");
        }
        if(cellid==null||"".equals(cellid)){
        	cellid=System.getProperty("CELLID");
        }
        if(cellid==null||"".equals(cellid)){
        	cellid=System.getProperty("CELL-ID");
        }
        form.append("CellID:"+cellid);
        form.append("JSR:"+System.getProperty("Version"));
        myList.addCommand(okcmd);
        form.addCommand(ecmd);
        form.setCommandListener(this);
        myList.setCommandListener(this);
    }
    protected void destroyApp(boolean arg0)
            throws MIDletStateChangeException {}
    protected void pauseApp(){}
    protected void startApp() throws MIDletStateChangeException
    {
        myDisplay.setCurrent(myList);
    }
    public void commandAction(Command arg0, Displayable arg1)
    {
    	if(arg0 == okcmd)
        {
            String version = System.getProperty("microedition.location.version");
            yes = (version != null && !version.equals(""));
            Thread t = new Thread(this);
            t.start();
        }
    	if(arg0 == ecmd)
        {
           this.notifyDestroyed();
        }
    }
    public void run()
    {
    	myDisplay.setCurrent(form);
        //测试真机是否支持 jsr179
        if(yes)
        {
            // Set criteria for selecting a location provider:
            // accurate to 500 meters horizontally
            // 设置精度
            Criteria myCriteria = new Criteria();
            myCriteria.setHorizontalAccuracy(500);

            double lat = 0;
            double lon = 0;

            // Get an instance of the provider
            // 找卫星,找服务
            try
            {
                LocationProvider myLocationProvider = LocationProvider.getInstance(myCriteria);

                // Request the location, setting a one-minute timeout
                // 请求位置,并设置超时时间
                Location myLocation = myLocationProvider.getLocation(5000);
                Thread.sleep(1000);
                Coordinates myCoordinates = myLocation.getQualifiedCoordinates();
                for(int i=0;i<10;i++){
                if(myCoordinates != null)
                {
                    // Use coordinate information
                    // 得到经纬度
                    lat = myCoordinates.getLatitude();
                    lon = myCoordinates.getLongitude();
                }

                form.append("真机支持JSR179,纬度坐标:" + String.valueOf(lat) + ",经度坐标:" + String.valueOf(lon));

                form.append("------------"+i);

                try{
                    Thread.sleep(1000);
                    }catch(Exception e){

                    }
                }
            }
            catch (LocationException e)
            {
            	form.append("LocationException 发生异常");
                e.printStackTrace();
            } catch (InterruptedException e)
            {
            	form.append("InterruptedException 发生异常");
                e.printStackTrace();
            }

        }
        //真机不支持 jsr179
        else
        {
        	form.append("真机不支持JSR179");

        }

    }

}

四、遗留问题

WM6.1手机系统运行JBED虚拟机上的Java ME软件,通过JSR179获取经纬度已实现。
(先运行WM上的GPS软件,后执行虚拟机里的测试代码)
两个问题:
1、室内等信号弱的地方无法定位;
2、JSR179是否需要WM上的程序进行GPS初始化待验证。
初步验证失败,貌似需要WM上先初始化GPS拨号找到卫星,虚拟机里程序才能读取数据,杯具。。。
再研究研究。。。