android资料(闪光灯手电筒)

Android------调用闪光灯当手电筒

拷贝到 在src目录下建立 android.os 包 并新建IHardwareService.aidl 文件

android资料(闪光灯手电筒)_第1张图片

IHardwareService.aidl 文件

package android.os;  
  
/** {@hide} */  
interface IHardwareService  
{  
    // obsolete flashlight support  
    boolean getFlashlightEnabled();  
    void setFlashlightEnabled(boolean on);  
}

Activity

package com.yf210yf.flashlight;

import java.lang.reflect.Method;

import android.app.Activity;
import android.os.Bundle;
import android.os.IBinder;
import android.os.IHardwareService;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;

public class FlashLightAvtivity extends Activity
{
	private ImageButton imageButton;  
	private int i = 0;  
	@Override
	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		imageButton = (ImageButton) findViewById(R.id.imageButton);  
		 setFlashlightEnabled(false);
		 imageButton.setOnClickListener(new OnClickListener()  
	     {            			 
			@Override
			public void onClick(View v)
			{
				if (i == 0)  
	             {  
					 imageButton.setImageResource(R.drawable.light2);  
					 setFlashlightEnabled(true);
	                 i = 1;  
	             } else  
	             {  
	            	 imageButton.setImageResource(R.drawable.light1);  
	            	 setFlashlightEnabled(false);
	                 i = 0;  
	             }				
			}    
	      });  
		 
		 
	}
	
	 /** 
     * 设置闪光灯的开启和关闭 
     */  
    private void setFlashlightEnabled(boolean isEnable)  
    {  
        try  
        {  
            Method method = Class.forName("android.os.ServiceManager").getMethod("getService", String.class);  
            IBinder binder = (IBinder) method.invoke(null, new Object[] { "hardware" });  
              
            IHardwareService localhardwareservice = IHardwareService.Stub.asInterface(binder);  
            localhardwareservice.setFlashlightEnabled(isEnable);  
        }  
        catch (Exception e)  
        {  
            e.printStackTrace();  
        }  
          
    }  
    
    public boolean onKeyDown(int kCode,KeyEvent kEvent) 
    { 
	    switch(kCode) 
	    { 
		    case KeyEvent.KEYCODE_BACK: 
		    	 int pid = android.os.Process.myPid() ;
	                android.os.Process.killProcess(pid) ;
		    return true; 		 
	    } 
	    return super.onKeyDown(kCode,kEvent); 
    } 
 
}

转载请注明出处:CSDN奔跑的蜗牛

你可能感兴趣的:(android资料(闪光灯手电筒))