Android 手电筒源码

最近由于公司需要,做了一个手电筒,其实手电筒原理很简单,就是调用照相机的闪光灯,控制闪光灯的开关,就可以实现手电筒的效果,

强调一下,代码中一定要注意在结束的时候对闪光灯进行释放,否则就会导致使用照相机的时候出现——无法连接到相机 这个问题

手电筒APK下载地址:点击打开下载链接

手电筒项目源码下载:点击打开下载链接

主要代码如下:

package com.techainsh.flashlight;

import java.util.List;
import android.hardware.Camera;
import android.hardware.Camera.Parameters;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.os.PowerManager;
import android.os.Vibrator;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.Toast;

public class MainActivity extends Activity implements OnClickListener {

	public ImageView btnimageView = null;
	public RelativeLayout bgmageView = null;
	private Camera camera;
	private boolean isOpen = true;
	public final static int OPEN_CAMERA = 1011;
	public final static int OPEN_LIGHT = 1012;
	public final static int CLOSE_LIGHT = 1013;
	private Vibrator vibrator;
	long[] pattern = { 100, 200 };
	PowerManager.WakeLock wakeLock;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		handler.sendEmptyMessage(OPEN_CAMERA);
		vibrator = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
		btnimageView = (ImageView) findViewById(R.id.btn_image);
		bgmageView = (RelativeLayout) findViewById(R.id.bg_image);
		bgmageView.setBackgroundResource(R.drawable.bg_flashlight_off);
		btnimageView.setImageResource(R.drawable.btn_flash_light_off);
		btnimageView.setOnClickListener(this);
		bgmageView.setBackgroundResource(R.drawable.bg_flashlight_on);
		btnimageView.setImageResource(R.drawable.btn_flash_light_on);
		handler.sendEmptyMessage(OPEN_LIGHT);
		vibrator.vibrate(pattern, -1);

	}

	Handler handler = new Handler(Looper.getMainLooper()) {
		@Override
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case OPEN_CAMERA:
				camera = Camera.open();
				break;
			case OPEN_LIGHT:
				Parameters params = camera.getParameters();
				List list = params.getSupportedFlashModes();
				if (list.contains(Parameters.FLASH_MODE_TORCH)) {
					params.setFlashMode(Parameters.FLASH_MODE_TORCH);
				} else {
					Toast.makeText(getApplicationContext(), "此设备不支持闪光灯模式",
							Toast.LENGTH_SHORT).show();
				}
				camera.setParameters(params);
				camera.startPreview();
				isOpen = true;
				break;
			case CLOSE_LIGHT:
				if (isOpen) {
					Parameters closepParameters = camera.getParameters();
					closepParameters
							.setFlashMode(Camera.Parameters.FLASH_MODE_OFF);
					camera.setParameters(closepParameters);
					camera.stopPreview();
					// camera.release();
					isOpen = false;
					// sendEmptyMessage(OPEN_CAMERA);
				}
				break;
			default:
				break;
			}
		}
	};

	@Override
	protected void onDestroy() {
		super.onDestroy();
		if (null != camera) {
			camera.release();
			camera = null;
		}
	}

	public void onStop() {
		super.onStop();
		vibrator.cancel();
		if (null != camera) {
			camera.release();
			camera = null;
		}

		MainActivity.this.finish();
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			QuitAlert();
		}

		return super.onKeyDown(keyCode, event);
	}

	public void QuitAlert() {
		new AlertDialog.Builder(MainActivity.this).setTitle("关闭手电筒提示")
				.setIcon(R.drawable.ic_launcher).setMessage("确定要关闭手电筒么?")
				.setPositiveButton("确定", new DialogInterface.OnClickListener() {

					public void onClick(DialogInterface dialog, int which) {
						MainActivity.this.finish();

					}
				})
				.setNegativeButton("取消", new DialogInterface.OnClickListener() {

					@Override
					public void onClick(DialogInterface arg0, int arg1) {
						// TODO Auto-generated method stub

					}
				}).create().show();
	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		PowerManager pm = (PowerManager) getSystemService(POWER_SERVICE);
		wakeLock = pm.newWakeLock(PowerManager.SCREEN_BRIGHT_WAKE_LOCK
				| PowerManager.ON_AFTER_RELEASE, "test");
		wakeLock.acquire();
	}

	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
		if (wakeLock != null)
			wakeLock.release();
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.btn_image:
			if (isOpen) {
				bgmageView.setBackgroundResource(R.drawable.bg_flashlight_off);
				btnimageView.setImageResource(R.drawable.btn_flash_light_off);
				handler.sendEmptyMessage(CLOSE_LIGHT);
				vibrator.vibrate(pattern, -1);
			} else {
				bgmageView.setBackgroundResource(R.drawable.bg_flashlight_on);
				btnimageView.setImageResource(R.drawable.btn_flash_light_on);
				handler.sendEmptyMessage(OPEN_LIGHT);
				vibrator.vibrate(pattern, -1);
			}
			break;
		default:
			break;
		}

	}

}


需要用到的权限如下:

 
    
    
    
       
     
     

界面布局如下:



    

        
    


实现的效果如下:

Android 手电筒源码_第1张图片Android 手电筒源码_第2张图片


你可能感兴趣的:(android)