android應用開發全程實錄-實現甩動撥打和掛斷電話

 今天繼續給你們帶來《Android應用開發全程實錄》中的章節,這部分是講傳感器中的一個實例。android

經過上面的例子咱們學會了如何得到某種類型的傳感器,下面經過一個實例來學習如何使用某一個類型的傳感器。咱們以加速傳感器爲例,來實現這樣一個功能:搖動手機便自動撥打某親情號碼,並實現再次搖動則掛機的功能。ide

 

工程目錄:EX_12_03佈局

第一步,UI佈局main.xml的代碼以下,其運行效果如圖12-10所示。學習

 

  
  
  
  
  1. <?xml version="1.0" encoding="utf-8"?> 
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
  3.     android:orientation="vertical" 
  4.     android:layout_width="fill_parent" 
  5.     android:layout_height="fill_parent" > 
  6.     <TextView  android:layout_width="fill_parent"  
  7.     android:layout_height="wrap_content"  
  8.     android:text="當前狀態:就緒" 
  9.     android:id="@+id/state" 
  10.     android:textColor="#ff0000" /> 
  11.     <Button android:layout_width="fill_parent"  
  12.     android:layout_height="wrap_content"  
  13.     android:id="@+id/call" 
  14.     android:text="打電話(10086)"/>     
  15.     <Button android:layout_width="fill_parent"  
  16.     android:layout_height="wrap_content"  
  17.     android:id="@+id/stop" 
  18.     android:text="掛機"/>   
  19.     </LinearLayout> 

第二步,掛電話須要用到系統的Itelephony類的支持,因爲Google從Android 2.1 SDK版本後,屏蔽了com.android.internal.telephony. Itelephony類,所以,須要從Android×××,找到該類並導入到工程目錄下,記得包名一致。測試

第三步,Activity類ShakeSensorActivity的實現。該類實現SensorListener接口,添加加速度偵聽事件,經過判斷設備X、Y、Z方向的總晃動值來判斷是否啓動打電話和掛機操做。以撥打10086測試爲例,當設備總晃動值大於100做爲一個測試判斷點,若是當前沒有在通話界面,就經過Intent啓動撥打電話,不然就掛機操做。設備搖動時,啓動電話、掛機的界面狀態如圖圖12-十一、圖12-12所示。this

 

 

▲圖12-10  軟件運行效果圖spa

▲圖12-11  電話啓動界面  日誌

  

 

   圖12-12  搖動設備掛機時的狀態界面xml

下面就來看看代碼:接口

  
  
  
  
  1. public class ShakeSensorActivity extends Activity implements SensorListener { 
  2.     private float lastX; 
  3.     private float lastY; 
  4.     private float lastZ; 
  5.     private View mainView; 
  6.     private long currTime; 
  7.     private long lastTime; 
  8.     private long duration;// 持續時間  
  9.     private float currShake; 
  10.     private float totalShake; 
  11.     private ITelephony iTelephony; 
  12.     private boolean isCalling = false
  13.     SensorManager sm = null
  14.     @Override 
  15.     public void onCreate(Bundle savedInstanceState) { 
  16.         super.onCreate(savedInstanceState); 
  17.         mainView = LinearLayout.inflate(this, R.layout.main, null); 
  18.         setContentView(mainView); 
  19.         ((Button) mainView.findViewById(R.id.call)).setOnClickListener(new   
  20. OnClickListener() { 
  21.                     @Override 
  22.                     public void onClick(View v) { 
  23.                         // 打電話 
  24.                         callPhoneNumber10086(); 
  25.                     } 
  26.                 }); 
  27.         ((Button) mainView.findViewById(R.id.stop)).setOnClickListener(new OnClick-         Listener() { 
  28.                     @Override 
  29.                     public void onClick(View v) { 
  30.                         // 掛機 
  31.                         closePhone(); 
  32.                     } 
  33.                 }); 
  34.         // 獲取傳感器管理器 
  35.         sm = (SensorManager) getSystemService(Context.SENSOR_SERVICE); 
  36.         // 註冊加速度傳感器  
  37.         sm.registerListener(this,  
  38. SensorManager.SENSOR_ACCELEROMETER,SensorManager.       SENSOR_DELAY_NORMAL); 
  39.  
  40.     } 
  41.     @Override 
  42.     public void finish() { 
  43.         // TODO Auto-generated method stub 
  44.         super.finish(); 
  45.         sm.unregisterListener(this);// 註銷偵聽 
  46.  
  47.     } 
  48.     @Override 
  49.     public void onAccuracyChanged(int sensor, int accuracy) { 
  50.         // 精度改變,該方法實質上不作任何操做,它只在每次調用時,添加一個日誌項 
  51.     } 
  52.     @Override 
  53.     public void onSensorChanged(int sensor, float[] values) { 
  54.         float x = values[0]; 
  55.         float y = values[1]; 
  56.         float z = values[2]; 
  57.         currTime = System.currentTimeMillis(); 
  58.         if (lastX == 0 && lastY == 0 && lastZ == 0) { 
  59.             // 第一次shake 
  60.             lastTime = currTime; 
  61.         } 
  62.         if (currTime - lastTime > 200) {// 200毫秒檢測一次 
  63.             duration = currTime - lastTime; 
  64.             currShake = (Math.abs(x - lastX) + Math.abs(y - lastY) + Math.abs(z - lastZ))/ duration * 200
  65.         } 
  66.         totalShake = totalShake + currShake; 
  67.         if (totalShake > 100) { 
  68.             totalShake = 0;// 重置爲0,從新累計計數 
  69.             lastX = 0
  70.             lastY = 0
  71.             lastZ = 0
  72.             lastTime = 0
  73.             currTime = 0
  74.             if (!isCalling) { 
  75.                 callPhoneNumber10086(); 
  76.                 ((TextView) mainView.findViewById(R.id.state)).setText("當前狀態:               通話中..."); 
  77.             } else { 
  78.                 closePhone(); 
  79.                 ((TextView) mainView.findViewById(R.id.state)).setText("當前狀態:通話結束..."); 
  80.             } 
  81.         } 
  82.         lastX = x; 
  83.         lastY = y; 
  84.         lastZ = z; 
  85.         lastTime = currTime; 
  86.     } 
  87.  
  88.     /** 
  89.      * tell 10086打開通話界面 
  90.      */ 
  91.     private synchronized void callPhoneNumber10086() { 
  92.         isCalling = true
  93.         Intent myIntentDial = new Intent(Intent.ACTION_CALL,Uri.parse("tel:" + 10086)); 
  94.         startActivity(myIntentDial); 
  95.     } 
  96.  
  97.     /** 
  98.      * 結束通話 
  99.      */ 
  100.     private synchronized void closePhone() { 
  101.         try { 
  102.             getTelephony(); 
  103.             iTelephony.endCall(); 
  104.             isCalling = false
  105.         } catch (RemoteException e) { 
  106.             // TODO Auto-generated catch block 
  107.             e.printStackTrace(); 
  108.         } 
  109.  
  110.     } 
  111.  
  112.     /** 
  113.      * 初始電話 實例 
  114.      */ 
  115.     public void getTelephony() { 
  116.  
  117.         TelephonyManager telMgr = (TelephonyManager)  
  118. this.getSystemService(Service.      TELEPHONY_SERVICE); 
  119.         Class<TelephonyManager> c = TelephonyManager.class
  120.         Method getITelephonyMethod = null
  121.         try { 
  122.             getITelephonyMethod = c.getDeclaredMethod("getITelephony",(Class[]) null); 
  123.             getITelephonyMethod.setAccessible(true); 
  124.         } catch (SecurityException e) { 
  125.             e.printStackTrace(); 
  126.         } catch (NoSuchMethodException e) { 
  127.             e.printStackTrace(); 
  128.         } 
  129.  
  130.         try { 
  131.             iTelephony = (ITelephony)  
  132. getITelephonyMethod.invoke(telMgr,(Object[])null); 
  133.         } catch (IllegalArgumentException e) { 
  134.             e.printStackTrace(); 
  135.         } catch (IllegalAccessException e) { 
  136.             e.printStackTrace(); 
  137.         } catch (InvocationTargetException e) { 
  138.             e.printStackTrace(); 
  139.         } 
  140.  
  141.     } 

                                 

 

經過getTelephony()方法,初始一個iTelephony實例,方便調用,目前只用到了掛機關閉通話,打電話也能夠經過iTelephony.dial(「10086」)直接撥打。這樣就輕鬆實現了甩動打掛電話功能了

相關文章
相關標籤/搜索