1、注意: 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.Camer
2、a.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
3、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.
4、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
5、 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;
6、 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 i
7、nt 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); H
8、omeKeyExit homeKeyExitReceiver = new HomeKeyExit(); // 生成注册广播接收 registerReceiver(homeKeyExitReceiver, new IntentFilter( Intent.ACTION_CLOSE_SYSTEM_DIALOGS)); checkflash(); mCamera = Camera.open(); param = mCamera.getParameters(); mVibrator = (Vibrator) getApplication().getSystemS
9、ervice( Service.VIBRATOR_SERVICE); mSensorManager = (SensorManager) getApplication().getSystemService( Service.SENSOR_SERVICE); mSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_LIGHT); mSensorManager.registerListener(this, mSensor, SensorManager.SENSOR_DELAY_GAME);
10、// 创建对话框 AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setTitle("选择闪烁速度"); // 创建选项数组 String[] options = { "快", "中(默认)", "慢" }; builder.setItems(options, dialogListener); // 设置选项 builder.setNegativeButton("确定", null); mdialog = builder.create(); // 创建对话框
11、 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
12、); 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(
13、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; isTimer
14、Off = 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(Vi
15、ew v) { remain = counter % 2; if (remain == 1) { isVibratorable = false; counter += 1; mTextView3.setText("非振动模式"); } else { isVibratorable = true; counter += 1; mTextView3.setText("光敏感振动模式"); } } }); mTimer = new Timer(); mTimer
16、Task = 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 sosTime
17、rTask(); 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("当前闪
18、烁速度:快"); break; case 600: mTextView2.setText("当前闪烁速度:默认"); break; case 1000: mTextView2.setText("当前闪烁速度:慢"); break; } } catch (Exception e) { Toast.makeText(MainActivity.this, "闪烁未成功!", Toast.LENGTH_LONG).show();
19、 } } } }); } // 对话框选项的监听对象 DialogInterface.OnClickListener dialogListener = new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int speed) { switch (speed) { case 0: mFlashPeriod = 500;// 快 mTextView2.setText("当前闪烁速度:快");
20、 break; case 1: mFlashPeriod = 700;// 中(默认) mTextView2.setText("当前闪烁速度:默认"); break; case 2: mFlashPeriod = 1000;// 慢 mTextView2.setText("当前闪烁速度:慢"); break; } } }; public void aboutApp() { AlertDialog.Builder builder = new AlertDialog.Builder(th
21、is); 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()
22、 } 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; } } i
23、f (!isFalshExists == true) { AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setTitle("提醒") .setMessage("没有找到可用的闪光灯设备\n确认并退出程序") .setPositiveButton("确认", new DialogInterface.OnClickListener() { public void onClick( DialogInterface
24、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.setFlashM
25、ode(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
26、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.K
27、EYCODE_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.
28、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 o
29、nSensorChanged(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
30、 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.runO
31、nUiThread(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;
32、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 St
33、ring 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.getStrin
34、gExtra(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
35、 }
}
}
}
}
第二部分:界面 activity_main.xml文件
36、"#000000"
android:id="@+id/about"
android:onClick="about">
38、ndroid:layout_marginBottom="62dp"
android:layout_marginLeft="42dp"
android:src="@drawable/on"
style="?android:attr/borderlessButtonStyle"/>
40、 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"/>
41、ew
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:textColor="#ffffff"/>
42、h="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" />
43、
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"
44、
android:src="@android:drawable/ic_menu_always_landscape_portrait"
style="?android:attr/borderlessButtonStyle"/>
45、oid: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" />
47、 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="#fff 48、fff"/>
49、minSdkVersion="16"
android:targetSdkVersion="18" />