Android: Getting device location during runtime - android

I'm developing an app where I want to get the lat,lng of the user's device.I found that I need Access_Fine_Location permission as well as Access_Coarse_Location, I know that I should ask for them during runtime as well as including them in my AndroidManifest.xml, but the app only asks me for the permission ACCESS_COARSE_LOCATION, and ignores asking about ACCESS_FINE_LOCATION.
Here's my code
private boolean checkAndRequestPermissions(){
int permissionReadContacts = ContextCompat.checkSelfPermission(this, Manifest.permission.READ_CONTACTS);
int locationPermission = ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION);
int locationPermission2 =ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION);
List<String> listPermissionsNeeded = new ArrayList<String>();
if (locationPermission != PackageManager.PERMISSION_GRANTED) {
listPermissionsNeeded.add(Manifest.permission.ACCESS_COARSE_LOCATION);
}
if (locationPermission2 != PackageManager.PERMISSION_GRANTED) {
Log.i("addingPerm","addingPerm");
listPermissionsNeeded.add(Manifest.permission.ACCESS_FINE_LOCATION);
}
if (permissionReadContacts != PackageManager.PERMISSION_GRANTED) {
listPermissionsNeeded.add(Manifest.permission.READ_CONTACTS);
}
if (!listPermissionsNeeded.isEmpty()) {
ActivityCompat.requestPermissions(this, listPermissionsNeeded.toArray(new String[listPermissionsNeeded.size()]),REQUEST_ID_MULTIPLE_PERMISSIONS);
return false;
}
return true;
}
and here's the callback:
#Override
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
String TAG = "sonder.SplashActivity";
Log.d(TAG, "Permission callback called-------");
switch (requestCode) {
case REQUEST_ID_MULTIPLE_PERMISSIONS: {
Map<String, Integer> perms = new HashMap<String, Integer>();
// Initialize the map with both permissions
perms.put(Manifest.permission.ACCESS_COARSE_LOCATION, PackageManager.PERMISSION_GRANTED);
perms.put(Manifest.permission.ACCESS_FINE_LOCATION, PackageManager.PERMISSION_GRANTED);
perms.put(Manifest.permission.READ_CONTACTS, PackageManager.PERMISSION_GRANTED);
// Fill with actual results from user
if (grantResults.length > 0) {
for (int i = 0; i < permissions.length; i++)
perms.put(permissions[i], grantResults[i]);
// Check for both permissions
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
LocationListener locationListener = new LocListener();
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 5000, 10, locationListener);
UserData.setLocation(LocListener.getLat() + "," + LocListener.getLon());
Log.i("aaaaaaaaaa", "aaaaaaaaaa");
Log.i("" + LocListener.getLat(), "" + LocListener.getLon());
if (perms.get(Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
Log.i("Ahmed", "Ahmed");
Log.d("Permissions", "location services permission granted");
FacebookSdk.sdkInitialize(getApplicationContext());
Thread myThread = new Thread() {
#Override
public void run() {
try {
Log.i("May", "May");
sleep(300);
Intent intent = new Intent(getApplicationContext(), setup_activity.class);
startActivity(intent);
finish();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
};
myThread.start();
} else {
//If permissions are denied
Log.d(TAG, "Location permission not granted ask again");
Intent intent = new Intent(getApplicationContext(), setup_activity.class);
startActivity(intent);
finish();
}
}
}
}
}
}
And here's my AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<uses-permission android:name="android.permission.AUTHENTICATE_ACCOUNTS" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.USE_CREDENTIALS" />
<uses-permission android:name="android.permission.MANAGE_ACCOUNTS" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permissions.NETWORK_ACCESS" />
<uses-permission android:name="android.permission.READ_CONTACTS" />
<uses-permission android:name="android.permissions.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permissions.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_LOCATION_EXTRA_COMMANDS"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-feature android:name="android.hardware.location.gps"
android:required="true"/>
<uses-feature android:name="android.hardware.location.network"
android:required="true"/>
<uses-feature
android:name="android.hardware.location"
android:required="true" />
<application
android:name=".utils.orientation_adj"
android:allowBackup="true"
android:icon="#drawable/icon"
android:label="#string/app_name"
android:roundIcon="#drawable/icon"
android:theme="#style/AppTheme"
android:largeHeap="true">
<activity
android:name=".home.splashscreen"
android:theme="#style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".home.main_activity"
android:theme="#style/AppTheme.NoActionBar">
</activity>
<activity android:name=".drawer.drawer_preferences"></activity>
<activity
android:name=".setup.edit_profile_tags"
android:theme="#style/DialogNoTitle"></activity>
<activity
android:name=".setup.setup_activity"
android:theme="#style/AppTheme">
</activity>
<activity android:name=".setup.backend_test"></activity>
<activity
android:name=".drawer.drawer_view_profile"
android:theme="#style/AppTheme.NoActionBar"></activity>
<activity android:name=".drawer.drawer_settings"
android:theme="#style/Switch"></activity>
<activity android:name=".drawer.drawer_help"></activity>
<activity android:name=".setup.not_available_in_your_country"
android:theme="#style/AppTheme.NoActionBar">
<!--Add intent filter here-->
</activity>
<activity android:name=".setup.readiness"
android:theme="#style/AppTheme.NoActionBar">
</activity>
<activity android:name=".setup.locationPermission"
android:theme="#style/AppTheme.NoActionBar">
</activity>
<service
android:name=".utils.GenericAccountService"
android:exported="false">
<intent-filter>
<action android:name="android.accounts.AccountAuthenticator" />
</intent-filter>
<meta-data
android:name="android.accounts.AccountAuthenticator"
android:resource="#xml/authenticator" />
</service>
<meta-data
android:name="com.facebook.sdk.ApplicationId"
android:value="#string/facebook_app_id" />
<meta-data
android:name="com.google.android.geo.API_KEY"
android:value="#string/google_maps_key" />
<meta-data
android:name="com.google.android.gms.version"
android:value="#integer/google_play_services_version" />
<meta-data
android:name="io.fabric.ApiKey"
android:value="43a4edcd41e180a4beca5aae4040115cfc1b52fe" />
</application>

Three of your <uses-permission> elements have incorrect permission names. There are no Android SDK permissions that begin with android.permissions (note the s on the end). So, for example, you need to change android.permissions.ACCESS_FINE_LOCATION to android.permission.ACCESS_FINE_LOCATION. You cannot request a runtime permission if the <uses-permission> element is missing or invalid.

Related

requestLocationUpdates is not working on Android 11 & 12

I'm trying to call my latitude & longitude data of the phone every 5 seconds.
This code worked well for Android 9 and 10, but it is not working for 11 and 12.
The initial location is updated but the following location is not updated at all.
FINE and COARSE location permissions are requested. What could be the problem?
if (!isGPSEnabled && !isNetworkEnabled) {
lat = 0;
lon = 0;
} else {
this.isGetLocation = true;
// GPS
if (isGPSEnabled) {
locationManager.requestLocationUpdates(
LocationManager.GPS_PROVIDER,
0,
0, this);
if (locationManager != null) {
location = locationManager
.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if(location != null && location.getTime() > Calendar.getInstance().getTimeInMillis() - 5000) {
gpsUse = true;
}
if (location != null) {
lat = location.getLatitude();
lon = location.getLongitude();
}
}
}
if (isNetworkEnabled) {
locationManager.requestLocationUpdates(
LocationManager.NETWORK_PROVIDER,
0,
0, this);
if (locationManager != null) {
location4 = locationManager
.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
if(location4 != null && location4.getTime() > Calendar.getInstance().getTimeInMillis() - 5000) {
networkUse = true;
}
if (location4 != null) {
lat = location4.getLatitude();
lon = location4.getLongitude();
}
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
I added the code of AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="com.HanyangHCI.crc_test">
<!-- <uses-permission android:name="android.permission.INTERNET"/>-->
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
<uses-permission android:name="android.permission.KILL_BACKGROUND_PROCESSES" />
<uses-permission
android:name="android.permission.WRITE_EXTERNAL_STORAGE"
android:maxSdkVersion="29" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.VIBRATE"/>
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS"/>
<uses-permission android:name="android.permission.ACCESS_BACKGROUND_SERVICE" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<!-- <uses-permission android:name="android.permission.ACCESS_BACKGROUND_LOCATION" />-->
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
<uses-permission
android:name="android.permission.MANAGE_EXTERNAL_STORAGE"
tools:ignore="ScopedStorage" />
<application
android:allowBackup="true"
android:icon="#mipmap/ic_hci"
android:label="#string/app_name"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="#style/AppTheme"
android:requestLegacyExternalStorage="true"
tools:ignore="GoogleAppIndexingWarning">
<activity android:name=".MainActivity"
android:screenOrientation="portrait">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name = ".Collecting"
android:screenOrientation="portrait" />
<service android:name=".Collecting"
android:foregroundServiceType="location"/>
<activity android:name=".Survay"
android:screenOrientation="portrait"/>
<activity android:name=".Survay2"
android:screenOrientation="portrait"/>
<activity android:name=".PopUp"
android:theme="#android:style/Theme.Dialog"
android:excludeFromRecents="true" />
<service android:name=".BackGroundCollecting"
android:enabled="true"
android:stopWithTask="false"
android:foregroundServiceType="location">
<intent-filter>
<action android:name="com.HanyangHCI.crc_test.BackGroundCollecting"/>
</intent-filter>
</service>
<service android:name=".GPSTracker"
android:foregroundServiceType="location">
<intent-filter>
<action android:name="com.HanyangHCI.crc_test.GPSTracker"/>
</intent-filter>
</service>
<receiver android:name="com.HanyangHCI.crc_test.RestartService" android:process=":remote"/>
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="com.hanyangHCI.crc_test.fileprovider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="#xml/filepaths" />
</provider>
</application>
</manifest>
It's maybe helping you
https://developer.android.com/training/location/permissions
p/s: Upload some image of AndroidManifest

eaccess permission denied even with permissions granted

I created a simple test app where I decode a file path into a Bitmap. But even if the permissions are granted it still gives permission denied into the console.
Here's the main activity code:
public class MainActivity extends AppCompatActivity {
public static final String TAG = "DEBUG_";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ImageView imageView = findViewById(R.id.image_view);
if (ContextCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED ||
ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) {
imageView.setImageBitmap(BitmapFactory.decodeFile("/storage/emulated/0/DCIM/Camera/IMG_20200118_175558.jpg"));
}
}
}
And here's the manifest file:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.frakm.imagepickertesting">
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.CAMERA" />
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:roundIcon="#mipmap/ic_launcher_round"
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>

How to access SYSTEM_ALERT_WINDOW permission run time?

This is my code for reference. I have added the run time permission code and system granting the permission but again it gives an error:
Failed due to non declaration of following android.permission.SYSTEM_ALERT_WINDOW permission in Manifest file
public boolean checkDrawOverlayPermission()
{
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M)
{
return true;
}
if (!Settings.canDrawOverlays(this))
{
Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION,
Uri.parse("package:" + getPackageName()));
startActivityForResult(intent, REQUEST_CODE);
return false;
}
else
{
return true;
}
}
#Override
#TargetApi(Build.VERSION_CODES.M)
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
if (requestCode == REQUEST_CODE)
{
if (Settings.canDrawOverlays(this))
{
//startService(new Intent(this, PowerButtonService.class));
//Toast.makeText(this, "permission granted", Toast.LENGTH_SHORT).show();
if(!checkPermission())
{
requestPermission();
}
else
{
//Toast.makeText(this, "Phone State Permission Already Granted", Toast.LENGTH_SHORT).show();
//callBeacon();
}
}
}
}
Manifest File :
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.adsl.beaconapp">
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
<uses-permission android:name="android.permission.INTERACT_ACROSS_USERS_FULL" />
<!-- Declaration that this app is usable on phones with Bluetooth Low Energy. -->
<uses-feature
android:name="android.hardware.bluetooth_le"
android:required="true" />
<!-- Declaration permission for using internet -->
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity
android:name=".MainActivity"
android:label="#string/app_name"
android:theme="#style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<meta-data
android:name="com.facebook.sdk.ApplicationId"
android:value="#string/app_id" />
<!-- <activity android:name="com.facebook.LoginActivity" /> -->
<activity
android:name="com.beaconstreamsdk.com.MainActivity"
android:screenOrientation="portrait"
android:theme="#android:style/Theme.Black.NoTitleBar" />
<service
android:name="com.beaconstreamsdk.com.service.BeaconService"
android:enabled="true"
android:exported="false" />
<service
android:name="com.beaconstreamsdk.com.service.FloatingHeadService"
android:enabled="true"
android:exported="false" />
<service
android:name="com.beaconstreamsdk.com.service.ConnectionService"
android:enabled="true"
android:exported="false" />
<activity
android:name="com.beaconstreamsdk.com.SignUpActivity"
android:screenOrientation="portrait" />
<activity android:name=".Main2Activity" />
<activity android:name=".BeaconStreamActivity"></activity>
</application>
</manifest>
onActivityResult :
#Override
#TargetApi(Build.VERSION_CODES.M)
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
if (requestCode == REQUEST_CODE)
{
if (Settings.canDrawOverlays(this))
{
//startService(new Intent(this, PowerButtonService.class));
//Toast.makeText(this, "permission granted", Toast.LENGTH_SHORT).show();
if(!checkPermission())
{
requestPermission();
}
else
{
Toast.makeText(this, "Phone State Permission Already Granted", Toast.LENGTH_SHORT).show();
//callBeacon();
}
}
}
}
Please try this way. May be you are facing condition problem,
if (Build.VERSION.SDK_INT > Build.VERSION_CODES.M)
{
if (!Settings.canDrawOverlays(this))
{
Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION,
Uri.parse("package:" + getPackageName()));
startActivityForResult(intent, REQUEST_CODE);
return false;
}
else
{
return true;
}
}else{
return true;
}

setMyLocation Button will not pop up when application loads

I'm trying to display the basic setMyLocation Button when my application loads the map but every time I do so the application crashes and says java.lang.SecurityException: my location requires permission ACCESS_FINE_LOCATION or ACCESS_COARSE_LOCATION
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3288) I have the necessary permissions included in the manifest
public class MainActivity extends AppCompatActivity {
public GoogleMap map;
Location location;
//double lat, longit;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
map = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap();
map.setMyLocationEnabled(true);
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
// TODO: Consider calling
// ActivityCompat#requestPermissions
// here to request the missing permissions, and then overriding
// public void onRequestPermissionsResult(int requestCode, String[] permissions,
// int[] grantResults)
// to handle the case where the user grants the permission. See the documentation
// for ActivityCompat#requestPermissions for more details.
return;
}
}
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.cnit425.patrickwalker.assignment2">
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission
android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<!-- GoogleMap API v2 Permission -->
<uses-permission android:name="org.smart_laboratory.laptop.locationmaptest.permission.MAPS_RECEIVE" />
<!-- GoogleMap API v2 Location related Permission -->
<uses-permission android:name="android.permission.INTERNET" />
<!-- GPS & Storage Permission -->
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<!-- OpenGL Use Setting -->
<uses-feature android:glEsVersion="0x00020000" android:required="true" />
<uses-library android:name="com.google.android.maps"/>
<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="AIzaSyCMmSB3RP5MOu7WqZgBBh5jpz-LXy6gQDo"/>
<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>

To obtain the current location

I am using the below code to obtain the location of a user
public class MainLocation extends Application implements LocationListener {
protected LocationManager locationManager;
protected LocationListener locationListener;
protected Context context;
String lat;
String provider;
protected String latitude, longitude;
protected boolean gps_enabled, network_enabled;
#Override
public void onCreate() {
super.onCreate();
Log.e("Location", "here");
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (checkSelfPermission(Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && checkSelfPermission(Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
// TODO: Consider calling
// public void requestPermissions(#NonNull String[] permissions, int requestCode)
// here to request the missing permissions, and then overriding
// public void onRequestPermissionsResult(int requestCode, String[] permissions,
// int[] grantResults)
// to handle the case where the user grants the permission. See the documentation
// for Activity#requestPermissions for more details.
return;
}
}
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);
}
#Override
public void onLocationChanged(Location location) {
Log.e("Location", "" + location.getLatitude());
}
#Override
public void onStatusChanged(String provider, int status, Bundle extras) {
Log.e("Location Status", "" + provider + " " + status + " " + extras);
}
#Override
public void onProviderEnabled(String provider) {
Log.e("Location", "Enabled");
}
#Override
public void onProviderDisabled(String provider) {
Log.e("Location","Disabled"+provider);
}
}
but here the onLocationChanged function is never getting called, so i m not able fetch the location. So how can i get the location?
In the logcat, the info tab is showing the latitude and longitude under the title onLocationReceived".
This is the manifest,
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="com.example.krijjj.loginapp" >
<!--
The ACCESS_COARSE/FINE_LOCATION permissions are not required to use
Google Maps Android API v2, but are recommended.
-->
<uses-sdk android:minSdkVersion="15" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<application
android:name=".Location.MainLocation"
android:allowBackup="true"
android:icon="#drawable/icon1"
android:label="#string/app_name"
android:theme="#style/AppTheme"
tools:replace="android:label" >
<activity
android:name=".Track"
android:label="Track" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".Security.Lockscreen"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name=".Second"
android:label="#string/title_activity_second" >
<intent-filter>
<action android:name="com.example.krijjj.loginapp.Second" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<meta-data
android:name="com.google.android.gms.version"
android:value="#integer/google_play_services_version" />
<activity
android:name=".MainActivity"
android:label="#string/title_activity_mapactivity" >
</activity>
<activity
android:name=".Security.MainActivity"
android:label="#string/title_activity_mapactivity" >
</activity>
<!--
The API key for Google Maps-based APIs is defined as a string resource.
(See the file "res/values/google_maps_api.xml").
Note that the API key is linked to the encryption key used to sign the APK.
You need a different API key for each encryption key, including the release key that is used to
sign the APK for publishing.
You can define the keys for the debug and release targets in src/debug/ and src/release/.
-->
<meta-data
android:name="com.google.android.geo.API_KEY"
android:value="#string/google_maps_key" />
<activity
android:name=".MapsActivity"
android:label="#string/title_activity_maps" >
</activity>
</application>
</manifest>
The Google Play services location APIs are preferred over the Android framework location APIs (android.location) as a way of adding location awareness to your app.
You can find the example how to use it right here.
P.S. Don't forget to check if permissions granted on your device, if you added permissions into manifest file...

Categories

Resources