sencha touch2 kryonet socket phonegap 通信 作者:围城
我不太喜欢写文章,具体请看代码吧。
转载请注明。。。。
http://www.cnblogs.com/solq/archive/2012/04/21/2462208.html
http://files.cnblogs.com/solq/sencha-touch2-kryonet-socket-phonegap-%E9%80%9A%E4%BF%A1-(solq).rar
配置 phonegap 使用的是 phonegap 1.4版
1.引用 phonegap .jar 包,
2.复制解压的 xml到 \res\xml\
3.配置 AndroidManifest.xml
<supports-screens android:largeScreens="true" android:normalScreens="true" android:smallScreens="true" android:resizeable="true" android:anyDensity="true" /> <uses-permission android:name="android.permission.CAMERA" /> <uses-permission android:name="android.permission.VIBRATE" /> <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> <uses-permission android:name="android.permission.ACCESS_LOCATION_EXTRA_COMMANDS" /> <uses-permission android:name="android.permission.READ_PHONE_STATE" /> <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.RECEIVE_SMS" /> <uses-permission android:name="android.permission.RECORD_AUDIO" /> <uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" /> <uses-permission android:name="android.permission.READ_CONTACTS" /> <uses-permission android:name="android.permission.WRITE_CONTACTS" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> 在activity 标签里加上 android:configChanges="orientation|keyboardHidden"
1.java 扩展 phonegap 类
/* * sencha touch2 kryonet socket phonegap 通信 * 作者:围城(solq) * blog:http://www.cnblogs.com/solq/ * * */ package org.solq.plugins; import org.json.JSONArray; import org.solq.deom.MyClient; import org.solq.deom.Network.ChatMessage; import android.util.Log; import com.phonegap.api.Plugin; import com.phonegap.api.PluginResult; public class SendMsg extends Plugin { @Override public PluginResult execute(String action, JSONArray jSONArray, String callbackId) { try { String t="hello"; if(action.equals("sendMsg")) //发送消息 { //do code } ChatMessage request=new ChatMessage(); request.text=jSONArray.getJSONObject(0).getString("text"); //MyClient.getInstantiation().getClient().sendTCP(request); t="发送数据成功!!!"; Log.v("action++++++++++", action); Log.v("jSONArray++++++++++", jSONArray.toString()); return new PluginResult(PluginResult.Status.OK, request.text); } catch (Exception e) { return new PluginResult(PluginResult.Status.ERROR,"error"); } } }
2.xml 注册扩展类
\res\xml\plugins.xml
<plugin name="SendMsg" value="org.solq.plugins.SendMsg"/>
3.js 注册 java 类
/* * sencha touch2 kryonet socket phonegap 通信 * 作者:围城(solq) * blog:http://www.cnblogs.com/solq/ * */ var AndroidAPI=function(){}; AndroidAPI.prototype.send = function(params, success, fail){ return PhoneGap.exec( function(args){ success(args); }, function(args){ fail(args); }, 'SendMsg', //java类 'sendMsg', //action [params] //params ); }; PhoneGap.addConstructor(function() { PhoneGap.addPlugin('AndroidAPI', new AndroidAPI()); });
4. js call java
//调用 java 类 window.plugins.AndroidAPI.send({text:text},function(r){ //alert("发送消息成功:" + r); },function(e){ });
5.java call js // 用 phonegap 方式
注意:用 handler 解决跨线程访问
appView.loadUrl("javascript:alert('" + xxx + "')");
6.忘了贴上main
/* * sencha touch2 kryonet socket phonegap 通信 * 作者:围城(solq) * blog:http://www.cnblogs.com/solq/ * */ package org.solq.deom; import org.solq.deom.Network.ChatMessage; import org.solq.deom.Network.RegisterName; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.util.Log; import com.esotericsoftware.kryonet.Client; import com.esotericsoftware.kryonet.Connection; import com.esotericsoftware.kryonet.Listener; import com.phonegap.DroidGap; public class TestPhonegapPluginActivity extends DroidGap { public static Handler handler ; Client client; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); super.loadUrl("file:///android_asset/chatTenchaTouch/index.html"); appView.setFocusable(true); appView.setFocusableInTouchMode(true); handler = new Handler() { @Override public void handleMessage(Message msg) { super.handleMessage(msg); switch (msg.what) { case 0: appView.loadUrl("javascript:callJs('" + msg.obj + "')"); break; default: break; } } }; MyClient myClient=MyClient.getInstantiation(); client = myClient.getClient(); } @Override public void onDestroy() { client.close(); client.stop(); Log.v("onDestroy", "onDestroy"); android.os.Process.killProcess(android.os.Process.myPid()); super.onDestroy(); //finish(); //强制在系统服务里 force stop } }
程序退出了,系统里还挂着。当再运行程序时,,,会出错。解决方法:
1.当 onDestroy 时,不关闭 socket ,另做一个 退出处理
2.当 onDestroy 时,关闭 sokcet ,关闭程序进程 android.os.Process.killProcess(android.os.Process.myPid());
@Override public void onDestroy() { client.close(); client.stop(); Log.v("onDestroy", "onDestroy"); android.os.Process.killProcess(android.os.Process.myPid()); super.onDestroy(); }