android 手电筒 代码

package com.lhw.light;import android.app.Activity;import android.graphics.Color;import android.hardware.Camera;import android.hardware.Camera.Parameters;import android.os.Bundle;import android.view.KeyEvent;import android.view.Menu;import android.view.MenuItem;import android.view.View;import android.view.View.OnClickListener;import android.view.Window;import android.view.WindowManager;import android.widget.Button;import android.widget.RelativeLayout;import android.widget.Toast;public class LightActivity extends Activity {private Button onebutton, bt2, bt3, bt4, bt5 = null;private Camera camera = null;private Parameters parameters = null;public static boolean kaiguan = true, flag = true;; // 定义�?��状�?,状态为false,打�?��态,状�?为true,关闭状�? //public static boolean action = false;// //定义的状态,状�?为false,当前界面不�?��,状态为true,当前界面�?�?private int back = 0;// 判断按几次backRelativeLayout layout;private static final int MINIMUM_BACKLIGHT = 30;// �?��亮度private static final int MAXIMUM_BACKLIGHT = 255;WindowManager.LayoutParams lp;@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);// 全屏设置,隐藏窗口所有装�?getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);requestWindowFeature(Window.FEATURE_NO_TITLE); // 设置屏幕显示无标题,必须启动就要设置好,否则不能再次被设�?getWindow().setFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD,WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);getWindow().setFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON,WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);setContentView(R.layout.main);onebutton = (Button) findViewById(R.id.onebutton);onebutton.setOnClickListener(new Mybutton());lp = getWindow().getAttributes();bt2 = (Button) findViewById(R.id.twobutton);bt3 = (Button) findViewById(R.id.threebutton);bt4 = (Button) findViewById(R.id.fourbutton);bt5 = (Button) findViewById(R.id.fivebutton);layout = (RelativeLayout) findViewById(R.id.relativelayout);bt2.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {// TODO Auto-generated method stubif (flag) {lp.screenBrightness = MAXIMUM_BACKLIGHT;getWindow().setAttributes(lp);layout.setBackgroundColor(Color.WHITE);flag = false;bt2.setBackgroundResource(R.drawable.bg1);} else {bt2.setBackgroundResource(R.drawable.bg);lp.screenBrightness = MINIMUM_BACKLIGHT;getWindow().setAttributes(lp);layout.setBackgroundColor(Color.BLACK);flag = true;}}});bt3.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {// TODO Auto-generated method stubif (flag) {lp.screenBrightness = MAXIMUM_BACKLIGHT;getWindow().setAttributes(lp);layout.setBackgroundColor(Color.GREEN);bt3.setBackgroundResource(R.drawable.greenbc1);flag = false;} else {lp.screenBrightness = MINIMUM_BACKLIGHT;getWindow().setAttributes(lp);layout.setBackgroundColor(Color.BLACK);bt3.setBackgroundResource(R.drawable.greenbc);flag = true;}}});bt5.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {// TODO Auto-generated method stubif (flag) {lp.screenBrightness = MAXIMUM_BACKLIGHT;getWindow().setAttributes(lp);layout.setBackgroundColor(Color.parseColor("#9900CC"));bt5.setBackgroundResource(R.drawable.purplebc);flag = false;} else {lp.screenBrightness = MINIMUM_BACKLIGHT;getWindow().setAttributes(lp);layout.setBackgroundColor(Color.BLACK);bt5.setBackgroundResource(R.drawable.purplebc2);flag = true;}}});bt4.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {// TODO Auto-generated method stubif (flag) {lp.screenBrightness = MAXIMUM_BACKLIGHT;getWindow().setAttributes(lp);layout.setBackgroundColor(Color.YELLOW);bt4.setBackgroundResource(R.drawable.yellowbc);flag = false;} else {lp.screenBrightness = MINIMUM_BACKLIGHT;getWindow().setAttributes(lp);layout.setBackgroundColor(Color.BLACK);bt4.setBackgroundResource(R.drawable.yellowbc1);flag = true;}}});} class Mybutton implements OnClickListener {@Overridepublic void onClick(View v) {if (kaiguan) {onebutton.setBackgroundResource(R.drawable.bg1);camera = Camera.open();parameters = camera.getParameters();parameters.setFlashMode(Parameters.FLASH_MODE_TORCH);// �?��camera.setParameters(parameters);// onebutton.setText("关闭");kaiguan = false;} else {onebutton.setBackgroundResource(R.drawable.bg);parameters.setFlashMode(Parameters.FLASH_MODE_OFF);// 关闭camera.setParameters(parameters);// onebutton.setText("�?��");kaiguan = true;camera.release();}}}@Overrideprotected void onDestroy() {// 停止推�?接口super.onDestroy();}@Overridepublic boolean onCreateOptionsMenu(Menu menu) {menu.add(0, 2, 2, "�?��");return super.onCreateOptionsMenu(menu);}@Overridepublic boolean onOptionsItemSelected(MenuItem item) {switch (item.getItemId()) {case 2:Myback();break;}return super.onOptionsItemSelected(item);}@Overridepublic boolean onKeyDown(int keyCode, KeyEvent event) {if (keyCode == KeyEvent.KEYCODE_BACK) {back++;switch (back) {case 1:Toast.makeText(LightActivity.this, "再按�?���?��", Toast.LENGTH_SHORT).show();break;case 2:back = 0;// 初始化back�?Myback();break;}return true;// 设置成false让back失效 ,true表示 不失�?} else {return super.onKeyDown(keyCode, event);}}public void Myback() { // 关闭程序if (kaiguan) {// �?��关闭�?LightActivity.this.finish();android.os.Process.killProcess(android.os.Process.myPid());// 关闭进程} else if (!kaiguan) {// �?��打开�?camera.release();LightActivity.this.finish();android.os.Process.killProcess(android.os.Process.myPid());// 关闭进程kaiguan = true;// 避免,打�?��关后�?��程序,再次进入不打开�?��直接�?��时,程序错误}}}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值