GetApplicationConext() error - android

So today I started reading on tutorials on how to get my current GPS location using Android studio. but I came up with an error " Error:(21, 24) error: cannot find symbol method getApplicationContext() " I have looked around stuckoverflow but none of the solutions worked for me. here is the code
package com.example.chara.usegps;
import android.location.Location;
import android.location.LocationListener;
import android.os.Bundle;
import android.widget.Toast;
/**
* Created by charalambous on 11/9/2015.
*/
public class MyLocationListener implements LocationListener {
#Override
public void onLocationChanged(Location location) {
location.getLatitude();
location.getLongitude();
String Text = "My Current Location is: " + " Latitude = " + location.getLatitude() + "longitude = " + location.getLongitude();
Toast.makeText(getApplicationContext(),Text,Toast.LENGTH_SHORT).show();
}
#Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
#Override
public void onProviderEnabled(String provider) {
Toast.makeText(getApplicationContext(), "Gps Enabled", Toast.LENGTH_SHORT).show();
}
#Override
public void onProviderDisabled(String provider) {
Toast.makeText(getApplicationContext(), "Gps Disabled", Toast.LENGTH_SHORT).show();
}
}

This is because your class is not extended from ActivityClass. you must add a Context object to your Costructor method and use it as context.

Related

Getting latitude and longitude in Android

I'm trying to learn how to get the current coordinates of an Android device and I have the following code:
package com.example.pruebageolocalizacion;
import java.util.List;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.widget.Toast;
public class MainActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
LocationManager locManager = (LocationManager)getSystemService(LOCATION_SERVICE);
List<String> listaProviders = locManager.getAllProviders();
for(int i=0;i<listaProviders.size();i++) {
Toast.makeText(getApplicationContext(),listaProviders.get(i).toString(),Toast.LENGTH_LONG).show();
}
if (!locManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
Toast.makeText(getApplicationContext(), "GPS DESACTIVADO",Toast.LENGTH_LONG).show();
}
else {
Location ultimaLocalizacion = locManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if(ultimaLocalizacion!=null) {
double ultimaLatitud = ultimaLocalizacion.getLatitude();
double ultimaLongitud = ultimaLocalizacion.getLongitude();
Toast.makeText(getApplicationContext(), "Ultma latitud: " + ultimaLatitud ,Toast.LENGTH_LONG).show();
Toast.makeText(getApplicationContext(), "Ultma longitud: " + ultimaLongitud ,Toast.LENGTH_LONG).show();
}
else {
Toast.makeText(getApplicationContext(), "Ultma localizacion es null",Toast.LENGTH_LONG).show();
LocationListener locListener = new LocationListener() {
public void onLocationChanged(Location location) {
Toast.makeText(getApplicationContext(), "Ultma latitud: " + String.valueOf(location.getLatitude()) ,Toast.LENGTH_LONG).show();
Toast.makeText(getApplicationContext(), "Ultma longitud: " + String.valueOf(location.getLongitude()) ,Toast.LENGTH_LONG).show();
Toast.makeText(getApplicationContext(), "Precision: " + String.valueOf(location.getAccuracy()) ,Toast.LENGTH_LONG).show();
}
public void onProviderDisabled(String provider){
Toast.makeText(getApplicationContext(), "GPS DESACTIVADO",Toast.LENGTH_LONG).show();
}
public void onProviderEnabled(String provider){
Toast.makeText(getApplicationContext(), "GPS ACTIVADO",Toast.LENGTH_LONG).show();
}
public void onStatusChanged(String provider, int status, Bundle extras){
Toast.makeText(getApplicationContext(), "Status del proveedor" + status,Toast.LENGTH_LONG).show();
}
};
Toast.makeText(getApplicationContext(), "A",Toast.LENGTH_LONG).show();
locManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 3000, 0, locListener);
Toast.makeText(getApplicationContext(), "B",Toast.LENGTH_LONG).show();
}
}
}
}
I have tried it both in an Android virtual device and in my cell phone and these are the messages I can see on the screen:
"network", "passive", "gps", "Ultma localizacion es null", "A", "B",
I have enabled WIFI and GPS in both devices. And I have used the GPS of my cell phone many times. So, shouldn't ultimaLocalizacion be different from null?
Also, if ultimaLocalizacion is null, why doesn't it show me the latitude, longitude and accuracy every three seconds?
Thanks a lot.

Getting longitude and latitude works in emulator but not on device?

I've been trying to work GPS coordinates but it's been a lot tougher than I thought. After a few hours of trial and error, I've managed to output the latitude and longitude (mocked) for my emulator. Below are the 2 ways I've done it:
First way:
import java.io.IOException;
import java.util.Calendar;
import java.util.List;
import java.util.Locale;
import android.app.Activity;
import android.content.Context;
import android.location.Address;
import android.location.Geocoder;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.widget.TextView;
public class MainActivity extends Activity
{
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String text = "";
LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
Location location = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if(location != null) {
showMyAddress(location);
}
final LocationListener locationListener = new LocationListener() {
public void onLocationChanged(Location location) {
showMyAddress(location);
}
public void onProviderDisabled(String arg0) {
}
public void onProviderEnabled(String arg0) {
}
public void onStatusChanged(String arg0, int arg1, Bundle arg2) {
}
};
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 30000, 10, locationListener);
double longitude = location.getLongitude();
double latitude = location.getLatitude();
Log.i(TAG, "longitude: " + longitude);
Log.i(TAG, "latitude: " + latitude);
text = "longitude: " + longitude + ", " + "latitude: " + latitude;
TextView textView = (TextView) findViewById(R.id.txtvwMain);
textView.setText(text);
}
private void showMyAddress(Location location) {
double latitude = location.getLatitude();
double longitude = location.getLongitude();
Geocoder myLocation = new Geocoder(getApplicationContext(), Locale.getDefault());
List<Address> myList;
try {
myList = myLocation.getFromLocation(latitude, longitude, 1);
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
Second way:
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.util.Log;
import android.widget.TextView;
public class MainActivity extends Activity implements LocationListener
{
private LocationManager lm;
private TextView tv;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv = (TextView) findViewById(R.id.txtvwMain);
lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 500, 1, this);
Location location = lm.getLastKnownLocation(lm.GPS_PROVIDER);
double longitude = location.getLongitude();
double latitude = location.getLatitude();
tv.setText("latitude="+latitude+", longitude="+longitude);
}
#Override
public void onLocationChanged(Location arg0) {
String lat = String.valueOf(arg0.getLatitude());
String lon = String.valueOf(arg0.getLongitude());
Log.e("GPS", "location changed: lat="+lat+", lon="+lon);
tv.setText("lat="+lat+", lon="+lon);
}
public void onProviderDisabled(String arg0) {
Log.e("GPS", "provider disabled " + arg0);
}
public void onProviderEnabled(String arg0) {
Log.e("GPS", "provider enabled " + arg0);
}
public void onStatusChanged(String arg0, int arg1, Bundle arg2) {
Log.e("GPS", "status changed to " + arg0 + " [" + arg1 + "]");
}
}
Both ways above work and print out the mock latitude and longitude onto a TextView in the emulator (running Android 4.2.2). However, when I upload the .apk file onto my device (tablet running Android 4.0.4), it just crashes. I don't know what's wrong because I can't see any error messages. What is the source of the problem and how may I go about solving it? Thanks.

Why does Location.getAltitude() always return zero, at least in the emulator?

Does anybody know why my getAltitude in the following always returns 0?
package com.example.helloandroid;
import android.app.Activity;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.util.Log;
public class HelloAndroid extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
Log.d("main", "onCreate");
setupGps();
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
LocationListener locationListener;
LocationManager lm;
void setupGps() {
Log.d("gps", "Setting up GPS...");
locationListener = new MyLocationListener();
lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 20000, 5,
locationListener);
Log.d("gps",
"GPS supports altitude: "
+ lm.getProvider(LocationManager.GPS_PROVIDER)
.supportsAltitude());
Log.d("gps", "Finished setting up GPS.");
}
static class MyLocationListener implements LocationListener {
public void onLocationChanged(Location location) {
Log.d("gps", "onLocationChanged");
Log.d("gps",
"x: " + location.getLongitude() + ", y: "
+ location.getLatitude() + ", alt.: "
+ location.getAltitude());
}
public void onProviderDisabled(String provider) {
Log.d("gps", "onProviderDisabled");
}
public void onProviderEnabled(String provider) {
Log.d("gps", "onProviderEnabled");
}
public void onStatusChanged(String provider, int status, Bundle extras) {
Log.d("gps",
"onStatusChanged; new status: " + String.valueOf(status));
}
}
}
To test, I issue a geo command with an altitude to the emulator:
[someone#somewhere ~]$ telnet localhost 5554
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
Android Console: type 'help' for a list of commands
OK
geo fix -121.45356 46.51119 4392
OK
But getAltitude() returns 0:
I have only tried with emulated devices.
Edit
Following up on cheeken's comment, I verified that Location.hasAltitude() is true. It is. Any other idea?
public void onLocationChanged(Location location) {
Log.d("gps", "onLocationChanged");
Log.d("gps", "x: " + location.getLongitude()
+ ", y: " + location.getLatitude());
Log.d("gps", "hasAltitude: " + location.hasAltitude()
+ ", alt.: " + location.getAltitude());
}
It seems like a lot of people are having similar issues with this and has even been reported as a possible bug. I wouldn't be surprised if the emulator simply doesn't support it at this time. You'll probably have to work with a real device.

Finding current Location when the application is running in background is not working

I want to find out current location when the Application is running in background
I Have below code for finding current location in background running services. But its not working.
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;
import android.widget.Toast;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
public class MyService extends Service {
private static LocationManager mlocManager;
private static final String TAG = "MyService";
#Override
public IBinder onBind(Intent intent) {
return null;
}
#Override
public void onCreate() {
Toast.makeText(this, "My Service Created", Toast.LENGTH_LONG).show();
Log.d(TAG, "onCreate");
}
#Override
public void onDestroy() {
Toast.makeText(this, "My Service Stopped", Toast.LENGTH_LONG).show();
Log.d(TAG, "onDestroy");
}
#Override
public void onStart(Intent intent, int startid) {
Toast.makeText(this, "start My Service Started", Toast.LENGTH_LONG).show();
Log.d(TAG, "onStart");
mlocManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
LocationListener mlocListener = new MyLocationListener();
//Location newloc= new Location();
mlocManager.requestLocationUpdates( LocationManager.GPS_PROVIDER, 0, 0, mlocListener);
Toast.makeText(this, "hiiiits created", Toast.LENGTH_LONG).show();
}
/* Class My Location Listener */
public class MyLocationListener implements LocationListener
{
#Override
public void onLocationChanged(Location loc)
{
Log.d(TAG, "onlocationchnage");
loc.getLatitude();
loc.getLongitude();
String Text ="My current location is: " +
"Latitud = " + loc.getLatitude() +
"Longitud = " + loc.getLongitude();
Toast.makeText( getApplicationContext(),
Text,
Toast.LENGTH_SHORT).show();
}
#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)
{
}
}/* End of Class MyLocationListener */
}
I think you need Current Latitude and Longitude values using GPS..
follow this link.. Click link
I already tried it and test it in android mobile phone.. Its working.
Note :
emulator we are not get the Curent GPS location.. You must and should install your application in phone..
First you ON the GPS in mobile phone after try it, it will work.
have a nice day.

Location Manager's requestLocationUpdates called only once

I am calling the method and expect location updates multiple times:
locationManager.requestLocationUpdates("gps",0 ,0, loc_listener);
My loc_listener is defined as:
LocationListener loc_listener = new LocationListener() {
private final String TAG = "xoxoxo.LocationListener";
public void onLocationChanged(Location l) {
Intent locationAlert = new Intent("xoxoxo.LOCATION_CHANGED")
.putExtra("target_location", l);
sendBroadcast(locationAlert);
// locationManager.requestLocationUpdates("gps", 0 ,0, this);
}
public void onProviderEnabled(String p) {
Log.i(TAG, "Provider enabled");
}
public void onProviderDisabled(String p) {
Log.i(TAG, "Provider disabled");
}
public void onStatusChanged(String p, int status, Bundle extras) {
Log.i(TAG, "Status changed");
}
};
Defined as is, I will only get an update once, both on HTC Evo 2.2 and 2.2 + Google API emulator.
The hack to get multiple updates is to uncomment the line which registers for updates on each update:
locationManager.requestLocationUpdates("gps", 0 ,0, this);
Have you guys seen anything like this?
I have never seen the issue. The following is my code to test LocationManager and LocationListener. It works as expected when LocationListener is implemented as an anonymous class.
package com.test.locationmanager;
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 LocationManagerStatus extends Activity {
private LocationManager locationManager;
private TextView textView;
private final LocationListener gpsLocationListener =new LocationListener(){
#Override
public void onStatusChanged(String provider, int status, Bundle extras) {
final String tvTxt = textView.getText().toString();
switch (status) {
case LocationProvider.AVAILABLE:
textView.setText(tvTxt + "GPS available again\n");
break;
case LocationProvider.OUT_OF_SERVICE:
textView.setText(tvTxt + "GPS out of service\n");
break;
case LocationProvider.TEMPORARILY_UNAVAILABLE:
textView.setText(tvTxt + "GPS temporarily unavailable\n");
break;
}
}
#Override
public void onProviderEnabled(String provider) {
textView.setText(textView.getText().toString()
+ "GPS Provider Enabled\n");
}
#Override
public void onProviderDisabled(String provider) {
textView.setText(textView.getText().toString()
+ "GPS Provider Disabled\n");
}
#Override
public void onLocationChanged(Location location) {
locationManager.removeUpdates(networkLocationListener);
textView.setText(textView.getText().toString()
+ "New GPS location: "
+ String.format("%9.6f", location.getLatitude()) + ", "
+ String.format("%9.6f", location.getLongitude()) + "\n");
}
};
private final LocationListener networkLocationListener =
new LocationListener(){
#Override
public void onStatusChanged(String provider, int status, Bundle extras){
final String tvTxt = textView.getText().toString();
switch (status) {
case LocationProvider.AVAILABLE:
textView.setText(tvTxt + "Network location available again\n");
break;
case LocationProvider.OUT_OF_SERVICE:
textView.setText(tvTxt + "Network location out of service\n");
break;
case LocationProvider.TEMPORARILY_UNAVAILABLE:
textView.setText(tvTxt
+ "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.main);
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);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,
3000, 0, gpsLocationListener);
}
#Override
protected void onPause() {
super.onPause();
locationManager.removeUpdates(networkLocationListener);
locationManager.removeUpdates(gpsLocationListener);
}
}

Categories

Resources