나는 블루투스를 통해 센서와 통신하기 위해 Android 앱을 개발 중이다.Android 서비스의 전역 변수 선언
내 코드의 스냅 샷을 첨부하고 있습니다. 이것은 서비스 내부에 작성되었습니다
내 센서에서 탭 (g force> 0.5g (yAccel> 500))을 탭하면 Android 휴대 전화 화면이 잠기 게하고 잠긴 후에 잠글 수 있어야합니다.) 0.1g 이하로 센서를 두 드릴 때.
내 논리 구현 : lockStatus (initialize = 2)라는 변수를 선언했습니다. 일단 탭이 감지되면 lockStatus = 1로 설정하고 screen (screenLock.class)을 잠그십시오. 이미 잠겨 있고 두 번째 탭이 < 0.1g 인 것을 계속 확인하십시오. 잠금을 해제하십시오. 이미 잠겨 아무 탭이 감지되지 않으면 는, 잠금 상태를 유지 (screenLock.class) 내 경우에는
문제 :
지금까지 내가 지킬> 0.5G의 힘을 적용하는 화면이 고정됩니다 (ScreenLock .class), 내가 0.5g에서 그것을 줄이는 순간, 화면 잠금이 해제됩니다.
이유 : 이것은 서비스 내부에 기록됩니다 (Bluetooth 서비스가 계속 실행되어야 함). 잠시 후, 다음 데이터 (첫 번째 잠금 이후에도 아무런 탭도 없음)가 내 센서의 블루투스를 통해 전송되고 lockStatus는 2로 고정되고 다른 블록으로 넘어 가서 화면이 잠금 해제됩니다.
글로벌 변수 lockStatus는 어디에 선언해야합니까? 그러면 서비스 (BLEService)를 호출 할 때마다 초기화되지 않습니다.
public class BLEService extends Service {
public static BLEService singleInstance;
private final static String logTag = BLEService.class.getSimpleName();
public int lockStatus = 2;
//// Section of Code of My Logic
if ((checksum_362 & 0x80) == 128) showAlert = true;
if (yAccel > 500 || yAccel < -500)
{
gMeasure = true;
lockStatus = 1; //lock done;
Intent intent = new Intent(this, ScreenLock.class);
startActivity(intent);
//delay for min 10 min
}
else
{
if (lockStatus == 1)
{
if (yAccel < 100 || yAccel > -100)
{
//UnLock
lockStatus = 2;
}
else
{
gMeasure = true;
lockStatus = 1; //lock done;
Intent intent = new Intent(this, ScreenLock.class);
startActivity(intent);
}
}
}
ChartActivity.singleInstance.newChartData(xAccel, yAccel, zAccel, showAlert, gMeasure);
GoogleDriveService.singleInstance.newLogData(xAccel, yAccel, zAccel, showAlert);
에 init을 싱글을 만들 수 있습니다. -http : //developer.android.com/reference/android/app/Application.html –