先说一下思路,程序启动的时候先启动一个检测apn的类CheckApnForm,CheckApnForm可以做出动画或者简单的字样提示例如“检测网 络中请稍等。。。”,同时启动两个线程job,一个通过设置代理去连接服务器,一个直连服务器,将CheckApnForm的引用传给两个线程,当线程检 测成功或者失败,都要返回结果给CheckApnForm,然后通过最终的状态判断手机的网络设置时cmwap还是cmnet,然后程序中再需要连接网络 的地方就都要根据当前网络状态,选择是否设置代理。以下是代码:
(备注:可能高手看来不算什么,但是这个框架我在工作中一直都用到,而且非常好用,觉得原创,首次发布到dev.chinamobile.com,如果转 载希望可以表明出处和作者,如果工作中用到了就无所谓了,随便用)
public interface CheckAPN {
public final int ERROR=0;
public final int CMWAP=1;
public final int CMNET=2;
public final int CMWAP_ERROR=3;
public final int CMNET_ERROR=4;
public final int CMWAP_SUCCESS=5;
public final int CMNET_SUCCESS=6;
public void getAPN(int apn);
}
public class CheckApnForm implements CheckAPN{
private MIDlet midlet;
private Form f = null;
private Form nextForm = null;
public static int status=ERROR;
private int threadCount=0;
private String content="";
private Label label;
private boolean hasChecked=false;
public CheckApnForm(MIDlet midlet){
this.midlet=midlet;
f = new Form();
f.setTitle("检测网络状态");
f.setLayout(new BorderLayout());
label=new Label("正在检测网络类型......");
f.addComponent(BorderLayout.CENTER,label);
f.show();
f.repaint();
try {
Thread.sleep(1000);
} catch (InterruptedException ex) {
ex.printStackTrace();
}
Thread t1=new CheckNetTread("t1",this,CMWAP);
Thread t2=new CheckNetTread("t2",this,CMNET);
t1.start();
t2.start();
}
public synchronized void getAPN(int apn) {
threadCount++;
if(apn>=5&&status<5){
status=apn;
}
if(!hasChecked){
//System.out.println(status);
if(status>=5){
hasChecked=true;
//Dialog.show("网络检测", ""+status, "确定", null);
System.gc();
LoginForm mainForm=new LoginForm(midlet);
nextForm=mainForm.getForm();
nextForm.show();
f.repaint();
return;
}else{
if(threadCount>=2){
label.setText("请退出,查看系统的网络连接配置");
content+="检测网络连接都失败!\r\n请检查系统网络连接配置\r\n";
Dialog.show("网络检测", content, "确定", null);
f.addCommand(new Command("退出",1));
f.addCommandListener(new ActionListener(){
public void actionPerformed(ActionEvent event) {
if(event.getCommand().getId()==1)
midlet.notifyDestroyed();
}
});
}
}
}
f.repaint();
}
public void msg(String msg) {
label.setText(msg);
}
}
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package com.http;
import com.gui.CheckAPN;
import java.io.DataInputStream;
import java.io.IOException;
import javax.microedition.io.Connector;
import javax.microedition.io.HttpConnection;
/**
*
* @author Administrator
*/
public class CheckNetTread extends Thread{
private int apn;
private CheckAPN apnForm;
public CheckNetTread(String name,CheckAPN apnForm,int apn){
super(name);
this.apnForm=apnForm;
this.apn=apn;
}
public void run(){
HttpConnection conn = null;
try {
//System.out.println(this.getName()+" begin open url");
if(apn==CheckAPN.CMWAP){
conn = (HttpConnection) Connector.open("http://10.0.0.172/",Connector.READ_WRITE,true);
conn.setRequestProperty("x-online-host", "wap.baidu.com");
}else if(apn==CheckAPN.CMNET){
conn = (HttpConnection) Connector.open("http://www.baidu.com/",Connector.READ_WRITE,true);
}
DataInputStream dis=conn.openDataInputStream();
//System.out.println(this.getName()+" open url over");
} catch (IOException ex) {
//System.out.println(this.getName()+" IOException,return");
if(apn==CheckAPN.CMWAP)
apnForm.getAPN(CheckAPN.CMWAP_ERROR);
else if(apn==CheckAPN.CMNET)
apnForm.getAPN(CheckAPN.CMNET_ERROR);
return;
}
if(apn==CheckAPN.CMWAP)
apnForm.getAPN(CheckAPN.CMWAP_SUCCESS);
else if(apn==CheckAPN.CMNET)
apnForm.getAPN(CheckAPN.CMNET_SUCCESS);
}
}
一个网络apn的自动检测框架
最新推荐文章于 2021-05-09 19:38:49 发布