0
이것은 완벽하게 작동합니다.Android GPS를 활성으로 사용
public class GPSActivity extends Activity {
LocationManager locmgr = null;
String android_id;
DbAdapter db;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
db = new DbAdapter(getBaseContext());
db.open();
android_id = Secure.getString(getBaseContext().getContentResolver(), Secure.ANDROID_ID);
locmgr = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
locmgr.requestLocationUpdates(LocationManager.GPS_PROVIDER, 5000, 5, this.onLocationChange);
Toast.makeText(getBaseContext(), "Waiting for location..." , Toast.LENGTH_SHORT).show();
}
LocationListener onLocationChange=new LocationListener() {
public void onLocationChanged(Location loc) {
String latlong = "Lat: " + loc.getLatitude() + " Long: " + loc.getLongitude();
Toast.makeText(getBaseContext(), latlong, Toast.LENGTH_SHORT).show();
try{
db.insertGPSCoordinates(android_id, Double.toString(loc.getLatitude()), Double.toString(loc.getLongitude()));
}
catch(Exception e){
}
}
public void onProviderDisabled(String provider) {
}
public void onProviderEnabled(String provider) {
}
public void onStatusChanged(String provider, int status,
Bundle extras) {
}
};
//pauses listener while app is inactive
@Override
public void onPause() {
super.onPause();
locmgr.removeUpdates(onLocationChange);
}
//reactivates listener when app is resumed
@Override
public void onResume() {
super.onResume();
locmgr.requestLocationUpdates(LocationManager.GPS_PROVIDER, 5000, 5, onLocationChange);
}
}
어떻게 이것을 Broadcast Receiver에서 사용합니까? BOOT_COMPLETE 일 때 좌표를 수집하기를 원합니다. 그리고 그 설정에 따라 실행을 계속하십시오 locmgr.requestLocationUpdates(LocationManager.GPS_PROVIDER, 5000, 5, this.onLocationChange);
. 주기적으로 작업을 유지하는 방법을 혼란스럽게 생각합니다.
서비스중인 LocationListener가 필요합니까? – user533844