‘编程学习’ 分类下的所有文章
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十一月18

MyCity 我的城市街拍,新浪微博街拍Android客户端 1.0发布

MyCity —我的城市街拍 1.0

是Android上的一个新浪微博的客户端,目的是实现手机上街拍发布图片和地理位置信息到微博的功能。
现阶段主要实现以下功能:
1. 通过MyCity拍照,可以把图片发布到微博
2. 通过MyCity获得GPS地理位置,分享到微博(通过google地图展示)
3. 用户可以附加100字以内的说明文字

下载地址: /MyCity.apk

使用说明:
按MENU键或第一次按发布键绑定微博帐号,进行拍照后附加文字发布微博,若在室外空旷处运行获得GPS地理位置信息后程序会自动在微博内容中添加google地图链接。

更新日志:
1、2010-11-18 9:00 修改位置信息未添加到微博中的bug,囧,我的错。

应用效果

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十一月12

mysql : Lock wait timeout exceeded; try restarting transaction

使用的InnoDB   表类型的时候,
默认参数:innodb_lock_wait_timeout设置锁等待的时间是50s,
因为有的锁等待超过了这个时间,所以报错.
可以把这个时间加长,或者优化存储过程,事务避免过长时间的等待.

my.ini文件:
#innodb_lock_wait_timeout = 50
改成
innodb_lock_wait_timeout = 500

 重启mysql服务。

2010九月29

C# Windows Mobile 实现休眠状态下定时任务

OpenNETCF.WindowsCE.LargeIntervalTimer llTimer = new OpenNETCF.WindowsCE.LargeIntervalTimer(); 

//第一次触发时间

llTimer.FirstEventTime = DateTime.Now;

llTimer.Interval = new TimeSpan(0, 30, 0);

//是否只触发一次

 llTimer.OneShot = false;

llTimer.Tick += new EventHandler(llTimer_Tick);

llTimer.Enabled = true;

static void llTimer_Tick(object sender, EventArgs e)

 {

             //

  }

 

来源:http://www.cnblogs.com/fox23/archive/2008/02/03/AtTime.html

opennetcf下载:http://www.opennetcf.com/Products/SmartDeviceFramework/tabid/65/Default.aspx

2010九月28

C# Windows Mobile 6 通过注册表实现开机自运行

方法1:制作CAB安装包

VS2005,新建项目–其他项目类型–安装和部署–智能Cab安装项目–在Program files里增加文件夹myexe

添加要可执行 a.exe。

打开注册表视图,在HKEY_LOCAL_MACHINE 增加 Init 文件夹,新建字符串 Launch98,值为

“\Program files\myexe\a.exe”

这样,安装cab之后重启下即可自运行。

private void Form1_Activated(object sender, EventArgs e)
        {
            this.Hide();  //隐藏窗体
        }

方法2:通过程序运行注册键值

 

  //注册开机
  //Registry.LocalMachine 对应于 HKEY_LOCAL_MACHINE主键
  RegistryKey key = Registry.LocalMachine.OpenSubKey("init", true);
  if (key.GetValue("Launch77") == null)
  {
       key.SetValue("Launch77", Assembly.GetExecutingAssembly().GetName().CodeBase);
       MessageBox.Show("注册成功!", "提示");
   }
   key.Close();
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九月21

windows mobile模拟器如何联网?

windows mobile开发的时候模拟器如何联网?

1. 安装Active sync,到微软网站上面去找找,有免费下载。

2. 打开Active sync连接选项,里面有下拉选框的那个(允许连接到以下其中一个端口),选择DMA

3. 在VS2005里面启动模拟器成功(要知道自己连的是哪个)之后,Tools菜单里面有个”Device Emulator Manager”,找到刚刚连上的模拟器(前面会有个小图标,其他没启动就没有),右键点击,选择”Cradle” 这时候Active sync跳出来说找到一个device,不理他,等待emulator里面跳出个小对话框,告诉你连接成功(很快就自己消失),OK,这时候就可以上网了。

注意:Active Sync会吃掉所有的UDP包,所以用这种方式开发UDP客户端行不通了。

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拨号找到卫星,虚拟机里程序才能读取数据,杯具。。。
再研究研究。。。