
GPS/ネットワークから座標を取得するサンプル#tryGPS00

GPSを使って座標を取り出す。
android.permission.ACCESS_FINE_LOCATIONを指定する
ネットワークを利用して位置情報を取り出したい時は
android.permission.ACCESS_COARSE_LOCATIONを使う
※ただし、コレを使う場合はACCESS_FINE_LOCATIONを指定してる場合はコレも一緒にONになってるから2個指定する必要は無い。
使えるすべてのプロバイダを使う様にしてなるべく精度のいい物をとれる様にする。
●検索した事
LOCATION_SERVICE
Android GPS座標
android Location getTime
java 時刻
●開発環境
Eclipse IDE バージョン: 3.7 Indigo Service Release 2
ターゲットプラットフォーム: 2.1
API レベル: 7
package sample.example.trygps00; import java.text.SimpleDateFormat; import java.util.List; import android.location.Criteria; import android.location.Location; import android.location.LocationListener; import android.location.LocationManager; import android.location.LocationProvider; import android.net.Uri; import android.os.Bundle; import android.app.Activity; import android.app.AlertDialog; import android.content.ActivityNotFoundException; import android.content.DialogInterface; import android.content.Intent; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.view.WindowManager; import android.widget.EditText; import android.support.v4.app.NavUtils; public class TryGPS00Activity extends Activity implements LocationListener { // ロケーションマネージャ private LocationManager mLocationManager; private Location mLastLocation = new Location(""); // 最後の位置 // 表示用のコントローラ private EditText mEditTextLat; // 緯度 private EditText mEditTextLng; // 経度 private EditText mEditTextLastUpdate; // 最終更新日 private EditText mEditTextStatus; // ステータス private EditText mEditTextProviderList; // プロバイダリスト @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_try_gps00); // 起動中はスリープしない this.getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); // ロケーションマネージャーを取り出す this.mLocationManager = (LocationManager)this.getSystemService(LOCATION_SERVICE); // コントローラーを取り出す this.mEditTextLat = (EditText)this.findViewById(R.id.editText_lat); this.mEditTextLng = (EditText)this.findViewById(R.id.editText_lng); this.mEditTextLastUpdate = (EditText)this.findViewById(R.id.editText_lastUpdate); this.mEditTextStatus = (EditText)this.findViewById(R.id.editText_status); this.mEditTextProviderList = (EditText)this.findViewById(R.id.editText_providerList); // 最適なプロバイダーを取り出す Criteria criteria = new Criteria(); criteria.setSpeedRequired(false); criteria.setBearingRequired(false); criteria.setAltitudeRequired(false); // ↓これで最後にGPSが取得した座標が取り出せる //Location location = this.mLocationManager.getLastKnownLocation(this.mBestGPSProvider); } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.activity_try_gps00, menu); return true; } @Override protected void onResume() { if (this.mLocationManager != null) { // 利用可能なプロバイダを全部使う(GPSがだめならネットワークみたいな考え方) // GPSは空が見えるところでないととれにくい。その場合はネットワークしか座標が取り出せない List<String> providerList = this.mLocationManager.getProviders(true); // //////////////////////////////////////////////////////////// // プロバイダリストの表示 String providerNames = ""; for (int i = 0; i < providerList.size(); i++) { providerNames = providerNames + " " + providerList.get(i); } this.mEditTextProviderList.setText(providerNames); // GPSが使えない if (this.mLocationManager.isProviderEnabled(LocationManager.GPS_PROVIDER) == false){ new AlertDialog.Builder(this) .setTitle("GPSが無効") .setMessage("GPS機能が有効ではありません。\n\n有効にすることで現在位置をさらに正確に検出できるようになります") .setPositiveButton("設定", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { try { startActivity(new Intent("android.settings.LOCATION_SOURCE_SETTINGS")); } catch (final ActivityNotFoundException e) {}; } }) .setNegativeButton("しない", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) {} }) .create() .show(); } // 全部の機能を使って座標を取り出す for (int i = 0; i < providerList.size(); i++) { this.mLocationManager.requestLocationUpdates( providerList.get(i), 10000, 1, this); } } super.onResume(); } @Override protected void onPause() { if (this.mLocationManager != null) { this.mLocationManager.removeUpdates(this); } super.onPause(); } // //////////////////////////////////////////////////////////// // 座標の取得コールバック // //////////////////////////////////////////////////////////// // //////////////////////////////////////////////////////////// // 座標が変更された public void onLocationChanged(Location location) { this.mLastLocation.set(location); // 座標とかを表示する this.mEditTextLat.setText(location.getLatitude() + ""); this.mEditTextLng.setText(location.getLongitude() + ""); SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss"); this.mEditTextLastUpdate.setText(sdf.format(location.getTime())); this.mEditTextStatus.setText(location.getProvider()); } public void onProviderDisabled(String provider) { // TODO 自動生成されたメソッド・スタブ } public void onProviderEnabled(String provider) { // TODO 自動生成されたメソッド・スタブ } // //////////////////////////////////////////////////////////// // ステータスが変更された public void onStatusChanged(String provider, int status, Bundle extras) { switch (status) { case LocationProvider.AVAILABLE: this.mEditTextStatus.setText("AVAILABLE"); break; case LocationProvider.OUT_OF_SERVICE: this.mEditTextStatus.setText("OUT_OF_SERVICE"); break; case LocationProvider.TEMPORARILY_UNAVAILABLE: this.mEditTextStatus.setText("TEMPORARILY_UNAVAILABLE"); break; } } // //////////////////////////////////////////////////////////// // 位置を表示するボタンがおされたら呼ばれる public void onClickShowLocation(View view) { Uri uri = Uri.parse("http://maps.google.co.jp/maps?q=" + this.mLastLocation.getLatitude() + "," + this.mLastLocation.getLongitude()); Intent intent = new Intent(Intent.ACTION_VIEW, uri); this.startActivity(intent); } }
磁気センサーの値を取りだして表示するサンプル#tryMagneticField00 [CSharp]SQL Compact3.5 でローカルデータベースに追加/削除/更新するサン...