Android 打开关闭闪光灯(里程碑2.1)


不同的手机,开启闪光灯的方法不一样,这里以摩托罗拉里程碑的手机为例

main.xml:
[html] view plain copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="fill_parent"  
  4.     android:layout_height="fill_parent"  
  5.     android:orientation="vertical" >  
  6.     <Button  
  7.         android:id="@+id/open"  
  8.         android:layout_width="fill_parent"  
  9.         android:layout_height="wrap_content"  
  10.         android:text="@string/open" />  
  11.     <Button  
  12.         android:id="@+id/close"  
  13.         android:layout_width="fill_parent"  
  14.         android:layout_height="wrap_content"  
  15.         android:text="@string/close" />  
  16. </LinearLayout>  

Activity代码:
[java] view plain copy
  1. package com.android.flashlight;  
  2.   
  3. import android.app.Activity;  
  4. import android.os.Bundle;  
  5. import android.view.View;  
  6. import android.widget.Button;  
  7.   
  8. public class AndroidFlashLightActivity extends Activity {  
  9.     /** Called when the activity is first created. */  
  10.       
  11.     private Button mBtnOpen,mBtnClose;  
  12.     private MyFlashLight myFlashLight;  
  13.       
  14.     @Override  
  15.     public void onCreate(Bundle savedInstanceState) {  
  16.         super.onCreate(savedInstanceState);  
  17.         setContentView(R.layout.main);  
  18.           
  19.         mBtnOpen = (Button) findViewById(R.id.open);  
  20.         mBtnClose = (Button) findViewById(R.id.close);  
  21.           
  22.         try {  
  23.             myFlashLight = new MyFlashLight();  
  24.         } catch (Exception e) {  
  25.             e.printStackTrace();  
  26.         }  
  27.           
  28.         mBtnOpen.setOnClickListener(new Button.OnClickListener(){  
  29.   
  30.             @Override  
  31.             public void onClick(View v) {  
  32.                 if(myFlashLight.isEnabled() == false){  
  33.                     myFlashLight.enable(true);  
  34.                 }  
  35.             }  
  36.         });  
  37.           
  38.         mBtnClose.setOnClickListener(new Button.OnClickListener(){  
  39.   
  40.             @Override  
  41.             public void onClick(View v) {  
  42.                 if(myFlashLight.isEnabled() == true){  
  43.                     myFlashLight.enable(false);  
  44.                 }  
  45.             }  
  46.         });  
  47.     }  
  48. }  

封装的方法:
[java] view plain copy
  1. package com.android.flashlight;    
  2.     
  3. import java.lang.reflect.Method;    
  4. import android.os.IBinder;    
  5.     
  6. public class MyFlashLight {    
  7.     private Object svc = null;    
  8.     private Method getFlashlightEnabled = null;    
  9.     private Method setFlashlightEnabled = null;    
  10.       
  11.     @SuppressWarnings("unchecked")    
  12.     public MyFlashLight() throws Exception{    
  13.         try {    
  14.             // call ServiceManager.getService("hardware") to get an IBinder for the service.    
  15.             // this appears to be totally undocumented and not exposed in the SDK whatsoever.    
  16.             Class sm = Class.forName("android.os.ServiceManager");    
  17.             Object hwBinder = sm.getMethod("getService", String.class).invoke(null"hardware");    
  18.               
  19.             // get the hardware service stub. this seems to just get us one step closer to the proxy    
  20.             Class hwsstub = Class.forName("android.os.IHardwareService$Stub");    
  21.             Method asInterface = hwsstub.getMethod("asInterface", android.os.IBinder.class);    
  22.             svc = asInterface.invoke(null, (IBinder) hwBinder);    
  23.               
  24.             // grab the class (android.os.IHardwareService$Stub$Proxy) so we can reflect on its methods    
  25.             Class proxy = svc.getClass();    
  26.               
  27.             // save methods    
  28.             getFlashlightEnabled = proxy.getMethod("getFlashlightEnabled");    
  29.             setFlashlightEnabled = proxy.getMethod("setFlashlightEnabled"boolean.class);    
  30.         }    
  31.         catch(Exception e) {    
  32.             throw new Exception("LED could not be initialized");    
  33.         }    
  34.     }    
  35.       
  36.     public boolean isEnabled() {    
  37.         try {    
  38.             return getFlashlightEnabled.invoke(svc).equals(true);    
  39.         }    
  40.         catch(Exception e) {    
  41.             return false;    
  42.         }    
  43.     }    
  44.       
  45.     public void enable(boolean tf) {    
  46.         try {    
  47.             setFlashlightEnabled.invoke(svc, tf);    
  48.         }    
  49.         catch(Exception e) {}    
  50.     }    
  51. }  

你可能感兴趣的:(java,android,layout,null,button,encoding)