티스토리 뷰

package com.ehsehsl.osz;
import android.content.IntentSender;
import android.os.Bundle;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import com.google.android.gms.common.api.ResolvableApiException;
import com.google.android.gms.location.LocationRequest;
import com.google.android.gms.location.LocationServices;
import com.google.android.gms.location.LocationSettingsRequest;
import com.google.android.gms.location.LocationSettingsResponse;
import com.google.android.gms.location.SettingsClient;
import com.google.android.gms.tasks.OnFailureListener;
import com.google.android.gms.tasks.OnSuccessListener;
import com.google.android.gms.tasks.Task;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
gps(); // 위치 체크
}
public void gps() {
LocationRequest locationRequest = LocationRequest.create();
locationRequest.setInterval(10000);
locationRequest.setFastestInterval(5000);
locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder().addLocationRequest(locationRequest);
SettingsClient client = LocationServices.getSettingsClient(this);
Task<LocationSettingsResponse> task = client.checkLocationSettings(builder.build());
task.addOnSuccessListener(this, new OnSuccessListener<LocationSettingsResponse>() {
@Override
public void onSuccess(LocationSettingsResponse locationSettingsResponse) {
// All location settings are satisfied. The client can initialize
// location requests here.
// ...
}
});
task.addOnFailureListener(this, new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
if (e instanceof ResolvableApiException) {
// Location settings are not satisfied, but this can be fixed
// by showing the user a dialog.
try {
// Show the dialog by calling startResolutionForResult(),
// and check the result in onActivityResult().
ResolvableApiException resolvable = (ResolvableApiException) e;
resolvable.startResolutionForResult(MainActivity.this,
0x1);
} catch (IntentSender.SendIntentException sendEx) {
// Ignore the error.
}
}
}
});
}
}
위치 서비스를 이용하는 앱인데 사용자가 위치 서비스를 꺼놨을 때 확인하여 키게 만들고 싶을 때 쓰면 좋은 코드입니다.
코드를 복붙하면 빨간줄이 많은데 함수 gps의 첫줄에 LocationRequest에 커서를 두고 빨간 전구 클릭해서 설정해주면 됩니다.
'안드로이드 네이티브' 카테고리의 다른 글
안드로이드 화면 전환 애니메이션 5가지 (0) | 2021.12.20 |
---|---|
안드로이드 - 웹 - phpMyAdmin 연동하기 (서버와 통신) (9) | 2021.05.18 |
안드로이드 뒤로 가기 버튼 두 번 눌러야 꺼지도록 하기 (0) | 2020.11.04 |
안드로이드 버튼 누르고 있으면 숫자 올라가거나 내려가기 (0) | 2020.10.26 |
안드로이드 진동, 소리, 음악 파일 제어하기 (0) | 2020.10.04 |
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
링크
TAG
- 안드로이드
- 구구단어플
- 안드로이드클라이언트
- sendinput
- 카카오톡
- inputbox
- 구구단공부
- Flutter
- 오토핫키
- 안드로이드스튜디오
- 로또
- 자바스크립트
- MouseMove
- 카카오봇
- loop
- 안드로이드네이티브
- 안드로이드앱
- 명언
- 안드로이드앱개발
- 노래
- 챗봇
- 플러터
- 채팅
- 앱개발
- 구구단앱
- JS
- 구구단
- 코틀린
- 매크로
- 자동답장
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
글 보관함