First I apologize for the duplication, I know this question was asked and I tried to follow up the existing answers, but it wont work. So if you can please see my code and say what is wrong or missing, that would help me a lot.
Here's the activity, it uses only the Log (no GUI).
import android.app.Activity;
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 lManager;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lManager = (LocationManager) getSystemService(LOCATION_SERVICE);
if (lManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER))
lManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,
0, 0, this);
else
Log.i("Test", "network provider unavailable");
Location lastKnownLocation =
lManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
if (lastKnownLocation != null) {
Log.i("Test", lastKnownLocation.getLatitude() + ", "
+ lastKnownLocation.getLongitude());
lManager.removeUpdates(this);
} else
Log.i("Test", "null");
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
public void onLocationChanged(Location location) {
Log.i("Test", "location changed: " + location.getLatitude() + ", "
+ location.getLongitude());
}
public void onProviderDisabled(String provider) {
}
public void onProviderEnabled(String provider) {
}
public void onStatusChanged(String provider, int status, Bundle extras) {
}
}
when I try to run the app everything seems to work but latitude and longitude wont show on the log. I am using a real device with network location on for testing.
The reason I couldn't get the location is the cellular company does not give that information (NETWORK_PROVIDER uses cellular antenna triangulation to determine location. Cellular company can provide or deny you this information).
Related
i recently asked a question for implementation of toast message when the GPS is not enabled.
of this i knew it was possible.
Is it possible to automatically activate the GPS function in the background.
When i remotely activate the application in the background. can i activate the GPS from the phone itself aswell without the other phone reporting anything to the user ?
the situation is like this.
we have a project named ParentalControl Application. So a parent has to be able to track its child remotely.
does anyone have suggestion or ideas ... ?
if it is of anyhelp
this is the code i'm using at the moment.
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.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity
{
TextView TxtLat;
TextView TxtLong;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TxtLat = (TextView) findViewById(R.id.TxtLat);
TxtLong = (TextView) findViewById(R.id.TxtLong);
LocationManager lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
// implementation of TOASTS
if (!lm.isProviderEnabled(LocationManager.GPS_PROVIDER))
{
Toast toast= Toast.makeText(MainActivity.this, " You can improve the accuracy by turning on the GPS", Toast.LENGTH_SHORT);
toast.show();
}
LocationListener ll = new myLocationListener();
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, ll);
}
class myLocationListener implements LocationListener
{
#Override
public void onLocationChanged(Location location)
{
if (location != null)
{
double pLong = location.getLongitude();
double pLat = location.getLatitude();
TxtLat.setText(Double.toString(pLat));
TxtLong.setText(Double.toString(pLong));
}
}
#Override
public void onStatusChanged(String s, int i, Bundle bundle) {
}
#Override
public void onProviderEnabled(String s) {
}
#Override
public void onProviderDisabled(String s) {
}
}
}
After registering with LocationManager for LocationManager.NETWORK_PROVIDER , the callback onStatusChanged() is never called even when data or network is switched on/off during registered period. Is this method not valid for Network based providers ?
Try this :
package com.mytest;
import android.R;
import android.app.Activity;
import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.location.LocationProvider;
import android.os.Bundle;
import android.widget.TextView;
public class NetworkTest extends Activity {
private LocationManager locationManager;
private TextView textView;
private final LocationListener networkLocationListener=new LocationListener(){
#Override
public void onStatusChanged(String provider, int status, Bundle extras) {
switch (status) {
case LocationProvider.AVAILABLE:
textView.setText(textView.getText().toString()
+ "Network location available again\n");
break;
case LocationProvider.OUT_OF_SERVICE:
textView.setText(textView.getText().toString()
+ "Network location out of service\n");
break;
case LocationProvider.TEMPORARILY_UNAVAILABLE:
textView.setText(textView.getText().toString()
+ "Network location temporarily unavailable\n");
break;
}
}
#Override
public void onProviderEnabled(String provider) {
textView.setText(textView.getText().toString()
+ "Network Provider Enabled\n");
}
#Override
public void onProviderDisabled(String provider) {
textView.setText(textView.getText().toString()
+ "Network Provider Disabled\n");
}
#Override
public void onLocationChanged(Location location) {
textView.setText(textView.getText().toString()
+ "New network location: "
+ String.format("%9.6f", location.getLatitude()) + ", "
+ String.format("%9.6f", location.getLongitude()) + "\n");
}
};
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activitynetwork);
textView = (TextView) findViewById(R.id.textview);
locationManager = (LocationManager)
getSystemService(Context.LOCATION_SERVICE);
}
#Override
protected void onResume() {
super.onResume();
locationManager.requestLocationUpdates(
LocationManager.NETWORK_PROVIDER, 5000, 0,
networkLocationListener);
}
#Override
protected void onPause() {
super.onPause();
locationManager.removeUpdates(networkLocationListener);
}
}
It works for me.
I am trying to get a location (latitude, longitude) on my Android smartphone (HTC Desire HD), using Wifi-network and GPS.
This is the code I use, only I keep getting "Location not available"
Iam working with Eclipse IDE.
import android.app.Activity;
import android.content.Context;
import android.location.Criteria;
import com.example.shortsproject.R;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.widget.TextView;
import android.widget.Toast;
public class LocationGetter extends Activity implements LocationListener {
private TextView latituteField;
private TextView longitudeField;
private LocationManager locationManager;
private String provider;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.location);
latituteField = (TextView) findViewById(R.id.locationtext);
longitudeField = (TextView) findViewById(R.id.locationtext2);
// Get the location manager
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
// Define the criteria how to select the location provider -> use
// default
Criteria criteria = new Criteria();
provider = locationManager.getBestProvider(criteria, false);
Location location = locationManager.getLastKnownLocation(provider);
// Initialize the location fields
if (location != null) {
System.out.println("Provider " + provider + " has been selected.");
onLocationChanged(location);
} else {
latituteField.setText("Location not available");
longitudeField.setText("Location not available");
}
}
/* Request updates at startup */
#Override
protected void onResume() {
super.onResume();
locationManager.requestLocationUpdates(provider, 400, 1, this);
}
/* Remove the locationlistener updates when Activity is paused */
#Override
protected void onPause() {
super.onPause();
locationManager.removeUpdates(this);
}
#Override
public void onLocationChanged(Location location) {
int lat = (int) (location.getLatitude());
int lng = (int) (location.getLongitude());
latituteField.setText(String.valueOf(lat));
longitudeField.setText(String.valueOf(lng));
}
#Override
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
}
#Override
public void onProviderEnabled(String provider) {
Toast.makeText(this, "Enabled new provider " + provider,
Toast.LENGTH_SHORT).show();
}
#Override
public void onProviderDisabled(String provider) {
Toast.makeText(this, "Disabled provider " + provider,
Toast.LENGTH_SHORT).show();
}
}
Can someone help me with this?
I had service that run by BrodcastReceiver which suppose to run and give me the GPS data that I requested and I stopped it after I did what suppose to be done, but I notice that It did not provide me with the data that requested and did not stop too , any body can help me in figuring out my problem
the code is below and the service tested in real device:
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.IBinder;
import android.os.SystemClock;
import android.widget.Toast;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
public class GpsService extends Service implements LocationListener{
static LocationManager mlocManager;
}
#Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
return null;
}
public void onCreate() {
}
#Override
public void onDestroy() {
}
#Override
public void onStart(Intent intent, int startid) {
mlocManager = (LocationManager)this.getSystemService(Context.LOCATION_SERVICE);
mlocManager.requestLocationUpdates( LocationManager.GPS_PROVIDER,0, 0,this);
}
#Override
public void onLocationChanged(Location loc)
{
loc.getLatitude();
loc.getLongitude();
String Text = "My current location is: " +"Latitud = " + loc.getLatitude() +"Longitud = " + loc.getLongitude();
Toast.makeText( getApplicationContext(),Text,Toast.LENGTH_SHORT).show();
// Here to stop the service and make it finish its task
SystemClock.sleep(40000);
// stop the Gps system by this application
mlocManager.removeUpdates(this);
//Here to stop the service by itself
stopSelf();
}
#Override
public void onProviderDisabled(String provider)
{
Toast.makeText( getApplicationContext(),"Gps Disabled",Toast.LENGTH_SHORT ).show();
}
#Override
public void onProviderEnabled(String provider)
{
Toast.makeText( getApplicationContext(),"Gps Enabled",Toast.LENGTH_SHORT).show();
}
#Override
public void onStatusChanged(String provider, int status, Bundle extras)
{
}
}
do you have the correct permissions on the manifest?
why do you make the service sleep and for so long? the service works on the UI thread , and such a thing can cause the service to be killed automatically by the system after about 5 seconds because the ui thread doesn't respond.
if you wish to delay the stopping and/or showing of the toast , either use Handler (and postDelayed) , or use asyncTask , or something of your own .
do you have the gps turned on? have you considered using a fake location app or the emulator for this task ?
good luck .
My app includes activity of getting coordinates of my current location and then showing the route to a destination for which i have given the coordinates.The problem is it takes hours to load the map and get the route.If anybody could suggest me how to get it done at a faster rate please.And i'm using The Map application present in mobile for getting the map functionality.
Here's the code i'm using
package com.map;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.net.Uri;
import android.os.Bundle;
import android.widget.Toast;
public class MapRouteActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
LocationManager mlocManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
LocationListener mlocListener = new MyLocationListener();
mlocManager.requestLocationUpdates( LocationManager.GPS_PROVIDER, 0, 0, mlocListener);
mlocManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, mlocListener);
}
public class MyLocationListener implements LocationListener
{
public void onLocationChanged(Location loc)
{
loc.getLatitude();
loc.getLongitude();
String Text = "My current location is: " + "Latitud = " + loc.getLatitude() +"Longitud = " + loc.getLongitude();
Toast.makeText( getApplicationContext(),Text,Toast.LENGTH_SHORT).show();
Intent intent = new Intent(android.content.Intent.ACTION_VIEW,
Uri.parse("http://maps.google.com/maps?saddr="+loc.getLatitude()+","+loc.getLongitude()+"&daddr=18.5204303,73.8567437"));
startActivity(intent);
}
#Override
public void onProviderDisabled(String provider)
{
Toast.makeText( getApplicationContext(),"Gps Disabled",Toast.LENGTH_SHORT ).show();
}
#Override
public void onProviderEnabled(String provider)
{
Toast.makeText( getApplicationContext(),"Gps Enabled",Toast.LENGTH_SHORT).show();
}
#Override
public void onStatusChanged(String provider, int status, Bundle extras)
{
}
}
}
GPS takes time to get a fix. It has to scan a lot of frequencies before it actually triangluates your position. The code that you have written launches Google Maps on getting a valid fix. ( on onLocationChanged() )
Instead you may use MapView. That way your map will be loaded much faster and you don't even have to wait for a proper fix.