[android] 位置情報をバックグラウンドで取得、DBに追加

目次

位置情報をバックグラウンドで取得、DBに追加

位置情報をバックグラウンドで取得するにはserviceを立ち上げてバックグラウンドで都度位置情報を取得する

Manifest.xml



LocationReceiver.java

public class LocationReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent){
        String locationKey = LocationManager.KEY_LOCATION_CHANGED;
        String providerEnableKey = LocationManager.KEY_PROVIDER_ENABLED;

        if(intent.hasExtra(providerEnableKey)){
            if(!intent.getBooleanExtra(providerEnableKey, true)){
                Toast.makeText(context, "provider disabled",
                        Toast.LENGTH_SHORT).show();
            }else{
                Toast.makeText(context, "provider enabled",
                        Toast.LENGTH_SHORT).show();
            }
        }

        if(intent.hasExtra(locationKey)){
            Location loc = (Location)intent.getExtras().get(locationKey);
            Toast.makeText(context, "Location changed : Lat: " +
                    loc.getLatitude() + "Lng: " + loc.getLongitude(),
                    Toast.LENGTH_SHORT).show();

            DBAdapter db = new DBAdapter(context);
            db.open();
            db.insertLocation(String.valueOf(loc.getLatitude()),
                    String.valueOf(loc.getLongitude()));
            db.close();
        }
    }

}

*SQLiteの実装方法は
[android] SQLiteを実装(DB Browser for SQLiteでDBを作成)
を参照

LocationService.java

10秒たった時に10m以上動いていた場合に位置情報を取得する

public class LocationService extends Service
{
    private static final String TAG = "LocationService";
    private LocationManager mLocationManager = null;
    private static final int LOCATION_INTERVAL = 10000; //10秒
    private static final float LOCATION_DISTANCE = 10f; //10m
    PendingIntent pendingIntent;

    @Override
    public IBinder onBind(Intent arg0){
        return null;
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId){
        Toast.makeText(this, "Service Started", Toast.LENGTH_LONG).show();

        //---use the LocationManager class to obtain locations data---
        mLocationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        Intent i = new Intent(this, LocationReceiver.class);
        pendingIntent = PendingIntent.getBroadcast(
                this, 0, i, PendingIntent.FLAG_UPDATE_CURRENT);

        //---request for location updates using GPS---
        try {
            mLocationManager.requestLocationUpdates(
                    LocationManager.GPS_PROVIDER, LOCATION_INTERVAL, LOCATION_DISTANCE,
                    pendingIntent);
        } catch (SecurityException ex) {
            Log.i(TAG, "fail to request location update, ignore", ex);
        } catch (IllegalArgumentException ex) {
            Log.d(TAG, "gps provider does not exist " + ex.getMessage());
        }
        return START_STICKY;
    }

    @Override
    public void onDestroy(){
        //---remove the pending intent---
        mLocationManager.removeUpdates(pendingIntent);

        super.onDestroy();
        Toast.makeText(this, "Service Destroyed", Toast.LENGTH_LONG).show();
    }
}

MainActivity.java

Intent intent = new Intent(this, LocationService.class);
startService(intent);

コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です