android 手电筒

代码地址:http://download.csdn.net/detail/u011324501/9422876

简单的手电筒代码:

MainActivity.java

package com.example.flashlight;

import android.hardware.Camera;
import android.hardware.Camera.Parameters;
import android.os.Bundle;
import android.os.Handler;
import android.app.Activity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class MainActivity extends Activity {
 
	 private boolean isopent = false;
	 private Camera camera;
	 private Button button;
	 boolean is = true;
	 boolean sosflag = true;
	 Button sos;
	    @Override
	    protected void onCreate(Bundle savedInstanceState) {
	        // TODO Auto-generated method stub
	        super.onCreate(savedInstanceState);
	        View view = View.inflate(this, R.layout.main, null);
	        setContentView(view);
	        button = (Button) findViewById(R.id.main_img);
	        //手电筒开关按钮
	        button.setOnClickListener(new View.OnClickListener() {
	 
	            @Override
	            public void onClick(View v) {
	                // TODO Auto-generated method stub
	            	is=false;
	            	try{
		                if (!isopent) {
		                	on();
		                } else {
		                	off();
		                }
	            	}catch(Exception e){
	            		e.printStackTrace();
	            	}
	            }
	        });
	        //sos按钮
	        sos = (Button)findViewById(R.id.sos);
	        sos.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View arg0) {
					// TODO Auto-generated method stub									
					if(sosflag){			        	
						//开启线程
						handler.removeCallbacks(runnable);
						handler.postDelayed(runnable,1000);	
						sosflag=false;
			        }else{
			        	//关闭线程
			        	handler.removeCallbacks(runnable);
			        }							       
				}
			});
	        
	        
	    }
	   //sos线程
	    private Handler handler = new Handler();
        private Runnable runnable = new Runnable() {
             public void run () {
             on();
             delay(500);
             off();
             handler.postDelayed(this,1000); 
          }
        };   
        
        /**
         * 延时函数
         * @param ms
         */
        private void delay(int ms){  
	        try {  
	            Thread.currentThread();  
	            Thread.sleep(ms);  
	        } catch (InterruptedException e) {  
	            e.printStackTrace();  
	        }   
	     }   
	    /*
	     * 打开手电筒
	     * */
	    public void on(){
	    	button.setBackgroundResource(R.drawable.on);
            // Toast.makeText(getApplicationContext(), "您已经打开了手电筒", 0).show();
             camera = Camera.open();
             Parameters params = camera.getParameters();
             params.setFlashMode(Parameters.FLASH_MODE_TORCH);
             camera.setParameters(params);
             camera.startPreview(); // 开始亮灯
             isopent = true;
	    }
	    //关闭手电筒
	    public void off(){
	    	 button.setBackgroundResource(R.drawable.off);
            // Toast.makeText(getApplicationContext(), "关闭了手电筒", Toast.LENGTH_SHORT).show();
             camera.stopPreview(); // 关掉亮灯
             camera.release(); // 关掉照相机
             isopent = false;
	    }
}
布局文件:main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >
 
    <Button
        android:id="@+id/main_img"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:background="@drawable/off"
        >
    </Button>


    <Button
        android:id="@+id/sos"
        android:layout_width="50dip"
        android:layout_height="40dip"
        android:textSize="15sp"
        android:layout_alignBaseline="@+id/main_img"
        android:layout_alignBottom="@+id/main_img"
        android:layout_centerHorizontal="true"
        android:text="@string/sos" />
 
</RelativeLayout>

 用到的权限:

<!-- 摄像头、手电筒 -->
    <uses-permission android:name="android.permission.CAMERA" />
    <uses-permission android:name="android.permission.FLASHLIGHT" />
 
    <uses-feature android:name="android.hardware.camera" />
    <uses-feature android:name="android.hardware.camera.autofocus" />
    <uses-feature android:name="android.hardware.camera.flash" />



你可能感兴趣的:(android,线程,手电筒,Flashlight)