Skip to content

Commit f582ed2

Browse files
committed
AndroidでGPSセンサを利用する方法
1 parent 613c0a2 commit f582ed2

File tree

10 files changed

+197
-0
lines changed

10 files changed

+197
-0
lines changed

Location/.classpath

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<classpath>
3+
<classpathentry kind="src" path="src"/>
4+
<classpathentry kind="src" path="gen"/>
5+
<classpathentry kind="con" path="com.android.ide.eclipse.adt.ANDROID_FRAMEWORK"/>
6+
<classpathentry kind="output" path="bin"/>
7+
</classpath>

Location/.project

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<projectDescription>
3+
<name>Location</name>
4+
<comment></comment>
5+
<projects>
6+
</projects>
7+
<buildSpec>
8+
<buildCommand>
9+
<name>com.android.ide.eclipse.adt.ResourceManagerBuilder</name>
10+
<arguments>
11+
</arguments>
12+
</buildCommand>
13+
<buildCommand>
14+
<name>com.android.ide.eclipse.adt.PreCompilerBuilder</name>
15+
<arguments>
16+
</arguments>
17+
</buildCommand>
18+
<buildCommand>
19+
<name>org.eclipse.jdt.core.javabuilder</name>
20+
<arguments>
21+
</arguments>
22+
</buildCommand>
23+
<buildCommand>
24+
<name>com.android.ide.eclipse.adt.ApkBuilder</name>
25+
<arguments>
26+
</arguments>
27+
</buildCommand>
28+
</buildSpec>
29+
<natures>
30+
<nature>com.android.ide.eclipse.adt.AndroidNature</nature>
31+
<nature>org.eclipse.jdt.core.javanature</nature>
32+
</natures>
33+
</projectDescription>

Location/AndroidManifest.xml

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
3+
package="org.jpn.techbooster.sample.location"
4+
android:versionCode="1"
5+
android:versionName="1.0">
6+
<application android:icon="@drawable/icon" android:label="@string/app_name">
7+
<activity android:name=".LocationActivity"
8+
android:label="@string/app_name">
9+
<intent-filter>
10+
<action android:name="android.intent.action.MAIN" />
11+
<category android:name="android.intent.category.LAUNCHER" />
12+
</intent-filter>
13+
</activity>
14+
15+
</application>
16+
<uses-sdk android:minSdkVersion="4" />
17+
<!-- ワイヤレスネットワークによる位置情報取得許可 -->
18+
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
19+
<!-- GPSによる位置情報取得許可 -->
20+
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
21+
</manifest>

Location/default.properties

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# This file is automatically generated by Android Tools.
2+
# Do not modify this file -- YOUR CHANGES WILL BE ERASED!
3+
#
4+
# This file must be checked in Version Control Systems.
5+
#
6+
# To customize properties used by the Ant build system use,
7+
# "build.properties", and override values to adapt the script to your
8+
# project structure.
9+
10+
# Project target.
11+
target=android-4

Location/res/drawable-hdpi/icon.png

4.05 KB
Loading

Location/res/drawable-ldpi/icon.png

1.68 KB
Loading

Location/res/drawable-mdpi/icon.png

2.51 KB
Loading

Location/res/layout/main.xml

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
3+
android:orientation="vertical"
4+
android:layout_width="fill_parent"
5+
android:layout_height="fill_parent"
6+
>
7+
<TextView
8+
android:id="@+id/ProviderLabel"
9+
android:layout_width="fill_parent"
10+
android:layout_height="wrap_content"
11+
android:text="NONE"
12+
/>
13+
<TextView
14+
android:layout_width="fill_parent"
15+
android:layout_height="wrap_content"
16+
android:text="経度"
17+
/>
18+
<TextView
19+
android:id="@+id/LatitudeLabel"
20+
android:layout_width="fill_parent"
21+
android:layout_height="wrap_content"
22+
android:text="---"
23+
/>
24+
<TextView
25+
android:layout_width="fill_parent"
26+
android:layout_height="wrap_content"
27+
android:text="緯度"
28+
/>
29+
<TextView
30+
android:id="@+id/LongitudeLabel"
31+
android:layout_width="fill_parent"
32+
android:layout_height="wrap_content"
33+
android:text="---"
34+
/>
35+
</LinearLayout>

Location/res/values/strings.xml

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<resources>
3+
4+
<string name="app_name">Location</string>
5+
</resources>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
package org.jpn.techbooster.sample.location;
2+
3+
import android.app.Activity;
4+
import android.content.Context;
5+
import android.os.Bundle;
6+
import android.location.Location;
7+
import android.location.LocationManager;
8+
import android.location.LocationListener;
9+
import android.location.LocationProvider;
10+
import android.util.Log;
11+
import android.widget.TextView;
12+
13+
public class LocationActivity extends Activity implements LocationListener {
14+
private LocationManager locationManager_;
15+
private LocationProvider locationProvider_;
16+
17+
@Override
18+
public void onCreate(Bundle savedInstanceState) {
19+
super.onCreate(savedInstanceState);
20+
setContentView(R.layout.main);
21+
}
22+
23+
@Override
24+
public void onStart() {
25+
super.onStart();
26+
27+
TextView providerLabel = (TextView)findViewById(R.id.ProviderLabel);
28+
29+
// ロケーションマネージャのインスタンスを取得する
30+
locationManager_ = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
31+
32+
// プロバイダにGPSを設定
33+
locationProvider_ = locationManager_.getProvider(LocationManager.GPS_PROVIDER);
34+
35+
if (null == locationProvider_) {
36+
// GPSが使えない場合、ネットワークからの位置情報取得を設定する
37+
locationProvider_ = locationManager_.getProvider(LocationManager.NETWORK_PROVIDER);
38+
if (null == locationProvider_) {
39+
return;
40+
} else {
41+
providerLabel.setText("NETWORK_PROVIDER");
42+
}
43+
} else {
44+
providerLabel.setText("GPS_PROVIDER");
45+
}
46+
47+
// 位置情報の更新を受け取るように設定
48+
locationManager_.requestLocationUpdates(locationProvider_.getName(),
49+
0,
50+
0,
51+
this);
52+
}
53+
54+
@Override
55+
public void onStop() {
56+
super.onStop();
57+
58+
// 位置情報の更新を止める
59+
locationManager_.removeUpdates(this);
60+
}
61+
62+
@Override
63+
public void onLocationChanged(Location location) {
64+
// 位置情報をTextViewに設定する
65+
TextView latitudeLabel = (TextView)findViewById(R.id.LatitudeLabel);
66+
TextView LongitudeLabel = (TextView)findViewById(R.id.LongitudeLabel);
67+
68+
latitudeLabel.setText(Double.toString(location.getLatitude()));
69+
LongitudeLabel.setText(Double.toString(location.getLongitude()));
70+
}
71+
72+
@Override
73+
public void onProviderEnabled(String provider) {
74+
Log.d("LocationActivity","onProviderEnabled");
75+
}
76+
77+
@Override
78+
public void onProviderDisabled(String provider) {
79+
Log.d("LocationActivity","onProviderDisabled");
80+
}
81+
@Override
82+
public void onStatusChanged(String provider, int status, Bundle extras) {
83+
Log.d("LocationActivity","onStatusChanged");
84+
}
85+
}

0 commit comments

Comments
 (0)