Last active
August 29, 2015 14:26
-
-
Save t-kashima/b9ddaf1ac76cafcf2cfe to your computer and use it in GitHub Desktop.
Get my location in Android
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<manifest xmlns:android="http://schemas.android.com/apk/res/android" | |
package="com.unuuu.location" > | |
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/> | |
<uses-permission android:name="android.permission.INTERNET" /> | |
<application | |
android:allowBackup="true" | |
android:icon="@mipmap/ic_launcher" | |
android:label="@string/app_name" | |
android:theme="@style/AppTheme" > | |
<activity | |
android:name=".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> | |
<meta-data | |
android:name="com.google.android.gms.version" | |
android:value="@integer/google_play_services_version" /> | |
</application> | |
</manifest> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
apply plugin: 'com.android.application' | |
android { | |
compileSdkVersion 22 | |
buildToolsVersion "22.0.1" | |
defaultConfig { | |
applicationId "com.unuuu.location" | |
minSdkVersion 14 | |
targetSdkVersion 22 | |
versionCode 1 | |
versionName "1.0" | |
} | |
buildTypes { | |
release { | |
minifyEnabled false | |
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' | |
} | |
} | |
} | |
dependencies { | |
compile fileTree(dir: 'libs', include: ['*.jar']) | |
compile 'com.android.support:appcompat-v7:22.2.0' | |
compile 'com.google.android.gms:play-services:7.5.+' | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.unuuu.location; | |
import android.location.Location; | |
import android.os.Bundle; | |
import android.support.v4.app.Fragment; | |
import android.util.Log; | |
import android.view.LayoutInflater; | |
import android.view.View; | |
import android.view.ViewGroup; | |
import com.google.android.gms.common.ConnectionResult; | |
import com.google.android.gms.common.GooglePlayServicesUtil; | |
import com.google.android.gms.common.api.GoogleApiClient; | |
import com.google.android.gms.location.LocationListener; | |
import com.google.android.gms.location.LocationRequest; | |
import com.google.android.gms.location.LocationServices; | |
import java.text.DateFormat; | |
import java.util.Date; | |
/** | |
* A placeholder fragment containing a simple view. | |
*/ | |
public class MainFragment extends Fragment implements GoogleApiClient.ConnectionCallbacks, | |
GoogleApiClient.OnConnectionFailedListener, | |
LocationListener { | |
private GoogleApiClient mGoogleApiClient; | |
private LocationRequest mLocationRequest; | |
private Location mCurrentLocation; | |
private static String TAG = "Location"; | |
@Override | |
public View onCreateView(LayoutInflater inflater, ViewGroup container, | |
Bundle savedInstanceState) { | |
View rootView = inflater.inflate(R.layout.fragment_main, container, false); | |
if (!isGooglePlayServicesAvailable()) { | |
getActivity().finish(); | |
} | |
this.mLocationRequest = LocationRequest.create(); | |
this.mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY); | |
this.mLocationRequest.setInterval(5000); | |
this.mLocationRequest.setFastestInterval(1000); | |
this.mGoogleApiClient = new GoogleApiClient.Builder(getActivity().getApplicationContext()) | |
.addApi(LocationServices.API) | |
.addConnectionCallbacks(this) | |
.addOnConnectionFailedListener(this) | |
.build(); | |
return rootView; | |
} | |
@Override | |
public void onStart() { | |
super.onStart(); | |
this.mGoogleApiClient.connect(); | |
} | |
@Override | |
public void onStop() { | |
this.mGoogleApiClient.disconnect(); | |
super.onStop(); | |
} | |
@Override | |
public void onPause() { | |
this.stopLocationUpdates(); | |
super.onPause(); | |
} | |
protected void stopLocationUpdates() { | |
LocationServices.FusedLocationApi.removeLocationUpdates( | |
this.mGoogleApiClient, this); | |
Log.d(TAG, "Location update stopped ......................."); | |
} | |
@Override | |
public void onResume() { | |
super.onResume(); | |
if (mGoogleApiClient.isConnected()) { | |
startLocationUpdates(); | |
Log.d(TAG, "Location update resumed ....................."); | |
} | |
} | |
@Override | |
public void onConnected(Bundle bundle) { | |
Log.d(TAG, "onConnected"); | |
this.startLocationUpdates(); | |
} | |
protected void startLocationUpdates() { | |
LocationServices.FusedLocationApi.requestLocationUpdates( | |
this.mGoogleApiClient, this.mLocationRequest, this); | |
Log.d(TAG, "Location update started ..............: "); | |
} | |
private boolean isGooglePlayServicesAvailable() { | |
int status = GooglePlayServicesUtil.isGooglePlayServicesAvailable(getActivity().getApplicationContext()); | |
if (ConnectionResult.SUCCESS == status) { | |
return true; | |
} else { | |
GooglePlayServicesUtil.getErrorDialog(status, getActivity(), 0).show(); | |
return false; | |
} | |
} | |
@Override | |
public void onLocationChanged(Location location) { | |
Log.d(TAG, "onLocationChanged"); | |
this.mCurrentLocation = location; | |
if (null != mCurrentLocation) { | |
String lat = String.valueOf(mCurrentLocation.getLatitude()); | |
String lng = String.valueOf(mCurrentLocation.getLongitude()); | |
String lastUpdateTime = DateFormat.getTimeInstance().format(new Date()); | |
Log.d(TAG, "At Time: " + lastUpdateTime + "\n" + | |
"Latitude: " + lat + "\n" + | |
"Longitude: " + lng + "\n" + | |
"Accuracy: " + mCurrentLocation.getAccuracy() + "\n" + | |
"Provider: " + mCurrentLocation.getProvider()); | |
} else { | |
Log.d(TAG, "location is null ..............."); | |
} | |
} | |
@Override | |
public void onConnectionSuspended(int i) { | |
Log.d(TAG, "onConnectionSuspended"); | |
} | |
@Override | |
public void onConnectionFailed(ConnectionResult connectionResult) { | |
Log.d(TAG, "Connection failed: " + connectionResult.toString()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment