Android手电筒后台Service

功能是后台service控制手电的打开和关闭。

public class ToggleFlashService extends Service {
	final String SMS_RECEIVED_STRING = "android.provider.Telephony.SMS_RECEIVED";
	AudioManager audioManager;
	int callState;
	int flashDuration;
	int flashInterval;
	boolean isCameraInUse = false;
	boolean isCameraOpen = false;
	boolean isFlashOn = false;
	Camera.Parameters params;
	Intent serviceIntent;
	TelephonyManager telephonyManager;
	Timer timer;
	TimerTask timerTask;

	public boolean isCameraInUse() {
		try {
			if ((ToggleFlashSettings.camera == null) && (!this.isCameraOpen)) {
				ToggleFlashSettings.camera = Camera.open();
				this.params = ToggleFlashSettings.camera.getParameters();
				this.isCameraOpen = true;
				return false;
			}			
		} catch (Exception localException) {
		}
		return true;
	}

	public void messageReceived() {
	}

	public IBinder onBind(Intent paramIntent) {
		return null;
	}

	public void onCreate() {
		this.telephonyManager = ((TelephonyManager) getSystemService("phone"));
		this.audioManager = ((AudioManager) getSystemService("audio"));
		this.callState = this.telephonyManager.getCallState();
		this.flashDuration = ToggleFlashPrefs
				.getCallFlashInterval(getApplicationContext());
		this.flashInterval = ToggleFlashPrefs
				.getCallFlashDuration(getApplicationContext());
		super.onCreate();
	}


	public void onDestroy() {
		Log.e("Toggle", "ToggleFlash Destroy");
		try {
			if (this.timer != null) {
				this.timer.cancel();
				this.timer = null;
			}
			if ((this.isFlashOn) && (ToggleFlashSettings.camera != null)) {
				this.isFlashOn = false;
				this.isCameraOpen = false;
				this.params = ToggleFlashSettings.camera.getParameters();
				List localList = this.params.getSupportedFlashModes();
				if ((localList.contains("torch"))
						|| (localList.contains("off"))
						|| (localList.contains("on"))) {
					this.params.setFlashMode("off");
					ToggleFlashSettings.camera.setParameters(this.params);
				}
				ToggleFlashSettings.camera.release();
				ToggleFlashSettings.camera = null;
			}			
		} catch (Exception localException) {
			if (ToggleFlashSettings.camera != null) {
				ToggleFlashSettings.camera.release();
				ToggleFlashSettings.camera = null;
			}
			localException.printStackTrace();
		}
	}


	public int onStartCommand(Intent paramIntent, int paramInt1, int paramInt2) {
		if (ToggleFlashSettings.camera != null) {
			ToggleFlashSettings.camera.release();
			ToggleFlashSettings.camera = null;
		}
		this.serviceIntent = paramIntent;
		this.telephonyManager.listen(new PhoneStateListener() {
			public void onCallStateChanged(int paramAnonymousInt,
					String paramAnonymousString) {
				switch (paramAnonymousInt) {
				default:
					ToggleFlashService.this.callState = 0;
					break;
				case 1:
					ToggleFlashService.this.callState = 1;
					break;
				case 2:
					ToggleFlashService.this.callState = 2;
					break;
				}

			}
		}, 32);
		this.timer = new Timer();
		this.timerTask = new TimerTask() {
			public void run() {
				ToggleFlashService.this.toggleFlash();
			}
		};
		if (this.flashDuration == 0) {
			this.timer.schedule(this.timerTask, this.flashInterval, 50L);
		} else {
			this.timer.schedule(this.timerTask, this.flashInterval,
					this.flashDuration);
		}
		return super.onStartCommand(paramIntent, paramInt1, paramInt2);
	}


	public boolean stopService(Intent paramIntent) {
		try {
			if (this.timer != null) {
				this.timer.cancel();
				this.timer = null;
			}
			if ((this.isFlashOn) && (ToggleFlashSettings.camera != null)) {
				this.isFlashOn = false;
				this.isCameraOpen = false;
				this.params = ToggleFlashSettings.camera.getParameters();
				List localList = this.params.getSupportedFlashModes();
				if ((localList.contains("torch"))
						|| (localList.contains("off"))
						|| (localList.contains("on"))) {
					this.params.setFlashMode("off");
					ToggleFlashSettings.camera.setParameters(this.params);
				}
				ToggleFlashSettings.camera.release();
				ToggleFlashSettings.camera = null;
			}
			this.isCameraOpen = false;
			this.isFlashOn = false;
			if (ToggleFlashSettings.camera != null) {
				ToggleFlashSettings.camera.release();
				ToggleFlashSettings.camera = null;
			}
			if (this.serviceIntent != null) {
				boolean bool = super.stopService(this.serviceIntent);
				return bool;
			}
		} catch (Exception localException) {
			localException.printStackTrace();
			if (ToggleFlashSettings.camera != null) {
				ToggleFlashSettings.camera.release();
				ToggleFlashSettings.camera = null;
			}
			ToggleFlashSettings.camera = null;
		}
		return super.stopService(paramIntent);
	}

	
	public void toggleFlash() {		
		try {
			this.isCameraInUse = isCameraInUse();
			if (this.isCameraInUse) {
				stopService(this.serviceIntent);
				Log.e("ToggleFlashServer", "is isCameraInUse ");     
				return;
			}
			if (this.callState != 1) {
				Log.e("ToggleFlashServer", "callState is 1 ");     
				return;
			}
			 List localList = this.params.getSupportedFlashModes();
			if (!this.isFlashOn) {
				if ((localList.contains("torch"))
						|| (localList.contains("off"))
						|| (localList.contains("on"))) {
					Log.e("ToggleFlashServer", "start torch.... ");    
					this.params.setFlashMode("torch");
					if (ToggleFlashSettings.camera != null) {
						ToggleFlashSettings.camera.setParameters(this.params);
						ToggleFlashSettings.camera.startPreview();
					}
				}
				this.isFlashOn = true;
				return;
			}
			
			if ((localList.contains("torch")) || (localList.contains("off"))
					|| (localList.contains("on"))) {
				this.params.setFlashMode("off");
				if (ToggleFlashSettings.camera != null) {
					ToggleFlashSettings.camera.setParameters(this.params);
					ToggleFlashSettings.camera.stopPreview();
				}
			}
			this.isFlashOn = false;
			stopService(this.serviceIntent);
			
		} catch (Exception localException) {
			localException.printStackTrace();

		}
		
	}
}

public class ToggleFlashPrefs {
	public static final String CALL_FLASH_DURATION = "call_flash_duration";
	public static final String CALL_FLASH_INTERVAL = "call_flash_interval";
	public static final String MSG_FLASH_DURATION = "msg_flash_duration";
	public static final String MSG_FLASH_INTERVAL = "msg_flash_interval";
	public static final String SERVICE_STARTED = "service_started";
	public static final String SET_ALARM_ENABLE = "set_alarm_enable";
	public static final String SET_CALL_ENABLE = "set_call_enable";
	public static final String SET_MESSAGE_ENABLE = "set_message_enable";
	public static final String SET_NOTIFICATION_ENABLE = "set_notification_enable";

	public static boolean getAlarmEnable(Context paramContext) {
		return getSharedPreferences(paramContext).getBoolean(
				"set_alarm_enable", false);
	}

	public static boolean getCallEnable(Context paramContext) {
		return getSharedPreferences(paramContext).getBoolean("set_call_enable",
				true);
	}

	public static int getCallFlashDuration(Context paramContext) {
		return getSharedPreferences(paramContext).getInt("call_flash_duration",
				250);
	}

	public static int getCallFlashInterval(Context paramContext) {
		return getSharedPreferences(paramContext).getInt("call_flash_interval",
				0);
	}

	public static int getMSGFlashDuration(Context paramContext) {
		return getSharedPreferences(paramContext).getInt("msg_flash_duration",
				250);
	}

	public static int getMSGFlashInterval(Context paramContext) {
		return getSharedPreferences(paramContext).getInt("msg_flash_interval",
				0);
	}

	public static boolean getMessageEnable(Context paramContext) {
		return getSharedPreferences(paramContext).getBoolean(
				"set_message_enable", true);
	}

	public static boolean getNotificationEnable(Context paramContext) {
		return getSharedPreferences(paramContext).getBoolean(
				"set_notification_enable", false);
	}

	public static boolean getServiceStarted(Context paramContext) {
		return getSharedPreferences(paramContext).getBoolean("service_started",
				false);
	}

	static SharedPreferences getSharedPreferences(Context paramContext) {
		return PreferenceManager.getDefaultSharedPreferences(paramContext);
	}

	public static void setAlarmEnable(Context paramContext, boolean paramBoolean) {
		SharedPreferences.Editor editor = getSharedPreferences(paramContext)
				.edit();
		editor.putBoolean("set_alarm_enable", paramBoolean);
		editor.commit();
	}

	public static void setCallEnable(Context paramContext, boolean paramBoolean) {
		SharedPreferences.Editor editor = getSharedPreferences(paramContext)
				.edit();
		editor.putBoolean("set_call_enable", paramBoolean);
		editor.commit();
	}

	public static void setCallFlashDuration(Context paramContext, int paramInt) {
		SharedPreferences.Editor editor = getSharedPreferences(paramContext)
				.edit();
		editor.putInt("call_flash_duration", paramInt);
		editor.commit();
	}

	public static void setCallFlashInterval(Context paramContext, int paramInt) {
		SharedPreferences.Editor editor = getSharedPreferences(paramContext)
				.edit();
		editor.putInt("call_flash_interval", paramInt);
		editor.commit();
	}

	public static void setMSGFlashDuration(Context paramContext, int paramInt) {
		SharedPreferences.Editor editor = getSharedPreferences(paramContext)
				.edit();
		editor.putInt("msg_flash_duration", paramInt);
		editor.commit();
	}

	public static void setMSGFlashInterval(Context paramContext, int paramInt) {
		SharedPreferences.Editor editor = getSharedPreferences(paramContext)
				.edit();
		editor.putInt("msg_flash_interval", paramInt);
		editor.commit();
	}

	public static void setMessageEnable(Context paramContext,
			boolean paramBoolean) {
		SharedPreferences.Editor editor = getSharedPreferences(paramContext)
				.edit();
		editor.putBoolean("set_message_enable", paramBoolean);
		editor.commit();
	}

	public static void setNotificationEnable(Context paramContext,
			boolean paramBoolean) {
		SharedPreferences.Editor editor = getSharedPreferences(paramContext)
				.edit();
		editor.putBoolean("set_notification_enable", paramBoolean);
		editor.commit();
	}

	public static void setServiceStarted(Context paramContext,
			boolean paramBoolean) {
		SharedPreferences.Editor editor = getSharedPreferences(paramContext)
				.edit();
		editor.putBoolean("service_started", paramBoolean);
		editor.commit();
	}
}

关注公众号“康波之道”回复“小程序”获取1000个小程序打包源码。回复“大数据”获取多本大数据电子书

发表评论

邮箱地址不会被公开。 必填项已用*标注