안드로이드 RAM 사용량 구하기 , 전체 RAM 용량 , 사용가능 RAM 용량


MainActivity.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
    //RAM 사용량 구하기
    public String getRamUsageRate(){
        //Ram 사용량 구하기
        ActivityManager activityManager = (ActivityManager)getSystemService(Context.ACTIVITY_SERVICE);
        ActivityManager.MemoryInfo memoryInfo = new ActivityManager.MemoryInfo();
 
        activityManager.getMemoryInfo(memoryInfo);
        //전체 RAM용량
        double totalMem = memoryInfo.totalMem;  //API 16부터
        //사용가능한 RAM용량
        double availMem = memoryInfo.availMem;
 
        //사용량
        DecimalFormat df = new DecimalFormat("#,###");
        double ram = 100*(totalMem-availMem)/totalMem;
        return df.format(ram)+"%";
    }
cs



결과


Android 메뉴 폴더구조 - layout 폴더에 xml파일이 다 나온다




Project 메뉴 폴더구조 - main > res 폴더아래에 layouts > popup > layout 서브폴더와 layouts > view > layout 서브폴더를 만든다 

layout 폴더에 xml 파일이 있어야 한다



gradle파일

1
2
3
4
5
6
7
8
9
10
11
sourceSets {
    main {
        res.srcDirs =
                [
                        'src/main/res/layouts/popup',
                        'src/main/res/layouts/view',
                        'src/main/res'
                ]
    }
}
 
cs

layout 폴더 위의 경로까지만 적어준다


안드로이드  String으로된 Color값을 Int로 바꾸기
Color.parseColor("String 형태의 rgb Color값")

ex) Color.parseColor("#00B700")

안드로이드 상태바(Status Bar) 색깔 바꾸기


MainActivity.java

1
2
3
4
        //싱태바 색깔 바꾸기
        if(Build.VERSION.SDK_INT >= 21 ){
            getWindow().setStatusBarColor(Color.parseColor("#00B700"));
        }
cs

API21 부터 가능하다


안드로이드 홈화면(배경화면)으로 이동하기


MainActivity.java

1
2
3
4
5
6
7
    //버튼
    public void mOnGoHomeClick(View v){
        Intent intent = new Intent(Intent.ACTION_MAIN); //태스크의 첫 액티비티로 시작
        intent.addCategory(Intent.CATEGORY_HOME);   //홈화면 표시
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); //새로운 태스크를 생성하여 그 태스크안에서 액티비티 추가
        startActivity(intent);
    }
cs



결과

 


drawable폴더 > seekbar_seekbar1.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
<?xml version="1.0" encoding="utf-8"?>
 
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <!--시크바 배경-->
    <item android:id="@android:id/background">
        <shape android:shape="line">    <!--선 : 길이6dp -->
            <stroke android:width="6dp" android:color="#D5D5D5" />
        </shape>
    </item>
 
    <!-- 시크바 배경2 -->
    <item android:id="@android:id/secondaryProgress">
        <clip>
            <shape android:shape="line">
                <stroke android:width="6dp" android:color="#B2CCFF" />
            </shape>
        </clip>
    </item>
 
    <!-- 시크바 프로그래스 -->
    <item android:id="@android:id/progress" >
        <clip>
            <shape android:shape="line">
                <stroke android:width="6dp" android:color="#6799FF" />
            </shape>
        </clip>
    </item>
 
</layer-list>
 
cs
shape타입이 line이어야 thumb부분이 더 크게 보일 수 있다.



drawable폴더 > seekbar_seekbar1_thumb.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<?xml version="1.0" encoding="utf-8"?>
<!-- 원모양의 시크바 컨트롤러 -->
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="oval"
    android:useLevel="false"  >
    <!-- 배경 -->
    <solid
        android:color="#D9E5FF"/>
    <!-- 테두리 -->
    <stroke
        android:width="2dp"
        android:color="#0054FF" />
    <!-- 크기 -->
    <size
        android:width="13dp"
        android:height="13dp"/>
</shape>
 
cs


activity_main.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
 
    <LinearLayout
        android:paddingBottom="8dp"
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <!-- 시크바 -->
        <SeekBar
            android:progress="5"
            android:max="10"
            android:thumb="@drawable/seekbar_seekbar1_thumb"
            android:progressDrawable="@drawable/seekbar_seekbar1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
        <!--// 시크바 -->
    </LinearLayout>
 
cs



결과


activity_main.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
    <LinearLayout
        android:paddingLeft="16dp"
        android:paddingRight="16dp"
        android:paddingBottom="8dp"
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <!-- 프로그레스바 -->
        <ProgressBar
            android:max="100"
            android:progress="50"
            style="@style/Widget.AppCompat.ProgressBar.Horizontal"
            android:progressDrawable="@drawable/progressbar_progressbar1"
            android:layout_width="match_parent"
            android:layout_height="12dp" />
        <!--// 프로그레스바 -->
    </LinearLayout>


cs

style="@style/Widget.AppCompat.ProgressBar.Horizontal" 로 지정해야한다(가로형태의 프로그레스바)



drawable폴더 > progressbar_progressbar1.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
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:id="@android:id/background">
        <shape>
            <corners android:radius="6dp" />
            <solid android:color="#D5D5D5" />
        </shape>
    </item>
    <item android:id="@android:id/secondaryProgress">
        <clip>
            <shape>
                <corners android:radius="6dp" />
                <solid android:color="#B2CCFF" />
            </shape>
        </clip>
    </item>
    <item android:id="@android:id/progress">
        <clip>
            <shape>
                <corners android:radius="6dp" />
                <solid android:color="#6799FF" />
            </shape>
        </clip>
    </item>
</layer-list>
 
cs

<clip>을 써야 progress 속성이 먹힌다

모서리를 둥글게하고 배경색을 줌



결과


MainActivity.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
    //폰인지 여부
    public boolean IsPhone() {
        //화면 사이즈 종류 구하기
        int screenSizeType = getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK;
 
        if(screenSizeType== Configuration.SCREENLAYOUT_SIZE_NORMAL || screenSizeType==Configuration.SCREENLAYOUT_SIZE_SMALL){
            return true;
        }
        return false;
    }
 
    //태블릿인지 여부
    public boolean IsTablet() {
        //화면 사이즈 종류 구하기
        int screenSizeType = getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK;
 
        if(screenSizeType==Configuration.SCREENLAYOUT_SIZE_XLARGE || screenSizeType==Configuration.SCREENLAYOUT_SIZE_LARGE) {
            return true;
        }
        return false;
    }
cs

단말기의 Screen Size Type을 구하여 폰, 태블릿 여부를 판단한다.



SCREENLAYOUT_SIZE_SMALL

              설명 : 소형 화면저밀도(low-density) QVGA  비슷한 크기. -  싸고 화면 작은 저가 폰들이  범주에 속하

 는  같다.

              레이아웃 사이즈 :  최소 320 x 426 dp.

               :  low-density QGVA,  high-density VGA.


SCREENLAYOUT_SIZE_NORMAL

 설명 :  일반 화면,  medium-density HVGA  비슷한 크기. -  일반적인 갤럭시 노트 포함.

 레이아웃 사이즈 :  최소 320x470 dp.

  :   low-density WQVGA , medium-density HVGA , high-density WVGA.


SCREENLAYOUT_SIZE_LARGE

 설명 :  대형 화면,  medium-density VGA  비슷한 크기.  - 넥서스7 갤탭등.

 레이아웃 사이즈 :  최소 480x640 dp.

  :  medium-density VGA, medium-density  WVGA


SCREENLAYOUT_SIZE_XLARGE

 설명 :  (?)대형 화면,  medium-density VGA  비슷한 크기 - 현재까지는 갤탭 10.1 갤노트 10.1, 기타10인 처의 타블릿등등.

 레이아웃 사이즈 :   720x960 dp 이상.



결과


custom_actionbar.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
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:gravity="center_vertical"
    android:background="#2F9D27"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
 
    <ImageButton
        android:id="@+id/btnBack"
        android:layout_alignParentLeft="true"
        android:layout_width="56dp"
        android:layout_height="56dp"
        android:background="@drawable/top_back"/>
 
    <TextView
        android:text="타이틀"
        android:id="@+id/title"
        android:textSize="20sp"
        android:textColor="#fff"
        android:gravity="center_vertical"
        android:layout_marginLeft="17dp"
        android:layout_toRightOf="@id/btnBack"
        android:layout_width="match_parent"
        android:layout_height="56dp" />
 
    <ImageButton
        android:id="@+id/btnHistory"
        android:layout_alignParentRight="true"
        android:background="@drawable/top_history"
        android:layout_width="56dp"
        android:layout_height="56dp" />
</RelativeLayout>
cs

RelativeLayout을 이용하여 Custom한다



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
public class MainActivity extends AppCompatActivity {
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
 
        //커스텀 액션바 만들기
    }
 
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        ActionBar actionBar = getSupportActionBar();
 
        // Custom Actionbar를 사용하기 위해 CustomEnabled을 true 시키고 필요 없는 것은 false 시킨다
        actionBar.setDisplayShowCustomEnabled(true);
        actionBar.setDisplayHomeAsUpEnabled(false);            //액션바 아이콘을 업 네비게이션 형태로 표시합니다.
        actionBar.setDisplayShowTitleEnabled(false);        //액션바에 표시되는 제목의 표시유무를 설정합니다.
        actionBar.setDisplayShowHomeEnabled(false);            //홈 아이콘을 숨김처리합니다.
 
 
        //layout을 가지고 와서 actionbar에 포팅을 시킵니다.
        LayoutInflater inflater = (LayoutInflater)getSystemService(LAYOUT_INFLATER_SERVICE);
        View actionbar = inflater.inflate(R.layout.custom_actionbar, null);
 
        actionBar.setCustomView(actionbar);
 
        //액션바 양쪽 공백 없애기
        Toolbar parent = (Toolbar)actionbar.getParent();
        parent.setContentInsetsAbsolute(0,0);
 
        return true;
    }
}
cs

onCreateOptionsMenu를 override한다



결과


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 {
 
    //GPS
    LocationManager locationManager;
 
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
 
        //GPS ON/OFF 확인해서 OFF이면 GPS 설정화면으로 이동하기
 
 
        //LocationManager
        locationManager = (LocationManager)getSystemService(LOCATION_SERVICE);
    }
 
    //버튼
    public void mOnGPSClick(View v){
        //GPS가 켜져있는지 체크
        if(!locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)){
            //GPS 설정화면으로 이동
            Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
            intent.addCategory(Intent.CATEGORY_DEFAULT);
            startActivity(intent);
        }
    }
}
cs



AndroidMenifest.xml

1
2
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
cs



결과

 


+ Recent posts