차근차근/Android

위치정보를 알아오는 간단한 예제(gps) -실행됨

예쁜꽃이피었으면 2015. 1. 8. 11:10

gps가 켜져 있지 않아도

계속 위치 정보를 받아온다.  갱신도 됨



http://skynsky.tistory.com/69


import android.app.Activity;

import android.content.Context;

import android.location.Criteria;

import android.location.LocationListener;

import android.location.LocationManager;

import android.os.Bundle;

import android.widget.Toast;


public class Location extends Activity implements LocationListener{

LocationManager location = null;

public void onCreate(Bundle savedInstanceState){

super.onCreate(savedInstanceState);


         location = (LocationManager)getSystemService(Context.LOCATION_SERVICE);

    Toast.makeText(this,"Starting..", Toast.LENGTH_SHORT).show();

   

    Criteria criteria = new Criteria();

    criteria.setAccuracy(Criteria.NO_REQUIREMENT);

    criteria.setPowerRequirement(Criteria.NO_REQUIREMENT);

   

    String provider = location.getBestProvider(criteria, true);

    location.requestLocationUpdates(provider,1000, 0, this);

        

        

        

@Override

public void onLocationChanged(android.location.Location location) {

// TODO Auto-generated method stub

double latitude = location.getLatitude();

double longitude = location.getLongitude();

Toast.makeText(this,latitude+","+longitude,Toast.LENGTH_LONG).show();

}


@Override

public void onStatusChanged(String provider, int status, Bundle extras) {

// TODO Auto-generated method stub

}


@Override

public void onProviderEnabled(String provider) {

// TODO Auto-generated method stub

}


@Override

public void onProviderDisabled(String provider) {

// TODO Auto-generated method stub

}

}


반응형