To obtain the current location - android

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...

Related

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>

Android: HERE map positioning manager getposition() always return Lat: 17976931348623200..,Lon: 17976931348623200

I am new to HERE maps currently trying to get current user location from Positioning manager , getlocation() method, but my code always return below information
com.nokia.maps.GeoPositionImpl[coordinate=GeoCoordinate [Latitude=-179769313486232000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000.000000 Longitude=-179769313486232000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000.000000 Altitude=1073741824.000000 Valid=FALSE]]
Below is my code:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//requestPermissions();
// Search for the Map Fragment
final MapFragment mapFragment = (MapFragment)
getFragmentManager().findFragmentById(R.id.mapfragment);
// initialize the Map Fragment and
// retrieve the map that is associated to the fragment
mapFragment.init(new OnEngineInitListener() {
#Override
public void onEngineInitializationCompleted(
OnEngineInitListener.Error error) {
if (error == OnEngineInitListener.Error.NONE) {
// now the map is ready to be used
map = mapFragment.getMap();
onMapFragmentInitializationCompleted();
} else {
System.out.println("ERROR: Cannot initialize MapFragment");
System.out.println("ERROR: Cannot initialize Map Fragment: " + error.toString());
}
}
});
}
private void onMapFragmentInitializationCompleted() {
PositioningManager posManager;
MapContainer placesContainer = null;
// retrieve a reference of the map from the map fragment
// map = mapFragment.getMap();
// start the position manager
posManager = PositioningManager.getInstance();
posManager.start(PositioningManager.LocationMethod.GPS_NETWORK);
GeoPosition position = posManager.getPosition();
GeoCoordinate cor = position.getCoordinate();
// Set a pedestrian friendly map scheme
map.setMapScheme(Map.Scheme.PEDESTRIAN_DAY);
// Display position indicator
map.getPositionIndicator().setVisible(true);
placesContainer = new MapContainer();
map.addMapObject(placesContainer);
// Set the map center coordinate to the current position
map.setCenter(posManager.getPosition().getCoordinate(), Map.Animation.NONE);
map.setZoomLevel(14);
}
Manifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.benz.event.navigation">
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="navigation"
android:hardwareAccelerated="true"
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>
<meta-data android:name="com.here.android.maps.appid" android:value="MY ID"/>
<meta-data android:name="com.here.android.maps.apptoken" android:value="MY TOKEN"/>
<meta-data android:name="com.here.android.maps.license.key" android:value="MY KEY"/>
<!--
Embed the HERE Map Service.
For more information, see the HERE SDK Developer's Guide
-->
<service
android:name="com.here.android.mpa.service.MapService"
android:label="HereMapService"
android:process="global.Here.Map.Service.v2"
android:exported="true" >
<intent-filter>
<action android:name="com.here.android.mpa.service.MapService" >
</action>
</intent-filter>
</service>
</application>
</manifest>
Any help/hint will save my day
Thanks
Valid=FALSE Implies your device does not have a GPS lock. Is your device indoors and unable to obtain a position? Second, please check to see if your application has the positioning permission.
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION " />

"network" location provider requires ACCESS_COARSE_LOCATION or ACCESS_FINE_LOCATION permission in GPS Location App

I have a problem with my GPS Location App I have tried by all means and I can't find the solution (I'm developing in Android 6.0) , this is the problem:
"network" location provider requires ACCESS_COARSE_LOCATION or ACCESS_FINE_LOCATION permission
this is my XML FIle:
<?xml version="1.0" encoding="utf-8"?>
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_LOCATION_EXTRA_COMMANDS" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<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_LOGS" />
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-feature android:name="android.hardware.location.gps" />
<uses-feature android:name="android.hardware.camera" />
<uses-feature android:name="android.hardware.camera.autofocus" />
<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>
<activity android:name=".chooseEmergency"></activity>
</application>
And this is my LocationManager Code:
LocationManager mlocManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
Localizacion Local = new Localizacion();
Local.setMainActivity(this);
mlocManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0,
(LocationListener) Local);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
//use checkSelfPermission()
if (checkSelfPermission(Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED
&& checkSelfPermission(Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
return;
}
} else {
//simply use the required feature
//as the user has already granted permission to them during installation
}
ActivityCompat.requestPermissions(this,new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, 1);
ActivityCompat.requestPermissions(this,new String[]{Manifest.permission.ACCESS_COARSE_LOCATION}, 1);
public class Localizacion implements LocationListener {
MainActivity mainActivity;
public MainActivity getMainActivity() {
return mainActivity;
}
public void setMainActivity(MainActivity mainActivity) {
this.mainActivity = mainActivity;
}
#Override
public void onLocationChanged(Location loc) {
latitud = loc.getLatitude();
longitud = loc.getLongitude();
this.mainActivity.setLocation(loc);
}
#Override
public void onProviderDisabled(String provider) {//pndiente de escribir;
}
#Override
public void onProviderEnabled(String provider) {//pendiente
}
#Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
}
Please help, I took 2 days looking at internet solutions and nothing works for me

Application class recreated each time the screen goes on

The steps that explains the problem I faced are below;
1. I start the android application.
2. I put it on backgorund by pressing home button.
3. I turn the screen off.
4. I turn the screen on after some seconds.
5. My application suddenly becomes foreground. I realised that the application is forced to start again and the application class that I used is created again and then the start page of my application (this is login page) is shown although what page is opened before the step 2.
What I have tried so far is below;
I put configChanges and screenOrientation in all the activiy tags in my Androidmanifest.xml (I tried to put them one by one and both together also.)
<activity android:configChanges="orientation|keyboardHidden|screenSize"
android:name="com.myproject.Activity"
android:screenOrientation="nosensor"/>
I also tried to use screenOrientation="portrait" but didn't work.
My problem is related with the How do I disable orientation change on Android?, Prevent Android activity from being recreated on turning screen off and like others, but I could not find the solution of my problem yet.
Can you please help?
Edit 1: ********************************
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.myproject"
android:versionCode="1"
android:versionName="1.0"
xmlns:tools="http://schemas.android.com/tools" >
<uses-sdk android:minSdkVersion="14" android:targetSdkVersion="21" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.CALL_PHONE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_SETTINGS" />
<!-- Permission to use NFC -->
<uses-permission android:name="android.permission.NFC" />
<uses-feature
android:name="android.hardware.nfc.hce"
android:required="true" />
<!-- Keeps the processor from sleeping when a message is received. -->
<uses-permission android:name="android.permission.DISABLE_KEYGUARD" />
<permission android:name="com.myproject.permission.C2D_MESSAGE"
android:protectionLevel="signature" />
<uses-permission android:name="com.myproject.permission.C2D_MESSAGE" />
<uses-feature android:name="android.hardware.camera" />
<uses-permission android:name="android.permission.CAMERA" />
<!--
Add this permission to check which network access properties (e.g.
active type: 3G/WiFi).
-->
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<!-- Add this permission to access WLAN MAC address. -->
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<!-- Add this permission to access HW ID. -->
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<meta-data
android:name="com.google.android.gms.version"
android:value="#integer/google_play_services_version" />
<application
android:name="com.myproject.hce.MyApplication"
android:hardwareAccelerated="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:largeHeap="true"
android:theme="#style/MyAppTheme"
tools:replace="android:icon,android:theme"
>
<!-- Splash Activity -->
<activity android:configChanges="orientation|keyboardHidden|screenSize"
android:name=".Splash"
android:theme="#android:style/Theme.Holo.Light.NoActionBar"
android:windowSoftInputMode="stateHidden"
android:screenOrientation="nosensor" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<!-- Login Activity -->
<activity android:configChanges="orientation|keyboardHidden|screenSize"
android:name="com.myproject.LoginView"
android:screenOrientation="nosensor"
android:windowSoftInputMode="adjustResize" >
</activity>
<activity android:configChanges="orientation|keyboardHidden|screenSize"
android:name="com.myproject.MainView"
android:screenOrientation="nosensor" >
</activity>
<meta-data
android:name="com.facebook.sdk.ApplicationId"
android:value="#string/fb_app_id" />
<meta-data
android:name="com.crashlytics.ApiKey"
android:value="12h3g21h4v32hv43hv4" />
<service
android:name="com.myproject.pushnotifications.GCMIntentService"
android:enabled="true" >
</service>
<service
android:name="com.myproject.hce.McbpHceService"
android:exported="true"
android:permission="android.permission.BIND_NFC_SERVICE" >
<intent-filter>
<action android:name="android.nfc.cardemulation.action.HOST_APDU_SERVICE" />
</intent-filter>
<meta-data
android:name="android.nfc.cardemulation.host_apdu_service"
android:resource="#xml/apduservice" />
</service>
<receiver
android:name="com.myproject.pushnotifications.GcmBroadcastReceiver"
android:permission="com.google.android.c2dm.permission.SEND" >
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RETRY" />
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
</intent-filter>
</receiver>
<service
android:name="com.myproject.hce.MyService"
android:exported="true"
android:enabled="true"
android:stopWithTask="false"/>
</application>
</manifest>
Application class
public class MyApplication extends Application {
private static MyApplication MyApplication;
private MyActivityLifeCycleCallbacks MyActivityLifeCycleCallbacks;
public static MyApplication getMyApplication() {
return MyApplication;
}
#Override
public void onCreate() { //coko1
MyLog.i(UtilConstants.LOG_TAG_HCE, "MyApplication " + "onCreate ...");
if (MyApplication == null)
MyApplication = this;
else
return;
MyLog.i(UtilConstants.LOG_TAG_HCE, "MyApplication " + "super.onCreate() ...");
super.onCreate();
registerActivityLifecycleCallbacks(new MyLifecycleHandler());
setMyActivityLifeCycleCallbacks(new MyActivityLifeCycleCallbacks());
registerActivityLifecycleCallbacks(getMyActivityLifeCycleCallbacks());
}
public MyActivityLifeCycleCallbacks getMyActivityLifeCycleCallbacks() {
return MyActivityLifeCycleCallbacks;
}
public void setMyActivityLifeCycleCallbacks(
MyActivityLifeCycleCallbacks MyActivityLifeCycleCallbacks) {
this.MyActivityLifeCycleCallbacks = MyActivityLifeCycleCallbacks;
}
}
Edit 2:****************
Splash.java
public class Splash extends Activity {
public static Context context;
public static final String LOG_TAG = "Splash ";
#Override
public void onCreate(Bundle bundle)
{
MyLog.i(UtilConstants.LOG_TAG_HCE, LOG_TAG + "onCreate...");
Thread.setDefaultUncaughtExceptionHandler(new ExceptionHandler(this)); // catch unexpected error
super.onCreate(bundle);
context = this;
MyLog.i(UtilConstants.LOG_TAG_HCE, LOG_TAG + "MainView starts...");
Intent mainIntent = new Intent(Splash.this, MainView.class);
Splash.this.startActivity(mainIntent);
Splash.this.finish();
}
#Override
protected void onResume() {
MyLog.i(UtilConstants.LOG_TAG_HCE, LOG_TAG + "onResume ...");
super.onResume();
}
}
MainView.class
public class MainView extends Activity {
Context context;
String LOG_TAG = "MainView ";
#Override
public void onCreate(Bundle savedInstanceState)
{
MyLog.i(UtilConstants.LOG_TAG_HCE, LOG_TAG + "onCreate ...");
super.onCreate(savedInstanceState);
Thread.setDefaultUncaughtExceptionHandler(new ExceptionHandler(this));
context = this;
MyLog.i(UtilConstants.LOG_TAG_HCE, LOG_TAG + "LoginView starts...");
Intent loginIntent = new Intent(Splash.this, LoginView.class);
Splash.this.startActivity(loginIntent);
Splash.this.finish();
}
#Override
protected void onResume() {
MyLog.i(UtilConstants.LOG_TAG_HCE, LOG_TAG + "onResume ...");
super.onResume();
}
#Override
protected void onStart() {
super.onStart();
MyLog.i(UtilConstants.LOG_TAG_HCE, LOG_TAG + "onStart ...");
};
#Override
protected void onPause() {
super.onPause();
MyLog.i(UtilConstants.LOG_TAG_HCE, LOG_TAG + "onPause ...");
};
#Override
protected void onDestroy() {
MyLog.i(UtilConstants.LOG_TAG_HCE, LOG_TAG + "onDestroy ...");
super.onDestroy();
};
}
Edit 3:****************
I use fragments, maybe this will be the reason, but I am not sure.
The problem is due to a library I used. It works in background and makes the application restarts.
I suspend it when the application goes to background and resume it when it comes foregorund. That is it, the problem is solved.
Thanks you anyway.

How to get user location continuosly without GPS

There are some questions regarding this issue but I could not find any method works without problem. I implemented the version of the project with GPS provider and now I want to implement a version which can find the location of user without GPS, just using network provider such as Wifi. I tried this but it is not working (I could not get the "done" message in any way):
public class MapsActivity extends FragmentActivity {
double latitude;
double longitude;
LocationManager locationManager;
android.location.LocationListener networkLocationListener;
Location location;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
networkLocationListener = new android.location.LocationListener() {
public void onLocationChanged(Location location) {
latitude = location.getLatitude();
longitude = location.getLongitude();
Log.d("->", "done.");
}
public void onStatusChanged(String provider, int status, Bundle extras) {}
public void onProviderEnabled(String provider) {}
public void onProviderDisabled(String provider) {}
};
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, networkLocationListener);
}
...
Also, this is my manifest file:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.doruk.myapplication">
<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" />
<!--
The ACCESS_COARSE/FINE_LOCATION permissions are not required to use
Google Maps Android API v2, but are recommended.
-->
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<application
android:allowBackup="true"
android:icon="#mipmap/orange"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<meta-data
android:name="com.google.android.gms.version"
android:value="#integer/google_play_services_version" />
<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="#string/google_maps_key" />
<activity
android:name=".MapsActivity"
android:label="#string/title_activity_maps" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver
android:name=".ConnectionChangeReceiver"
android:label="NetworkConnection">
<intent-filter>
<action android:name="android.net.conn.CONNECTIVITY_CHANGE"/>
</intent-filter>
</receiver>
</application>
</manifest>
Can you see the problem? What you advise at this point? Is there a better way?
Dorukhan Arslan
- You can use GoogleclientAPi, as locationmanager has been
Deprecated
-Using FusedLocationAPi it will automatically discover the provider to find the location.
- https://developer.android.com/google/auth/api-client.html have a look
on this
- Here is one exampleprotected void createLocationRequest() {
LocationRequest mLocation = LocationRequest.create();
GoogleAPiClient mgoogle;
/*enter code here
* Set the update interval
*/
mLocation.setSmallestDisplacement(500); // 2km
mLocation.setInterval(180000);// 3 min
mLocationRequest.setFastestInterval(120000);// 2 min
mLocation.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
if (mgoogle == null) {
mgoogle = new GoogleApiClient.Builder(this)
.addApi(LocationServices.API).addConnectionCallbacks(this)
.addOnConnectionFailedListener(this).build();
mgoogle.connect();
}
}
It's a sample and implementing OnConnectionFailedListener,
ConnectionCallbacks, com.google.android.gms.location.LocationListener above 3 interfaces you can get the continuos Location.
getLastLocation Method Of LocationRequest Class will give you Location.
onLocationChanged Method will give you Your desired output.
You can use IP address of user for getting approx location.

Categories

Resources