SystemClock.elapsedRealtime()

Posted by ITPangPang
2016. 4. 7. 00:36 안드로이드(android)/System


SystemClock.elapsedRealtime()




- elapsed라는 단어는 동사로 (시간이)흐르다 라는 뜻으로 SystemClock.elapsedRealtime()는 

  System이 부팅된 후 실제로 흐른 시간이다

  * 한가지 더 중요한것은 Deep Sleep 시간도 포함된다는 것이다.


- 이때 시간은 1ms단위로 기록됩니다(1초=1000), 시스템부팅이 된 후 1분 = 60000, 10분 = 600000

  

- 개념은 배웠으니 이제 한번 써먹어보겠습니다



① main.xml(부팅된 누적 시간만 간단히 알아보기 위해 TextView 1개, Button 1개 사용)

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".TimeTest" >
    <TextView
        android:id="@+id/tvRealTime"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="0"
        android:textSize="40sp" />
    <Button
        android:id="@+id/CheckBtn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="check" />
</LinearLayout>


 ②TimeTest.java


public class TimeTest extends Activity
{
    Button Check;
    TextView tvTime;
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        Check = (Button)findViewById(R.id.CheckBtn);
        tvTime = (TextView)findViewById(R.id.tvRealTime);
        
        Check.setOnClickListener(new View.OnClickListener() //버튼을 누르면
        {
            @Override
            public void onClick(View v)
            {
                tvTime.setText("" + SystemClock.elapsedRealtime());  //TextView에 부팅된 누적시간 표시
            }
        });
    }
}



Check 버튼을 누르니 6886901이란 숫자가 나왔다

(6886901/1000) = 부팅된지 6886초가 되었다는것 같다


과연 위에 시간을 어떻게 활용 할 수 있을까?

일단 결과값에서 부팅된 시간을 한번에 보기 쉽게 00시 00분 00초로 변환해보겠습니다



 

① 00초 => (time/1000)%60

   부팅시간 = time이라고 한다면 time을 일단 1000으로 나누면 초단위가 되겠죠?

   1000으로 나눈후 1분은 60초 이므로 60으로 나눈 나머지가 00초가 됩니다.

   이해가 잘 안되면 간단한 시간으로 테스트 해보면 됩니다

   ex) 270초는 몇분 몇초일까요? 쉽게 4분 30초라는걸 알 수 있습니다


       우리는 4분은 필요없고 뒤에 30초만 필요하므로 270%60을 해서 몫은 버리고 나머지만

 

② 00분 => (time/1000/60)%60

    분 또한 초를 구할때와 마찬가지로 일단 1000으로 나눈후 초로 변환 60으로 나눈후 분으로 변환

    그 후에 1시간은 60분이므로 마찬가지로 60으로 나눈 나머지가 00분이 됩니다

 

③ 00시 => (time/1000/3600)

   시간 구하는것도 마찬가지 입니다 (time/1000/60/60) 1시간이 3600초 라는걸 알면 금방

   구할 수 있습니다



 

main.xml

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".TimeTest" >
    <TextView
        android:id="@+id/tvRealTime"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="0"
        android:textSize="40sp" />
    <TextView
        android:id="@+id/tvConvertTime"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="0"
        android:textSize="40sp" />
    <Button
        android:id="@+id/CheckBtn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="check" />
</LinearLayout>

 


TimeTest.java

public class TimeTest extends Activity
{
    Button Check;
    TextView tvTime, tvConvert;
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        Check = (Button)findViewById(R.id.CheckBtn);
        tvTime = (TextView)findViewById(R.id.tvRealTime);
        tvConvert = (TextView)findViewById(R.id.tvConvertTime);
 
        Check.setOnClickListener(new View.OnClickListener() //버튼을 누르면
        {
            @Override
            public void onClick(View v)
            {
                //%02d는 두자리 십진수로 나타낸다는 얘기입니다
                String Hour = String.format("%02d",SystemClock.elapsedRealtime()/1000/3600);
                String Min = String.format("%02d",(SystemClock.elapsedRealtime()/1000/60)%60);
                String Sec = String.format("%02d",(SystemClock.elapsedRealtime()/1000)%60);
                tvTime.setText("" + SystemClock.elapsedRealtime());
                tvConvert.setText(Hour+"시간"+Min+"분"+Sec+"초");
            }
        });
    }
}

 


 

새로 부팅을 한 후에 바로 실행시켜봤습니다.


여기까지 변환했다면 조금만 응용하면 스탑워치도 만들 수 있습니다

Start버튼을 생성한 후 버튼을 누를때 누적부팅시간을 저장하고

다음 다시 누적되는 부팅시간과의 차이를 이용 + Handler를 사용하여 TextView에 찍어주면 됩니다