GPSの研究 その22


概要

GPSを理解したかった。
android2.1でGPSロガー作って見た。

写真

サンプルコード

sdcardに、log.txtが出来る。

package com.ohisamallc.ohiapo9;

import android.app.Activity;
import android.location.GpsStatus;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.widget.TableLayout;
import android.widget.TextView;
import android.view.ViewGroup;
import java.io.FileOutputStream;
import java.io.IOException;
import android.content.Context;
import android.text.format.DateFormat;
import android.util.Log;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import android.os.Environment;

public class ohiapo9 extends Activity
{
    private LocationManager mLocationManager;
    private LocationListener mLocationListener;
    private TextView textView;
    private File aFile;
    private FileOutputStream out;
    @Override public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        textView = new TextView(this);
        textView.setWidth(200);
        textView.setText("ok");
        TableLayout tableLayout = new TableLayout(this);
        tableLayout.addView(textView, new TableLayout.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
        setContentView(tableLayout);
        aFile = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/log.txt"); 
        try
        {
            out =  new FileOutputStream(aFile);
        } 
        catch (FileNotFoundException e)
        {
            e.printStackTrace();
        }
    }
    @Override protected void onDestroy()
    {
        super.onDestroy();
        mLocationManager.removeUpdates(mLocationListener);
    }
    @Override public void onStart()
    {
        super.onStart();
        mLocationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
        if (!mLocationManager.isProviderEnabled(LocationManager.GPS_PROVIDER))
        {
        }
        mLocationListener = new locationListener();
        mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 5, mLocationListener);
        mLocationManager.addNmeaListener(nmeaListener);
    }
    private final GpsStatus.NmeaListener nmeaListener = new GpsStatus.NmeaListener()
    {
        public void onNmeaReceived(long timestamp, String nmea)
        {
            String s = nmea + textView.getText().toString();
            textView.setText(s);
            try 
            {
                out.write(nmea.getBytes());
            } 
            catch (IOException e)
            {
                e.printStackTrace();
            }
        }
    };
    private class locationListener implements LocationListener
    {
        public void onLocationChanged(Location loc)
        {
        }
        public void onProviderDisabled(String arg0)
        {
        }
        public void onProviderEnabled(String provider)
        {
        }
        public void onStatusChanged(String provider, int status, Bundle extras)
        {
        }
    }
}

以上。