키보드가 올라올때 밑의 버튼(뷰)가 같이 따라 올라오는 것을 막는 옵션


AndroidMenifest.xml

1
android:windowSoftInputMode="adjustPan"
cs



또는



java 

1
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN)
cs

android:windowSoftInputMode – 소프트 키보드 상태 : 액티비티가 사용자 관심의 포커스를 받을 때 소프트 키보드가 보여지는지 여부를 설정한다. 

 


옵션

stateUnspecified : 소프트 키보드 상태에 대해 시스템이 적절한상태를 선택하거나 테마 설정값을 따른다. 소프트 키보드의 디폴트 설정 값.

stateUnchanged : 소프트 키보드는 마지막 상태로 유지

stateHidden : 사용자 액티비티를 선택할 때 소프트 키보드는 숨겨짐

stateAlwaysHidden : 액티비티의 메인 위도우가 입력 포커스를 가질 때 소프트 키보드는 항상 숨겨짐

stateVisible : 사용자가 액티비티 메인 위도우 앞으로 갈 때 소프트 키보드 보여짐

stateAlwaysVisible : 사용자가 액티비티를 선택할 때 소프트 키보드 보여짐

adjustUnspecified : 스크롤 할 수 잇는 레이아웃 뷰들을 가지고 있다면 윈도우 크기 재조정. 메인 윈도우의 디폴트 값

adjustResize : 스크린에 소프트 키보드 공간을 만들기 위해메인 윈도우 크기가 항상 재조정 됨 

adjustPan : 소프트 키보드 공간을 만들기 위해 메인 윈도우 크기가 재조정 되지 않음


에디트텍스트 뷰를 두면 화면진입시 키보드가 자동으로 올라오는데 
디트텍스트의 부모 레이아웃에 아래와 같은 옵션을 설정한다
1
android:focusableInTouchMode="true"
cs


화면진입시에는 키보드가 올라오지 않고 에디트텍스트를 클릭하여 포커스를 주면 키보드가 올라온다


저장할 폴더명과 이미지 파일명을 지정한다
지정한 저장경로로 촬영한 이미지를 저장할 수 있다
1
2
3
4
5
6
7
8
9
10
11
12
13
14
    //Camera App Call
    public void CameraAppCall(String dirPath, String fileName){
        Intent intent = new Intent();
 
        //촬영한 이미지 저장경로
        File file = new File(dirPath, fileName);
        intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(file));
 
        //카메라앱 호출
        intent.setAction(MediaStore.ACTION_IMAGE_CAPTURE);
 
        ((Activity)context).startActivityForResult(intent, REQUEST_CODE_CAMERA_APP_CALL);
    }
 
cs

이러면 사진이 2군데에 저장된다. (내가 지정한 경로와 카메라앱이 저장하는 경로)


디바이스 내에 호출할 수 있는 카메라앱이 존재하는지 확인한다

1
2
3
4
5
6
7
8
9
    //사용가능한 카메라앱 존재 유무
    public boolean IsCameraApp(){
        PackageManager packageManager = context.getPackageManager();
        Intent inten = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        List<ResolveInfo> list = packageManager.queryIntentActivities(inten, PackageManager.MATCH_DEFAULT_ONLY);
 
        if(list.size()>0return true;  //존재
        return false;   //존재안함함
    }
cs


안드로이드 사이드메뉴(페이지 슬라이딩) 구현 예제 , 슬라이드 레이아웃


MainActivity.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
public class MainActivity extends AppCompatActivity {
 
    //슬라이드 열기/닫기 플래그
    boolean isPageOpen = false;
    //슬라이드 열기 애니메이션
    Animation translateLeftAnim;
    //슬라이드 닫기 애니메이션
    Animation translateRightAnim;
    //슬라이드 레이아웃
    LinearLayout slidingPage01;
 
    Button button1;
 
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
 
        //UI
        slidingPage01 = (LinearLayout)findViewById(R.id.slidingPage01);
        button1 = (Button)findViewById(R.id.buton1);
 
        //애니메이션
        translateLeftAnim = AnimationUtils.loadAnimation(this, R.anim.translate_left);
        translateRightAnim = AnimationUtils.loadAnimation(this, R.anim.translate_right);
 
        //애니메이션 리스너 설정
        SlidingPageAnimationListener animationListener = new SlidingPageAnimationListener();
        translateLeftAnim.setAnimationListener(animationListener);
        translateRightAnim.setAnimationListener(animationListener);
    }
 
    //버튼
    public void onButton1Clicked(View v){
        //닫기
        if(isPageOpen){
            //애니메이션 시작
            slidingPage01.startAnimation(translateRightAnim);
        }
        //열기
        else{
            slidingPage01.setVisibility(View.VISIBLE);
            slidingPage01.startAnimation(translateLeftAnim);
        }
    }
 
    //애니메이션 리스너
    private class SlidingPageAnimationListener implements Animation.AnimationListener {
        @Override
        public void onAnimationEnd(Animation animation) {
            //슬라이드 열기->닫기
            if(isPageOpen){
                slidingPage01.setVisibility(View.INVISIBLE);
                button1.setText("Open");
                isPageOpen = false;
            }
            //슬라이드 닫기->열기
            else{
                button1.setText("Close");
                isPageOpen = true;
            }
        }
        @Override
        public void onAnimationRepeat(Animation animation) {
 
        }
        @Override
        public void onAnimationStart(Animation animation) {
 
        }
    }
}
 
cs



activity_main.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
 
    <!-- 일반 레이아웃 -->
    <LinearLayout
        android:orientation="vertical"
        android:background="#eee"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <TextView
            android:text="일반 레이아웃"
            android:textColor="#000"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
    </LinearLayout>
 
    <!-- 슬라이드 레이아웃 -->
    <LinearLayout
        android:id="@+id/slidingPage01"
        android:orientation="vertical"
        android:background="#5F00FF"
        android:visibility="gone"
        android:layout_gravity="right"
        android:layout_width="200dp"
        android:layout_height="match_parent">
        <TextView
            android:text="Slide Contents"
            android:textColor="#000"
            android:layout_weight="1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
        <TextView
            android:text="Slide Contents"
            android:textColor="#000"
            android:layout_weight="1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
    </LinearLayout>
 
    <!-- 열기/닫기 버튼 레이아웃 -->
    <LinearLayout
        android:orientation="vertical"
        android:layout_gravity="right|center_vertical"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">
        <Button
            android:id="@+id/buton1"
            android:text="Open"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:onClick="onButton1Clicked"/>
    </LinearLayout>
 
</FrameLayout>
 
cs



anim폴더 > translate_left.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
<?xml version="1.0" encoding="utf-8"?>
<!--android:fillAfter="true" 애니메이션 종료후 그 상태를 유지-->
<!-- android:interpolator="@android:anim/accelerate_decelerate_interpolator" 애니메이션이 점점 빠르게 동작하다가 점점 천천히 동작-->
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:fillAfter="true"
    android:interpolator="@android:anim/accelerate_decelerate_interpolator">
    <!-- 오른쪽에서 왼쪽으로 0.5초 간 이동 -->
    <translate
        android:fromXDelta="100%p"
        android:toXDelta="0%"
        android:duration="500"
        android:repeatCount="0"/>
</set>
cs



anim폴더 > translate_right.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
<?xml version="1.0" encoding="utf-8"?>
<!--android:fillAfter="true" 애니메이션 종료후 그 상태를 유지-->
<!-- android:interpolator="@android:anim/accelerate_decelerate_interpolator" 애니메이션이 점점 빠르게 동작하다가 점점 천천히 동작-->
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:fillAfter="true"
    android:interpolator="@android:anim/accelerate_decelerate_interpolator">
    <!-- 왼쪽에서 오른쪽으로 0.5초 간 이동 -->
    <translate
        android:fromXDelta="0%p"
        android:toXDelta="100%"
        android:duration="500"
        android:repeatCount="0"/>
</set>
cs



결과

 


안드로이드 뷰에 애니메이션 주기 예제 , 글자흐르는 애니메이션


MainActivity.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
public class MainActivity extends AppCompatActivity {
 
    Animation flowAnim;
    TextView textView1;
 
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
 
        //애니메이션
        flowAnim = AnimationUtils.loadAnimation(this, R.anim.flow);
 
        //UI
        textView1 = (TextView)findViewById(R.id.textView1);
    }
 
    //버튼
    public void onButton1Clicked(View v){
        //애니메이션 객체에 리스너 설정
        flowAnim.setAnimationListener(new FlowAnimationListener());
        flowAnim.setRepeatCount(2);
 
        //뷰에 애니메이션 설정정
       textView1.startAnimation(flowAnim);
    }
 
 
    //애니메이션 리스너너
    private final class FlowAnimationListener implements Animation.AnimationListener {
        //애니메이션 종료되었을때
        @Override
        public void onAnimationEnd(Animation animation) {
            Toast.makeText(getApplicationContext(), "애니메이션 종료됨", Toast.LENGTH_SHORT).show();
        }
 
        @Override
        public void onAnimationRepeat(Animation animation) {
            Log.d("ddd""Animation Repeat");
        }
        //애니메이션 시작했을때
        @Override
        public void onAnimationStart(Animation animation) {
            Log.d("ddd""Animation Start");
        }
    }
}
cs



activity_main.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
 
    <TextView
        android:id="@+id/textView1"
        android:text="흐르는 글자 구현하기..."
        android:textColor="#00f"
        android:textSize="30dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>
 
    <LinearLayout
        android:orientation="horizontal"
        android:paddingTop="10dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <Button
            android:id="@+id/startButton"
            android:text="시작 "
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:onClick="onButton1Clicked" />
    </LinearLayout>
 
</LinearLayout>
 
cs



anim폴더 > flow.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <!--위치이동을 위한 애니메이션 액션 정의-->
    <!--오른쪽에서 왼쪽으로 6초동안 이동 * 3회반복 -->
    <translate android:fromXDelta="100%p"
        android:toXDelta="0%p"
        android:duration="6000"
        android:repeatCount="3" />
 
    <!--투명도 변경을 위한 애니메이션 액션 정의-->
    <!--투명도 0.1 -> 1.0 으로 6초동안 변경 * 3회반복-->
    <alpha android:fromAlpha="0.1"
        android:toAlpha="1"
        android:duration="6000"
        android:repeatCount="3" />
</set>
cs



결과

 


안드로이드 프레그먼트(Fragment) 예제2 , 버튼 클릭할 때마다 이미지 바꾸기 , 두개의 Fragment간의 통신


MainActivity.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
public class MainActivity extends AppCompatActivity implements TabFragment.ImageSelectionCallback {
 
    TabFragment tabFragment;
    ViewerFragment viewerFragment;
 
    int []images = {R.drawable.dream01, R.drawable.dream02, R.drawable.dream03};
 
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
 
 
        FragmentManager manager = getSupportFragmentManager();
 
        tabFragment = (TabFragment)manager.findFragmentById(R.id.listFragment);
        viewerFragment = (ViewerFragment)manager.findFragmentById(R.id.viewerFragment);
    }
 
 
    @Override
    public void onImageSelected(int position) {
        viewerFragment.setImage(images[position]);
    }
}
cs



activity_main.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
 
    <fragment
        android:id="@+id/listFragment"
        android:layout_marginTop="10dp"
        android:layout_marginBottom="10dp"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:name="com.ghj.ex_01_2.TabFragment" />
 
    <fragment
        android:id="@+id/viewerFragment"
        android:layout_weight="1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:name="com.ghj.ex_01_2.ViewerFragment" />
 
</LinearLayout>
 
cs


TabFragment.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
public class TabFragment  extends Fragment {
 
    public ImageSelectionCallback callback;
 
    //인터페이스 - 탭 클릭시 이미지 변경하기 위한 인터페이스
    //여러 액티비티가 fragment를 호출하여도 동일한 인터페이스를 구현하도록 한다
    public static interface ImageSelectionCallback{
        public void onImageSelected(int position);
    }
 
 
    //fragment가 activity에 연결되었을때 호출
    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
 
        if(context instanceof ImageSelectionCallback){
            callback = (ImageSelectionCallback)context;
        }
    }
 
    //fragment 뷰 계층을 리턴
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        ViewGroup rootView = (ViewGroup)inflater.inflate(R.layout.fragment_tab, container, false);
 
        //UI
        Button btn1 = (Button)rootView.findViewById(R.id.btnImg1);
        Button btn2 = (Button)rootView.findViewById(R.id.btnImg2);
        Button btn3 = (Button)rootView.findViewById(R.id.btnImg3);
 
        //이벤트
        btn1.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v) {
                callback.onImageSelected(0);
            }
        });
        btn2.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v) {
                callback.onImageSelected(1);
            }
        });
        btn3.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v) {
                callback.onImageSelected(2);
            }
        });
 
        return rootView;
    }
}
 
cs



fragment_tab.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
 
    <Button
        android:id="@+id/btnImg1"
        android:text="첫번째"
        android:layout_weight="1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>
 
    <Button
        android:id="@+id/btnImg2"
        android:text="두번째"
        android:layout_weight="1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>
 
    <Button
        android:id="@+id/btnImg3"
        android:text="세번째"
        android:layout_weight="1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
 
</LinearLayout>
cs


ViewerFragment.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
public class ViewerFragment extends Fragment {
 
    ImageView imageView;
 
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        ViewGroup rootView = (ViewGroup)inflater.inflate(R.layout.fragment_viewer, container, false);
        imageView = (ImageView)rootView.findViewById(R.id.imageView);
 
        return rootView;
    }
 
    //이미지 설정
    public void setImage(int resId){
        imageView.setImageResource(resId);
    }
}
 
cs



fragment_viewer.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
 
    <!-- android:scaleType="centerInside" : 이미지뷰에 맞춰 이미지의 크기를 동일한 비율로 맞춘다 -->
    <ImageView
        android:id="@+id/imageView"
        android:scaleType="centerInside"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
 
</LinearLayout>
cs



결과

  


안드로이드 프레그먼트(Fragment) 예제


MainActivity.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
 
public class MainActivity extends AppCompatActivity {
 
    Fragment1 fragment1;
    Fragment2 fragment2;
 
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
 
        //fragment 객체
        fragment1 = (Fragment1)getSupportFragmentManager().findFragmentById(R.id.mainFragment);
        fragment2 = new Fragment2();
    }
 
    //fragment 교체
    public void onFragmentChanged(int index){
        if(index==0){
            //fragment manager 를 이용하여 현재의 fragment를 교체
            //begin transaction를 이용하여 롤백할 수 있도록한다
            //commit를 호출하여 실행
            getSupportFragmentManager().beginTransaction().replace(R.id.container, fragment2).commit();
        }else if(index==1){
            getSupportFragmentManager().beginTransaction().replace(R.id.container, fragment1).commit();
        }
    }
}
cs



activity_main.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:background="#eee"
    android:padding="16dp"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
 
    <!-- fragment 는 RelativeLayout 안에 있어야 한다 -->
    <RelativeLayout
        android:id="@+id/container"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <fragment
            android:id="@+id/mainFragment"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:name="com.ghj.ex_01.Fragment1"/>
    </RelativeLayout>
 
</LinearLayout>
 
cs



Fragment1.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
public class Fragment1 extends Fragment {
 
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        //화면 inflate
        ViewGroup rootView = (ViewGroup)inflater.inflate(R.layout.fragment1, container, false);
 
        //버튼 이벤트정의
        Button button = (Button)rootView.findViewById(R.id.button);
        button.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v) {
                //fragment 를 호출한 activity 얻기
                MainActivity activity = (MainActivity)getActivity();
                activity.onFragmentChanged(0);
            }
        });
        return rootView;
    }
}
cs



fragment1.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:background="#f00"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
 
    <TextView
        android:text="첫번째 프래그먼트"
        android:textSize="30dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
 
    <Button
        android:id="@+id/button"
        android:text="두번째 화면으로"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
</LinearLayout>
cs



Fragment2.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
public class Fragment2 extends Fragment {
 
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        ViewGroup rootView = (ViewGroup)inflater.inflate(R.layout.fragment2, container, false);
 
        Button button = (Button)rootView.findViewById(R.id.button);
        button.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v) {
                MainActivity activity = (MainActivity)getActivity();
                activity.onFragmentChanged(1);
            }
        });
        return rootView;
    }
}
cs



fragment2.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:background="#00f"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
 
    <TextView
        android:text="두번째 프래그먼트"
        android:textSize="30dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
 
    <Button
        android:id="@+id/button"
        android:text="첫번째 화면으로"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
</LinearLayout>
cs



결과

 


안드로이드 CPU Core 개수 구하기


MainActivity.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
    //CPU 코어갯수
    public String getCpuCores(){
        File dir = new File("/sys/devices/system/cpu/");
        //CPU 코어 파일목록
        File []files = dir.listFiles(new FileFilter() {
            @Override
            public boolean accept(File pathname) {
                if(Pattern.matches("cpu[0-9]", pathname.getName())){
                    return true;
                }
                return false;
            }
        });
 
        //CPU Core 개수
        return files.length+"개";
    }
cs



결과


안드로이드 내부저장소(Internal Storage) 사용량 구하기 , 저장소 전체용량 , 저장소 사용가능 용량


MainActivity.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
    //내부저장소(Internal Storage) 사용량 구하기
    public String getInternalUsageRate(){
        String path = Environment.getDataDirectory().getAbsolutePath();
        StatFs stat = new StatFs(path);
 
        long blockSize = stat.getBlockSizeLong();   //API 18부터
        //전체 Internal Storage 크기
        long totalSize = stat.getBlockCountLong() * blockSize;   //API 18부터
        //사용가능한 Internal Storage 크기
        long availableSize = stat.getAvailableBlocksLong() * blockSize;   //API 18부터
 
        //사용량
        DecimalFormat df = new DecimalFormat("#,###");
        double storage = 100.0*(totalSize-availableSize)/totalSize;
        return df.format(storage)+"%";
    }
cs



결과


+ Recent posts