Last active
June 8, 2016 02:21
-
-
Save talhahasanzia/caaf91b35592bc28cd130b98cf3e97c4 to your computer and use it in GitHub Desktop.
Location service initialization 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.company.product.package"> | |
<uses-permission android:name="android.permission.ACCESS_COARSE_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:supportsRtl="true" | |
android:theme="@style/AppTheme"> | |
<activity android:name=".MainActivity"> | |
<intent-filter> | |
<action android:name="android.intent.action.MAIN" /> | |
<category android:name="android.intent.category.LAUNCHER" /> | |
</intent-filter> | |
</activity> | |
</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
public class MainActivity extends AppCompatActivity implements LocationListener{ | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_main); | |
LocationManager lm = (LocationManager) | |
getSystemService(Context.LOCATION_SERVICE);; | |
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { | |
Toast.makeText(MainActivity.this,"GPS Disabled, enable GPS and allow app to use the service.",Toast.LENGTH_LONG).show(); | |
} | |
else { | |
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 1, this); // Change GPS provider with other location provider like Network based locaiton | |
} | |
} | |
//_________________________________________________________________________________________________________________ | |
@Override | |
public void onLocationChanged(Location location) { | |
double Latitude=location.getLatitude(); | |
double Longitude=location.getLongitude(); | |
String currentLocation="Lat: "+Latitude+", Lng: "+Longitude; | |
Log.d("Current Location",currentLocation); | |
} | |
@Override | |
public void onStatusChanged(String provider, int status, Bundle extras) { | |
} | |
@Override | |
public void onProviderEnabled(String provider) { | |
} | |
@Override | |
public void onProviderDisabled(String provider) { | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment