lssjzmn安卓手電筒源碼_第1頁(yè)
lssjzmn安卓手電筒源碼_第2頁(yè)
lssjzmn安卓手電筒源碼_第3頁(yè)
lssjzmn安卓手電筒源碼_第4頁(yè)
lssjzmn安卓手電筒源碼_第5頁(yè)
已閱讀5頁(yè),還剩8頁(yè)未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說(shuō)明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡(jiǎn)介

1、注意:1. 不帶圖標(biāo)資源,可以自己畫一個(gè)2. 編譯平臺(tái)為Eclipse 最低安卓版本為v4.03. 工程源碼下載地址:第一部分:java源碼(共兩個(gè).java)MainActivity.javapackage 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.hardwa

2、re.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 and

3、roid.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.

4、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 mTextVie

5、w1;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 isFal

6、shExists = 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;Overrideprotected void onCreate(Bundle savedInstanceState)

7、 super.onCreate(savedInstanceState);requestWindowFeature(Window.FEATURE_NO_TITLE);setContentView(R.layout.activity_main);HomeKeyExit homeKeyExitReceiver = new HomeKeyExit(); / 生成注冊(cè)廣播接收registerReceiver(homeKeyExitReceiver, new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);checkflash();mCamera = Ca

8、mera.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.regist

9、erListener(this, mSensor,SensorManager.SENSOR_DELAY_GAME);/ 創(chuàng)建對(duì)話框AlertDialog.Builder builder = new AlertDialog.Builder(this);builder.setTitle("選擇閃爍速度");/ 創(chuàng)建選項(xiàng)數(shù)組String options = "快", "中(默認(rèn))", "慢" ;builder.setItems(options, dialogListener); / 設(shè)置選項(xiàng)builder.setNega

10、tiveButton("確定", null);mdialog = builder.create(); / 創(chuàng)建對(duì)話框mTextView1 = (TextView) findViewById(R.id.textView);mTextView2 = (TextView) findViewById(R.id.speeddisp);mTextView2.setText("當(dāng)前閃爍速度:默認(rèn)");mTextView3 = (TextView) findViewById(R.id.isvibratoring);mTextView3.setText("非振動(dòng)

11、模式");mImageViewbg = (ImageView) findViewById(R.id.backgrd);ImageButton btnon = (ImageButton) findViewById(R.id.on);btnon.setOnClickListener( / 匿名內(nèi)層類new View.OnClickListener() Overridepublic void onClick(View v) if (isSosOn = true) mTimer.cancel();isSosOn = false;isTimerOff = true;flashOn(mCamer

12、a, param););ImageButton btnoff = (ImageButton) findViewById(R.id.off);btnoff.setOnClickListener( / 匿名內(nèi)層類new View.OnClickListener() Overridepublic void onClick(View v) if (isSosOn = true) mTimer.cancel();isSosOn = false;isTimerOff = true;if (isFlashOn = true) flashOff(mCamera, param); elsereturn;);Im

13、ageButton btnvib = (ImageButton) findViewById(R.id.vibrator);btnvib.setOnClickListener( / 匿名內(nèi)層類new View.OnClickListener() Overridepublic void onClick(View v) remain = counter % 2;if (remain = 1) isVibratorable = false;counter += 1;mTextView3.setText("非振動(dòng)模式"); else isVibratorable = true;cou

14、nter += 1;mTextView3.setText("光敏感振動(dòng)模式"););mTimer = new Timer();mTimerTask = new sosTimerTask();ImageButton btnsos = (ImageButton) findViewById(R.id.bntsos);btnsos.setOnClickListener( / 匿名內(nèi)層類new View.OnClickListener() Overridepublic void onClick(View v) if (isTimerOff = true) mTimer = new T

15、imer();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("當(dāng)前閃爍速度:快");break;case 600:mTextView2.setText(&quo

16、t;當(dāng)前閃爍速度:默認(rèn)");break;case 1000:mTextView2.setText("當(dāng)前閃爍速度:慢");break; catch (Exception e) Toast.makeText(MainActivity.this, "閃爍未成功!",Toast.LENGTH_LONG).show(););/ 對(duì)話框選項(xiàng)的監(jiān)聽對(duì)象DialogInterface.OnClickListener dialogListener = new DialogInterface.OnClickListener() public void onCli

17、ck(DialogInterface dialog, int speed) switch (speed) case 0:mFlashPeriod = 500;/ 快mTextView2.setText("當(dāng)前閃爍速度:快");break;case 1:mFlashPeriod = 700;/ 中(默認(rèn))mTextView2.setText("當(dāng)前閃爍速度:默認(rèn)");break;case 2:mFlashPeriod = 1000;/ 慢mTextView2.setText("當(dāng)前閃爍速度:慢");break;public void a

18、boutApp() AlertDialog.Builder builder = new AlertDialog.Builder(this);builder.setTitle("關(guān)于手電筒").setMessage("名稱:ZM_Flashn作者:lssjzmnn時(shí)間:2014/4/4").setPositiveButton("我知道了",new DialogInterface.OnClickListener() public void onClick(DialogInterface dialoginterface, int i) /

19、Do nothing).show();public void checkflash() PackageManager pm = this.getPackageManager();FeatureInfo features = pm.getSystemAvailableFeatures();for (FeatureInfo f : features) if (PackageManager.FEATURE_CAMERA_FLASH.equals() isFalshExists = true;return;if (!isFalshExists = true) AlertDialog.Bui

20、lder builder = new AlertDialog.Builder(this);builder.setTitle("提醒").setMessage("沒(méi)有找到可用的閃光燈設(shè)備n確認(rèn)并退出程序").setPositiveButton("確認(rèn)",new DialogInterface.OnClickListener() public void onClick(DialogInterface dialoginterface, int i) finish();System.exit(0);).show(); else return;

21、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) m

22、ImageViewbg.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;Overridepublic boo

23、lean 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;defau

24、lt:break;return super.onKeyDown(keyCode, event);Overrideprotected void onDestroy() super.onDestroy();if (mCamera != null) mCamera.release();mCamera = null;mTimer.cancel();Overridepublic void onAccuracyChanged(Sensor arg0, int arg1) Overridepublic void onSensorChanged(SensorEvent event) lux = (int) e

25、vent.values0;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(Vie

26、w v) finish();System.exit(0);public void about(View v) aboutApp();class sosTimerTask extends TimerTask Overridepublic void run() MainActivity.this.runOnUiThread(new Runnable() Overridepublic void run() flashOn(mCamera, param);for (int i = 0; i < 1000; i+) / uselessmi += i;flashOff(mCamera, param)

27、;);HomeKeyExit.javapackage 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

28、"/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);

29、 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=&quo

30、t;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&q

31、uot; 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" and

32、roid:layout_alignParentBottom="true" android:layout_alignParentLeft="true" android:layout_marginBottom="62dp" android:layout_marginLeft="42dp" android:src="drawable/on" style="?android:attr/borderlessButtonStyle"/> <ImageButton androi

33、d: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=

34、"?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/

35、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" andro

36、id: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="andr

37、oid: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" an

38、droid: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/flashs

39、peed" 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" androi

40、d: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_alignPa

41、rentBottom="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&

溫馨提示

  • 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。

評(píng)論

0/150

提交評(píng)論