Skip to content

Instantly share code, notes, and snippets.

@shikajiro
Created October 7, 2013 12:16
Show Gist options
  • Save shikajiro/6866888 to your computer and use it in GitHub Desktop.
Save shikajiro/6866888 to your computer and use it in GitHub Desktop.
位置情報取得
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.gpsexample"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<uses-permission
android:name="android.permission.ACCESS_FINE_LOCATION" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.gpsexample.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
package com.example.gpsexample;
import java.util.List;
import android.app.Activity;
import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
public class MainActivity extends Activity implements LocationListener {
private LocationManager mLocationManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mLocationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
// Object systemService = getSystemService(Context.LOCATION_SERVICE);
List<String> allProviders = mLocationManager.getAllProviders();
for (String provider : allProviders) {
Log.i("provider", provider);
mLocationManager.requestLocationUpdates(
provider,
10 * 1000,
0,
this);
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public void onLocationChanged(Location location) {
Log.i("location",
location.getLatitude() + "," + location.getLongitude());
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
@Override
public void onProviderEnabled(String provider) {
}
@Override
public void onProviderDisabled(String provider) {
}
@Override
protected void onPause() {
super.onPause();
mLocationManager.removeUpdates(this);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment