收藏 分销(赏)

lssjzmn安卓手电筒源码.doc

上传人:仙人****88 文档编号:11767411 上传时间:2025-08-12 格式:DOC 页数:13 大小:99KB 下载积分:10 金币
下载 相关 举报
lssjzmn安卓手电筒源码.doc_第1页
第1页 / 共13页
lssjzmn安卓手电筒源码.doc_第2页
第2页 / 共13页


点击查看更多>>
资源描述
注意: 1. 不带图标资源,可以自己画一个 2. 编译平台为Eclipse 最低安卓版本为v4.0 3. 工程源码下载地址: 第一部分:java源码(共两个.java) MainActivity.java package com.lssjzmn.zm_flash; import java.util.Timer; import java.util.TimerTask; import android.graphics.Color; import android.hardware.Camera; import android.hardware.Camera.Parameters; import android.hardware.Sensor; import android.hardware.SensorEvent; import android.hardware.SensorEventListener; import android.hardware.SensorManager; import android.os.Bundle; import android.os.Vibrator; import android.view.KeyEvent; import android.view.View; import android.view.Window; import android.widget.Button; import android.widget.ImageButton; import android.widget.ImageView; import android.widget.RelativeLayout; import android.widget.TextView; import android.widget.Toast; import android.app.Activity; import android.app.AlertDialog; import android.app.Service; import android.content.DialogInterface; import android.content.Intent; import android.content.IntentFilter; import android.content.pm.FeatureInfo; import android.content.pm.PackageManager; public class MainActivity extends Activity implements SensorEventListener { private Camera mCamera; private Parameters param; private ImageView mImageViewbg; private TextView mTextView1; private TextView mTextView2; private TextView mTextView3; private SensorManager mSensorManager = null; private Sensor mSensor = null; private Timer mTimer; private TimerTask mTimerTask; private Vibrator mVibrator; private AlertDialog mdialog; private static Boolean isFlashOn = false; private Boolean isFalshExists = false; private Boolean isSosOn = false; private Boolean isTimerOff = false; private Boolean isVibratorable = false; private int lux, mi = 0; private int remain = 2, counter = 2; private long mExitTime = 0; private long mFlashPeriod = 600; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.activity_main); HomeKeyExit homeKeyExitReceiver = new HomeKeyExit(); // 生成注册广播接收 registerReceiver(homeKeyExitReceiver, new IntentFilter( Intent.ACTION_CLOSE_SYSTEM_DIALOGS)); checkflash(); mCamera = Camera.open(); param = mCamera.getParameters(); mVibrator = (Vibrator) getApplication().getSystemService( Service.VIBRATOR_SERVICE); mSensorManager = (SensorManager) getApplication().getSystemService( Service.SENSOR_SERVICE); mSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_LIGHT); mSensorManager.registerListener(this, mSensor, SensorManager.SENSOR_DELAY_GAME); // 创建对话框 AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setTitle("选择闪烁速度"); // 创建选项数组 String[] options = { "快", "中(默认)", "慢" }; builder.setItems(options, dialogListener); // 设置选项 builder.setNegativeButton("确定", null); mdialog = builder.create(); // 创建对话框 mTextView1 = (TextView) findViewById(R.id.textView); mTextView2 = (TextView) findViewById(R.id.speeddisp); mTextView2.setText("当前闪烁速度:默认"); mTextView3 = (TextView) findViewById(R.id.isvibratoring); mTextView3.setText("非振动模式"); mImageViewbg = (ImageView) findViewById(R.id.backgrd); ImageButton btnon = (ImageButton) findViewById(R.id.on); btnon.setOnClickListener( // 匿名内层类 new View.OnClickListener() { @Override public void onClick(View v) { if (isSosOn == true) { mTimer.cancel(); isSosOn = false; isTimerOff = true; } flashOn(mCamera, param); } }); ImageButton btnoff = (ImageButton) findViewById(R.id.off); btnoff.setOnClickListener( // 匿名内层类 new View.OnClickListener() { @Override public void onClick(View v) { if (isSosOn == true) { mTimer.cancel(); isSosOn = false; isTimerOff = true; } if (isFlashOn == true) { flashOff(mCamera, param); } else return; } }); ImageButton btnvib = (ImageButton) findViewById(R.id.vibrator); btnvib.setOnClickListener( // 匿名内层类 new View.OnClickListener() { @Override public void onClick(View v) { remain = counter % 2; if (remain == 1) { isVibratorable = false; counter += 1; mTextView3.setText("非振动模式"); } else { isVibratorable = true; counter += 1; mTextView3.setText("光敏感振动模式"); } } }); mTimer = new Timer(); mTimerTask = new sosTimerTask(); ImageButton btnsos = (ImageButton) findViewById(R.id.bntsos); btnsos.setOnClickListener( // 匿名内层类 new View.OnClickListener() { @Override public void onClick(View v) { if (isTimerOff == true) { mTimer = new Timer(); mTimerTask = new sosTimerTask(); isTimerOff = false; } if (isSosOn == false) { isSosOn = true; isFlashOn = true; try { flashOff(mCamera, param); mTimer.schedule(mTimerTask, 0, mFlashPeriod); switch ((int) mFlashPeriod) { case 300: mTextView2.setText("当前闪烁速度:快"); break; case 600: mTextView2.setText("当前闪烁速度:默认"); break; case 1000: mTextView2.setText("当前闪烁速度:慢"); break; } } catch (Exception e) { Toast.makeText(MainActivity.this, "闪烁未成功!", Toast.LENGTH_LONG).show(); } } } }); } // 对话框选项的监听对象 DialogInterface.OnClickListener dialogListener = new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int speed) { switch (speed) { case 0: mFlashPeriod = 500;// 快 mTextView2.setText("当前闪烁速度:快"); break; case 1: mFlashPeriod = 700;// 中(默认) mTextView2.setText("当前闪烁速度:默认"); break; case 2: mFlashPeriod = 1000;// 慢 mTextView2.setText("当前闪烁速度:慢"); break; } } }; public void aboutApp() { AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setTitle("关于手电筒") .setMessage("名称:ZM_Flash\n作者:lssjzmn\n时间:2014/4/4") .setPositiveButton("我知道了", new DialogInterface.OnClickListener() { public void onClick( DialogInterface dialoginterface, int i) { // Do nothing } }).show(); } public void checkflash() { PackageManager pm = this.getPackageManager(); FeatureInfo[] features = pm.getSystemAvailableFeatures(); for (FeatureInfo f : features) { if (PackageManager.FEATURE_CAMERA_FLASH.equals(f.name)) { isFalshExists = true; return; } } if (!isFalshExists == true) { AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setTitle("提醒") .setMessage("没有找到可用的闪光灯设备\n确认并退出程序") .setPositiveButton("确认", new DialogInterface.OnClickListener() { public void onClick( DialogInterface dialoginterface, int i) { finish(); System.exit(0); } }).show(); } else { return; } } public void flashOff(Camera mCamera, Parameters param) { mImageViewbg.setBackground(getResources().getDrawable( R.drawable.flashoff)); param.setFlashMode(Parameters.FLASH_MODE_OFF); mCamera.setParameters(param); mCamera.stopPreview(); isFlashOn = false; } public void flashOn(Camera mCamera, Parameters param) { mImageViewbg.setBackground(getResources().getDrawable( R.drawable.flashon)); param.setFlashMode(Parameters.FLASH_MODE_TORCH);// 只用FLASH_MODE_TORCH // mImageViewbg.setBackgroundColor(Color.parseColor("#333333")); mCamera.setParameters(param); mCamera.startPreview(); isFlashOn = true; } @Override public boolean onKeyDown(int keyCode, KeyEvent event) { switch (keyCode) { case KeyEvent.KEYCODE_BACK: if ((System.currentTimeMillis() - mExitTime) > 2000) { Toast.makeText(this, "再按一次退出手电筒", Toast.LENGTH_SHORT).show(); mExitTime = System.currentTimeMillis(); } else { finish(); System.exit(0); } return true; default: break; } return super.onKeyDown(keyCode, event); } @Override protected void onDestroy() { super.onDestroy(); if (mCamera != null) { mCamera.release(); mCamera = null; } mTimer.cancel(); } @Override public void onAccuracyChanged(Sensor arg0, int arg1) { } @Override public void onSensorChanged(SensorEvent event) { lux = (int) event.values[0]; mTextView1.setText("光照度Lux= " + lux + "/10000"); if (isVibratorable == true) vibrator(); } public void vibrator() { if (lux <= 20) mVibrator.vibrate(new long[] { 300, 500 }, -1); } public void speed(View v) { if (isFlashOn) flashOff(mCamera, param); mdialog.show(); } public void exit(View v) { finish(); System.exit(0); } public void about(View v) { aboutApp(); } class sosTimerTask extends TimerTask { @Override public void run() { MainActivity.this.runOnUiThread(new Runnable() { @Override public void run() { flashOn(mCamera, param); for (int i = 0; i < 1000; i++) {// useless mi += i; } flashOff(mCamera, param); } }); } } } HomeKeyExit.java package com.lssjzmn.zm_flash; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; class HomeKeyExit extends BroadcastReceiver { static final String SYSTEM_REASON = "reason"; static final String SYSTEM_HOME_KEY = "homekey";//home key static final String SYSTEM_RECENT_APPS = "recentapps";//long home key @Override public void onReceive(Context context, Intent intent) { String action = intent.getAction(); if (action.equals(Intent.ACTION_CLOSE_SYSTEM_DIALOGS)) { String reason = intent.getStringExtra(SYSTEM_REASON); if (reason != null) { if (reason.equals(SYSTEM_HOME_KEY)) { // home key System.exit(0); } else if (reason.equals(SYSTEM_RECENT_APPS)) { // long home key } } } } } 第二部分:界面 activity_main.xml文件 <RelativeLayout xmlns:android=" xmlns:tools=" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity" android:background="#000000" android:id="@+id/about" android:onClick="about"> <ImageView android:id="@+id/backgrd" android:layout_width="600dp" android:layout_height="fill_parent" android:layout_alignParentBottom="true" android:layout_alignParentLeft="true" android:layout_alignParentRight="true" /> <ImageButton android:id="@+id/on" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_alignParentLeft="true" android:layout_marginBottom="62dp" android:layout_marginLeft="42dp" android:src="@drawable/on" style="?android:attr/borderlessButtonStyle"/> <ImageButton android:id="@+id/off" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignBottom="@+id/on" android:layout_alignParentRight="true" android:layout_marginRight="42dp" android:src="@drawable/off" style="?android:attr/borderlessButtonStyle"/> <ImageButton android:id="@+id/exit" android:layout_width="50dp" android:layout_height="50dp" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" android:src="@drawable/exit" android:onClick="exit" style="?android:attr/borderlessButtonStyle"/> <TextView android:id="@+id/textView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:textColor="#ffffff"/> <ImageButton android:id="@+id/bntsos" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignBottom="@+id/exit" android:layout_alignParentRight="true" android:src="@android:drawable/ic_lock_silent_mode_off" style="?android:attr/borderlessButtonStyle" /> <ImageButton android:id="@+id/vibrator" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignBottom="@+id/bntsos" android:layout_alignLeft="@+id/off" android:layout_marginLeft="22dp" android:onClick="vibrator" android:src="@android:drawable/ic_menu_always_landscape_portrait" style="?android:attr/borderlessButtonStyle"/> <ImageButton android:id="@+id/flashspeed" style="?android:attr/borderlessButtonStyle" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignTop="@+id/vibrator" android:layout_marginLeft="26dp" android:layout_toRightOf="@+id/on" android:onClick="speed" android:src="@android:drawable/ic_menu_manage" /> <TextView android:id="@+id/speeddisp" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignLeft="@+id/off" android:layout_alignParentBottom="true" android:textColor="#ffffff" /> <TextView android:id="@+id/isvibratoring" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignBottom="@+id/exit" android:layout_marginBottom="13dp" android:layout_marginLeft="21dp" android:layout_toRightOf="@+id/exit" android:textColor="#ffffff"/> </RelativeLayout> 第三部分:Mainfest.xml文件 <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android=" package="com.lssjzmn.zm_flash" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="16" android:targetSdkVersion="18" /> <uses-permission android:name="android.permission.CAMERA" /> <uses-permission android:name="android.permission.FLASHLIGHT" /> <uses-permission android:name="android.permission.VIBRATE"/> <uses-feature android:name="android.hardware.camera" /> <uses-feature android:name="android.hardware.camera.autofocus" /> <uses-feature android:name="an
展开阅读全文

开通  VIP会员、SVIP会员  优惠大
下载10份以上建议开通VIP会员
下载20份以上建议开通SVIP会员


开通VIP      成为共赢上传

当前位置:首页 > 包罗万象 > 大杂烩

移动网页_全站_页脚广告1

关于我们      便捷服务       自信AI       AI导航        抽奖活动

©2010-2026 宁波自信网络信息技术有限公司  版权所有

客服电话:0574-28810668  投诉电话:18658249818

gongan.png浙公网安备33021202000488号   

icp.png浙ICP备2021020529号-1  |  浙B2-20240490  

关注我们 :微信公众号    抖音    微博    LOFTER 

客服