OPHONE联网详解
在移动游戏基地调试了2天,终于明白了OPHONE联网是咋回事了,不敢独享,贴出来给后来的朋友们。
先从OPHONE的联网机制开始说起。
OPHONE联网,具体分两步, 第一步是启动联网,就跟普通手机启动GPRS一样,只是OPHONE改成了程序可以控制启动和关闭,GPRS则是系统根据程序需求自动连接的。
进入OPHONE系统,设置-》数据连接, 就能看到一个选择框,“开机时连接”, 如果这个已经钩上,则表明,第一步系统已经给你做了,只是默认连接时CMNET, 我测试的MOTO MT710是这样的。 如果这个钩没有钩上,则需要你的联网应用在程序内启动数据连接。 具体的代码如下:
启动数据连接 注意下面的参数 networkmode, 这个就是你在OPHONE数据连接设置里看到的,比如internet, cms,wap,等等,你自己建的也可以,根据名字来就行
public boolean ActiveNetWorkByMode(String networkmode) {
try
{
Context context = getApplicationContext();
ConnectivityManager connectivity = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
connectivity.startUsingNetworkFeature(ConnectivityManager.TYPE_MOBILE, networkmode);
return isNetworkCMWAPAvailable();
}
catch(Exception e)
{
}
return false;
}
//关闭数据连接
public boolean DestroyNetWorkByMode(String networkmode) {
try
{
Context context = getApplicationContext();
ConnectivityManager connectivity = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
connectivity.stopUsingNetworkFeature(ConnectivityManager.TYPE_MOBILE, networkmode);
return isNetworkCMWAPAvailable();
}
catch(Exception e)
{
}
return false;
}
//判断数据连接是否激活,如果激活则进一步判断是不是CMWAP连接,我的联网只用CMWAP,你可以根据自己的需要具体去改写。
public boolean isNetworkCMWAPAvailable() {
Context context = getApplicationContext();
ConnectivityManager connectivity = (ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE);
//System.out.println("NETWORK active connectivity ");
if (connectivity != null) {
NetworkInfo[] info = connectivity.getAllNetworkInfo();
if (info != null) {
//System.out.println("NETWORK active info is well ");
for (int i = 0; i < info.length; i++) {
String typeName = info.getTypeName();
String extraInfo = info.getExtraInfo();
//System.out.println("NETWORK active info state " + info.getState() + " " + typeName + " " + extraInfo);
if (info.getState() == NetworkInfo.State.CONNECTED) {
//System.out.println("NETWORK CONNECTED " + extraInfo);
String subType = info.getSubtypeName();
if ( extraInfo!=null && extraInfo.contains("cmwap") ) {
//System.out.println("NETWORK CONNECTED SHUTDOWN " + extraInfo);
//connectivity.stopUsingNetworkFeature(ConnectivityManager.TYPE_MOBILE, "wap");
return true;
}
}
}
}
}
return false;
}
以上就是第一步,先判断数据连接是否激活,没有激活则代码激活之。另外,激活的过程大概需要3到5秒,所以激活调用后,立即去判断是否激活,返回的结果肯定是FALSE,如果等个10再去调用判断,则会显示成功,所以建议激活不要和联网放在一起,我一般是启动游戏,激活,关闭游戏时同时关掉激活。
第二步,就是联网,
联网方式最好不要用传统的联网,我一开始就是传统的,比如。
CMNET
private int CountMoney(String urlstr)
{
String strHead = "";
try{
URL url = new URL(urlstr);
URLConnection conn = url.openConnection();
String headerstr = conn.getHeaderField("Content-Type");
if (headerstr!= null && headerstr.startsWith("text")) {
conn = url.openConnection();
}
InputStream inputstream = conn.getInputStream();
ByteArrayOutputStream bytearrayoutputstream = new ByteArrayOutputStream();
byte abyte1[] = new byte[1024];
for(int k = 0; -1 != (k = inputstream.read(abyte1));)
bytearrayoutputstream.write(abyte1, 0, k);
strHead = new String(bytearrayoutputstream.toByteArray(), "UTF-8");
System.out.println("cmnet " + strHead);
}
catch (Exception e) {
s_netmodewap = !s_netmodewap;
return 2;
}
if(strHead.indexOf("status=1301") > -1 || strHead.indexOf("status=1300") > -1)
{
return 1;
}
else
{
return 0;
}
}
CMWAP,代理用得很糙,呵呵。
private int CountMoneyCMWAP(String urlstr)
{
String strHead = "";
try{
URL url = new URL(urlstr);
Proxy proxy=new Proxy(java.net.Proxy.Type.HTTP,new InetSocketAddress("10.0.0.172",80));
HttpURLConnection connection=(HttpURLConnection)
url.openConnection(proxy);
String headerstr = connection.getHeaderField("Content-Type");
if (headerstr!= null && headerstr.startsWith("text")) {
connection=(HttpURLConnection)
url.openConnection(proxy);
}
InputStream inputstream = connection.getInputStream();
ByteArrayOutputStream bytearrayoutputstream = new ByteArrayOutputStream();
byte abyte1[] = new byte[1024];
for(int k = 0; -1 != (k = inputstream.read(abyte1));)
bytearrayoutputstream.write(abyte1, 0, k);
strHead = new String(bytearrayoutputstream.toByteArray(), "UTF-8");
System.out.println("cmwap " + strHead);
}
catch (Exception e) {
e.printStackTrace();
return 2;
}
if(strHead.indexOf("status=1301") > -1 || strHead.indexOf("status=1300") > -1)
{
return 1;
}
else
{
return 0;
}
}
以上都是传统的联网思维,所以连起来速度会很慢。后来经过移动OPHONE的人指点,终于弄明白了。如下,速度会快很多。
private int CountMoneyCMWAPNEWWAY(String urlstr)
{
String strHead = "";
try{
if(!GameLet._self.isNetworkCMWAPAvailable())
{
GameLet._self.ActiveNetWorkByMode("wap");
return 2;
}
int splashIndex = urlstr.indexOf("/", 7);
String hosturl = urlstr.substring(7, splashIndex);
String hostfile = urlstr.substring(splashIndex);
System.out.println("hosturl " + hosturl);
System.out.println("hostfile " + hostfile);
HttpHost proxy = new HttpHost( "10.0.0.172", 80, "http");
HttpHost target = new HttpHost(hosturl, 80, "http");
DefaultHttpClient httpclient = new DefaultHttpClient();
httpclient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy);
HttpGet req = new HttpGet(hostfile);
HttpResponse rsp = httpclient.execute(target, req);
HttpEntity entity = rsp.getEntity();
InputStream inputstream = entity.getContent();
ByteArrayOutputStream bytearrayoutputstream = new ByteArrayOutputStream();
byte abyte1[] = new byte[1024];
for(int k = 0; -1 != (k = inputstream.read(abyte1));)
bytearrayoutputstream.write(abyte1, 0, k);
strHead = new String(bytearrayoutputstream.toByteArray(), "UTF-8");
httpclient.getConnectionManager().shutdown();
}
catch (Exception e) {
return 2;
}
if(strHead.indexOf("status=1301") > -1 || strHead.indexOf("status=1300") > -1)
{
return 1;
}
else
{
return 0;
}
}
最后,有几个PERRMISSION别忘了加了。
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" />
就写到这里,祝大家成功。
补充一下:如果想准确的接收连接建立的通知事件,还需要监听一个消息
android.net.conn.CONNECTIVITY_CHANGE
然后在Receiver程序中做个判断
NetworkInfo ni = intent.getParcelableExtra(ConnectivityManager.EXTRA_NETWORK_INFO);
if (ni != null && ni.isConnected()&& NetAccess.NETWORK_AP.equals(Utils.getAPType(ni))) {......}
这样就可以了。
http://www.ophonesdn.com/forum/thread-2831-1-1.html
---------------------------------------------------------
专注移动开发
Android, Windows Mobile, iPhone, J2ME, BlackBerry, Symbian
posted on 2010-09-14 14:56
TiGERTiAN 阅读(424)
评论(0) 编辑 收藏 所属分类:
Android