0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

GPSの研究 その22

Posted at

概要

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

写真

device-2018-06-21-071855.png

サンプルコード

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)
		{
		}
	}
}

以上。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?