본문 바로가기
프로그래밍/Android

Android Version Check Thread 안드로이드 버전 체크하기

by 듀빈 2016. 8. 4.
반응형

안드로이드 버전 체크하기


작동 방식은 HttpURLConnectoin 클래스를 이용하여 웹에서 버전 정보를 불러온 다음 안드로이드 어플리케이션의 버전이랑 확인 후 다르면 자동으로 play store로 이동 시켜주는 클래스 입니다.


질문은 댓글로 달아주세요!


버전 파일

version.txt

1
1.0
cs


Class GetVersionData.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
    // =======================================
    //   버전 정보 확인
    // =======================================
    public class GetVersionData implements Runnable {
        @Override
        public void run() {
            try {
                URL url = new URL("버전 정보 파일 위치(네트워크)");
                HttpURLConnection con = (HttpURLConnection) url.openConnection();
                InputStream is = con.getInputStream();
                BufferedReader br = new BufferedReader(new InputStreamReader(is, Charset.forName("euc-kr")));
                
                if( 버전 변수(1.0) != Double.parseDouble(br.readLine())) {
                    // 버전이 다를 경우
                    currentActivity.this.runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            new AlertDialog.Builder(currentActivity.this)
                                    .setTitle("업데이트 필요!")
                                    .setMessage("프로그램을 최신 버전으로 업데이트 해주셔야 사용 가능합니다!")
                                    .setCancelable(false)
                                    .setPositiveButton("네"new DialogInterface.OnClickListener() {
                                        @Override
                                        public void onClick(DialogInterface dialogInterface, int i) {
                                            currentActivity.this.runOnUiThread(new Runnable() {
                                                @Override
                                                public void run() {
                                                    final String appPackageName = getPackageName(); // getPackageName() from Context or Activity object
                                                    try {
                                                        startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=" + appPackageName)));
                                                    } catch (android.content.ActivityNotFoundException anfe) {
                                                        startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("https://play.google.com/store/apps/details?id=" + appPackageName)));
                                                    }
                                                    currentActivity.this.finish();
                                                }
                                            });
                                        }
                                    })
                                    .setNegativeButton("아니오"new DialogInterface.OnClickListener() {
                                        @Override
                                        public void onClick(DialogInterface dialogInterface, int i) {
                                            currentActivity.this.runOnUiThread(new Runnable() {
                                                @Override
                                                public void run() {
                                                    Toast.makeText(currentActivity.this"프로그램을 종료합니다!", Toast.LENGTH_SHORT).show();
                                                    currentActivity.this.finish();
                                                }
                                            });
                                        }
                                    })
                                    .show();
                        }
                    });
                } else {
                    // 버전이 같을 경우
                    // 다음 액티비티 실행
                    currentActivity.this.runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            Intent intent = new Intent(currentActivity.this, nextActivity.class);
                            startActivity(intent);
                            currentActivity.this.finish();
                        }
                    );
                }
            } catch ( Exception e ) { }
        }
    }
cs

위의 클래스는 currentActivity 내부 클래스로 선언 했기 때문에 currentActivity.this 를 사용하였지만, 외부 클래스로 빼 낼 경우에는 currentActivity.this 대신에 new GetVersionData(Activity activity) 형식으로 받아 와서 대체 해주어야 합니다! 그래도 모르시겠다면 댓글 주세요!


currentActivity.java

1
2
3
4
5
6
7
8
9
10
11
public class currentActivity extends AppCompatActivity {
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.current_activity);
 
        Thread th = new Thread(new GetVersionData());
        th.start();
    }
}
cs


currentActivity 와 nextActivity는 본인의 코드에 맞게 수정해주시면 됩니다.

반응형

'프로그래밍 > Android' 카테고리의 다른 글

위도 경도 길이 구하기  (0) 2018.04.09
안드로이드 유용한 라이브러리  (0) 2016.12.05