android vs. javascript 建國科技大學資管系 饒瑞佶 2013/5. javascript call java by...

13
Android vs. JavaScript 建建建建建建建建建 建建建 2013/5

Upload: norma-knight

Post on 18-Jan-2016

233 views

Category:

Documents


0 download

TRANSCRIPT

Android vs. JavaScript

建國科技大學資管系饒瑞佶2013/5

JavaScript call Java

• By JavascriptInterface

• 將 native code寫在 JavascriptInterface內,例如取得方位的程式

• 透過WebView來串接 JavascriptInterface 與JavaScript

• JavaScript端透過 window.interface名稱 .函數來呼叫程式

建立 class JsInterface

呼叫 class JsInterface

建立 index.html<html><head> <title>PhoneGap</title> <meta charset="utf-8" /> <meta name="viewport" content="width=device-width, initial-scale=1"/> <script src="jquery-1.9.1.min.js"></script> <link rel="stylesheet" href="jquery.mobile-1.3.0.min.css"> <script src="jquery.mobile-1.3.0.min.js"></script> <script> $(document).ready(function(){ $('#calljava').click(function(){ window.myjsinterface.getSomeString(); }); $('#sethtml').click(function(){ $('#showval').html(window.myjsinterface.setValFromJava()); }); }); </script></head><body> <div data-role="page"> <div data-role="header"> <h1>Android JAVA vs. Javascript</h1> </div> <div data-role="content"> <input type="button" id="calljava" value="call JAVA"> <input type="button" id="sethtml" value="Set HTML"> <div id="showval"></div> </div> <div data-role="footer"> <h1>2013/5/20</h1> </div> </div></body></html>

製作硬體呼叫 jar 檔

public class compassjar { private SensorManager sm=null; // Sensor Manager private Sensor aSensor;//加速度計 private Sensor mSensor;//磁方位 float[] accelerometerValues=new float[3]; //加速度計值 float[] magneticFieldValues=new float[3];//磁方位值 float[] rotate=new float[16]; //原始值 float[] outrotate=new float[16]; float[] values=new float[3];

private compassjar.Callback cb=null; // constructor public compassjar(Context context, compassjar.Callback cb) { this.cb=cb; //返回呼叫 sm = (SensorManager)context.getSystemService(Context.SENSOR_SERVICE); aSensor=sm.getDefaultSensor(Sensor.TYPE_ACCELEROMETER); mSensor=sm.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD); sm.registerListener(myListener, aSensor, SensorManager.SENSOR_DELAY_UI); sm.registerListener(myListener, mSensor, SensorManager.SENSOR_DELAY_UI); }

// sensor 關閉public void close() { sm.unregisterListener(myListener);} private void outxyz(float x,float y,float z) { if (cb!=null) { cb.getxyz(x,y,z); }}

public interface Callback { void getxyz(float x,float y,float z);}

private SensorEventListener myListener=new SensorEventListener() { public void onSensorChanged(SensorEvent e) { if(e.sensor.getType()==Sensor.TYPE_ACCELEROMETER){ accelerometerValues=e.values; } if(e.sensor.getType()==Sensor.TYPE_MAGNETIC_FIELD){ magneticFieldValues=e.values; } // 取得方位角 SensorManager.getRotationMatrix(rotate, null, accelerometerValues, magneticFieldValues); SensorManager.remapCoordinateSystem(rotate,SensorManager.AXIS_Z ,SensorManager.AXIS_MINUS_X, outrotate); SensorManager.getOrientation(outrotate, values); // 轉換成度 180~-180 values[0]=(float)Math.toDegrees(values[0]); //x values[1]=(float)Math.toDegrees(values[1]); //y values[2]=(float)Math.toDegrees(values[2]); //z // 輸出 outxyz(values[0],values[1],values[2]); }

public void onAccuracyChanged(Sensor sensor, int accuracy) { // unused }};}

輸出 jar 檔

輸出 jar 檔

使用 jar 檔首先匯入 jar檔