GPSまたはネットワークWi-Fiから現在位置の情報を取得して、テキストで表示する。
LocationSwitcherActivity.java ソース
package bz.hi_five.locationswitcher;
import android.app.Activity;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.widget.TextView;
/**
* 位置情報取得して色々やってみよう処理
*
*/
public class LocationSwitcherActivity extends Activity implements
LocationListener {
protected TextView locationView;
protected TextView statusView;
protected TextView providerView;
protected LocationManager mLocationManager;
protected String bestProvider;
// アクティビティ生成時に実行する
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
findViews();
// 位置情報の初期設定
initLocationService();
}
// ユーザの操作受付時に呼ばれる
@Override
protected void onResume() {
super.onResume();
// 最小で○○msec周期、
// 最小で○○mの位置変化の場合
// どんなに変化しても5000msecのより短い間隔では通知されず、1mより小さい変化の場合は通知されない
mLocationManager.requestLocationUpdates(bestProvider, 5000, 1, this);
}
// ユーザの操作中断時に実行する
@Override
protected void onPause() {
super.onPause();
mLocationManager.removeUpdates(this);
}
// 位置情報の初期設定
protected void initLocationService() {
mLocationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
Criteria criteria = new Criteria();
// この設定でGPS(ACCURACY_FINE)とネットワーク(ACCURACY_COARSE)を切り替え出来ます
criteria.setAccuracy(Criteria.ACCURACY_FINE);
criteria.setPowerRequirement(Criteria.POWER_MEDIUM);
bestProvider = mLocationManager.getBestProvider(criteria, true);
providerView.setText(bestProvider);
}
// 画面表示の初期設定
protected void findViews() {
locationView = (TextView) findViewById(R.id.location_view);
statusView = (TextView) findViewById(R.id.status_view);
providerView = (TextView) findViewById(R.id.provider_view);
}
// 緯度と経度を表示する
protected void refreshGeoLocation(Location location) {
String desc = "経度:" + location.getLongitude() + ", 緯度:" + location.getLatitude();
locationView.setText(desc);
}
// 位置情報が変更された場合に実行する
@Override
public void onLocationChanged(Location location) {
refreshGeoLocation(location);
statusView.setText("Log: onLocationChanged");
}
@Override
public void onProviderDisabled(String provider) {
statusView.setText("Log: onProviderDisabled");
}
@Override
public void onProviderEnabled(String provider) {
statusView.setText("Log: onProviderEnabled");
}
// 位置情報のステータスが変更された場合に実行する(onLocationChangedの後)
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
statusView.setText("Log: onStatusChanged");
}
}
main.xmlのソース
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Location Switcher"
/>
<TextView
android:id="@+id/status_view"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text=""
/>
<TextView
android:id="@+id/provider_view"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text=""
/>
<TextView
android:id="@+id/location_view"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text=""
/>
</LinearLayout>
AndroidManifest.xmlに以下を記入します
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
LocationSwitcherActivity.java ソース
package bz.hi_five.locationswitcher;
import android.app.Activity;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.widget.TextView;
/**
* 位置情報取得して色々やってみよう処理
*
*/
public class LocationSwitcherActivity extends Activity implements
LocationListener {
protected TextView locationView;
protected TextView statusView;
protected TextView providerView;
protected LocationManager mLocationManager;
protected String bestProvider;
// アクティビティ生成時に実行する
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
findViews();
// 位置情報の初期設定
initLocationService();
}
// ユーザの操作受付時に呼ばれる
@Override
protected void onResume() {
super.onResume();
// 最小で○○msec周期、
// 最小で○○mの位置変化の場合
// どんなに変化しても5000msecのより短い間隔では通知されず、1mより小さい変化の場合は通知されない
mLocationManager.requestLocationUpdates(bestProvider, 5000, 1, this);
}
// ユーザの操作中断時に実行する
@Override
protected void onPause() {
super.onPause();
mLocationManager.removeUpdates(this);
}
// 位置情報の初期設定
protected void initLocationService() {
mLocationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
Criteria criteria = new Criteria();
// この設定でGPS(ACCURACY_FINE)とネットワーク(ACCURACY_COARSE)を切り替え出来ます
criteria.setAccuracy(Criteria.ACCURACY_FINE);
criteria.setPowerRequirement(Criteria.POWER_MEDIUM);
bestProvider = mLocationManager.getBestProvider(criteria, true);
providerView.setText(bestProvider);
}
// 画面表示の初期設定
protected void findViews() {
locationView = (TextView) findViewById(R.id.location_view);
statusView = (TextView) findViewById(R.id.status_view);
providerView = (TextView) findViewById(R.id.provider_view);
}
// 緯度と経度を表示する
protected void refreshGeoLocation(Location location) {
String desc = "経度:" + location.getLongitude() + ", 緯度:" + location.getLatitude();
locationView.setText(desc);
}
// 位置情報が変更された場合に実行する
@Override
public void onLocationChanged(Location location) {
refreshGeoLocation(location);
statusView.setText("Log: onLocationChanged");
}
@Override
public void onProviderDisabled(String provider) {
statusView.setText("Log: onProviderDisabled");
}
@Override
public void onProviderEnabled(String provider) {
statusView.setText("Log: onProviderEnabled");
}
// 位置情報のステータスが変更された場合に実行する(onLocationChangedの後)
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
statusView.setText("Log: onStatusChanged");
}
}
main.xmlのソース
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Location Switcher"
/>
<TextView
android:id="@+id/status_view"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text=""
/>
<TextView
android:id="@+id/provider_view"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text=""
/>
<TextView
android:id="@+id/location_view"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text=""
/>
</LinearLayout>
AndroidManifest.xmlに以下を記入します
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
Comments
Post a Comment