android.provider.Settings.Secure를 이용한 GPS On/Off 상태 체크
android.provider.Settings.Secure는 Application에서 system preference를 읽기전용으로 사용가능한 secure system settings이다.
- Android 2.2 ( API Level 8 )이상에서 지원
- Settings.Secure.isLocationProviderEnable() : Location Provider enable 체크를 위한 helper method.
[ Example ]
package com.example.myfirstapp;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.content.ContentResolver;
import android.location.LocationManager;
import android.provider.Settings;
import android.widget.TextView;
public class MainActivity extends Activity {
TextView textGpsStatus;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
textGpsStatus = (TextView)findViewById(R.id.gpsTextView);
displayGpsStatus();
return true;
}
private void displayGpsStatus(){
ContentResolver contentResolver = getBaseContext().getContentResolver();
boolean gpsStatus = Settings.Secure.isLocationProviderEnabled(contentResolver, LocationManager.GPS_PROVIDER);
if(gpsStatus){
textGpsStatus.setText("GPS: ON");
}else{
textGpsStatus.setText("GPS: OFF");
}
}
}
댓글 없음:
댓글 쓰기