当前位置导航:炫浪网>>网络学院>>编程开发>>JAVA教程>>J2ME

J2ME:手机归属地查询软件

最近在网上下载了一个"猫头鹰"手机归属地查询软件,界面太繁杂,看着很不爽,于是自己也做了一个.速度也有明显的改进:

原版的软件一次读取整个文件,我这个一次只读取一组,比较完后再读取!占用内存相对较少!!!!!!

在我的Nokia5300上测试通过

import java.io.*;
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.midlet.MIDlet;
import java.util.Vector;

public class PhoneNumber extends MIDlet implements CommandListener
{
private Display display;
private TextBox tb;
private String ok="";
private String[][] tel= { { "10", "北京", "北京" }, { "20", "广东", "广州" }, { "21", "上海", "上海" }, { "22", "天津", "天津" }, { "23", "重庆", "涪陵" }, { "23", "重庆", "黔江" }, { "23", "重庆", "万州" }, { "23", "重庆", "巫山" }, { "23", "重庆", "重庆" }, { "24", "辽宁", "沈阳" }}; //区号,省,市!只写了一部分,太长了...

Command commit;
Command about;
public PhoneNumber(){
display=Display.getDisplay(this);
}
public void startApp(){
commit=new Command("查询",Command.SCREEN,1);
about=new Command("关于",Command.SCREEN,1);
tb=new TextBox("请在下面输入要查询的手机号:","1382728",7,TextField.NUMERIC);
tb.addCommand(commit);
tb.addCommand(about);
tb.setCommandListener(this);
display.setCurrent(tb);
}
public void pauseApp(){}
public void destroyApp(boolean abc){}
public void commandAction(Command c,Displayable s){
if(c==commit){
//查询
if (this.tb.getString().length() == 7)
query();
else
MsgBox("请输入手机号的前7位数!","提示");
}
if(c==about){
Alert ab=new Alert("关于...");
ab.setType(AlertType.INFO);
ab.setTimeout(Alert.FOREVER);
ab.setString("手机查询软件\r\n(C)2008 石磊\r\nQQ:262754413");
display.setCurrent(ab);
return;
}
}
//执行查询
void query()
{
String dq = "";
int ext = Integer.parseInt(this.tb.getString().substring(3)); //取电话号码后面的数字
int bef = Integer.parseInt(this.tb.getString().substring(0, 3)); //前三
int qx = 0; //区号
String type="UNKNOW"; //类型
//获取号码类型
if ((bef == 130) || (bef == 131) || (bef == 132) || (bef == 153) || (bef == 156))
type = "中国联通GSM卡";
else if(bef == 133)
type = "中国联通CDMA卡";
else
type = "中国移动GSM卡";
//取得数据文件路径
//try{
String dataFile = "/phone" + bef + ".txt"; //数据文件地址
InputStream is = getClass().getResourceAsStream(dataFile); //输入流
InputStreamReader r = new InputStreamReader(is); //流读取器
int ct=0;
String nums = "";
while (true) {
//一次读取一位
int chr = 0;
try{
chr = r.read(); //一次读取一Byte
}
catch (IOException ioe) {
MsgBox(ioe.toString(),"Read异常");
return;
}
if (chr == -1) break; //如果没有可用数据则跳出
//读取到一排就比较
if (chr == 124)
{
String[] t = split(nums, ",");
if ((ext >= Integer.parseInt(t[0])) && (ext <= Integer.parseInt(t[1])))
{
qx = Integer.parseInt(t[2]);
break;
}
nums = "";
}
else
{
nums = nums + (char)chr;
}
}

try{
r.close();
is.close();
}catch(IOException ioe){}


//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

if (qx == 0) {
MsgBox("未知号码!","错误");
return;
}
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
for (int i = 0; i < tel.length; i++) {
if (Integer.parseInt(tel[i][0]) == qx) {
dq = tel[i][1] + tel[i][2];
break;
}
}
//补0
type = "号码:" + tb.getString() + "\r\n" + type + "\r\n"+dq+"(";
if ((qx < 100)||(qx < 1000)) type += "0";
type = type +qx+ ")";
//}
//catch(IOException err){
// MsgBox(err.toString(), "错误");
// return;
//}
//显示结果
MsgBox(type, "查询结果");
return;

}
//消息框
void MsgBox(String msg,String title) {
Alert ab = new Alert(title);
ab.setType(AlertType.CONFIRMATION);
ab.setTimeout(Alert.FOREVER);
ab.setString(msg);
display.setCurrent(ab);

}
//字符分割
public static String[] split(String original,String regex) {
int startIndex = 0;
Vector v = new Vector();
String[] str = null;
int index = 0;
startIndex = original.indexOf(regex);
while(startIndex < original.length() && startIndex != -1)
{
String temp = original.substring(index,startIndex);
v.addElement(temp);
index = startIndex + regex.length();
startIndex = original.indexOf(regex,startIndex + regex.length());
}
v.addElement(original.substring(index + 1 - regex.length()));
str = new String[v.size()];
for(int i=0;i<v.size();i++)
str[i] =(String)v.elementAt(i);
return str;
}
}

相关内容
赞助商链接