project structure
I am trying to getContentResolver in MyLocationListenerActivity for accessing the data base which is implemented as LogContentProvider here. I have registered the MyLocationListenerActivity to the locationManager in MainActivity.
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void trackingServiceButtonOnClick(View v) {
startActivity(new Intent(MainActivity.this,
MyLocationListenerActivity.class));
LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
MyLocationListenerActivity myLocationListener = new MyLocationListenerActivity();
try {
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1, 1, myLocationListener);
} catch (SecurityException e) {
Log.d("debug", e.toString());
}
if (getContentResolver() != null) {
Log.d("debug", "I can get content resolver");
}
}
and in MyLocationListenerActivity, If I want to getContentResolver() in onCreate(), that's fine, but If I try to getContentResolver in onLocationChanged, a error "get contentResolver() on a null object reference occurs", does anyone know why this happens?
public class MyLocationListenerActivity extends AppCompatActivity implements LocationListener {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my_location_listener);
// test() works fine here
test();
}
public void test() {
getContentResolver();
}
#Override
public void onLocationChanged(Location location) {
// error will happen here
test();
}
#Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
#Override
public void onProviderEnabled(String provider) {
}
#Override
public void onProviderDisabled(String provider) {
}
Related
I am trying to get gps and network location in android. But every time I get location null.
LocationManager getting null in debugging mode.
Here is my Code
public class LocationService extends Service {
private static Timer timer = new Timer();
public Location mLocation;
#Override
public void onCreate() {
super.onCreate();
_startService();
}
#Nullable
#Override
public IBinder onBind(Intent intent) {
return null;
}
private void _startService() {
timer.scheduleAtFixedRate(new TimerTask() {
#Override
public void run() {
_serviceWork();
}
}, 1000, 30000);
}
private void _serviceWork() {
new LocationListener() {
#Override
public void onLocationChanged(Location location) {
mLocation.set(location);
}
#Override
public void onStatusChanged(String s, int i, Bundle bundle) {
}
#Override
public void onProviderEnabled(String s) {
}
#Override
public void onProviderDisabled(String s) {
}
};
}
#Override
public void onDestroy() {
super.onDestroy();
_shutdownService();
}
private void _shutdownService() {
if (timer != null) {
timer.cancel();
}
}
}
Here are my sample code:
public class GeoPointMapNewActivity extends Activity implements GpsStatus.NmeaListener {
private LocationManager mLocationManager;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.geopointmapnew_layout);
mLocationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
mLocationManager.addNmeaListener(this);
}
#Override
public void onNmeaReceived(long timestamp, String nmea) {
//i have call removeNmeaListener, but this method still be call?
mLocationManager.removeNmeaListener(this);
}
#Override
protected void onResume() {
super.onResume();
//******
}
#Override
protected void onStop() {
super.onStop();
//******
}
#Override
protected void onDestroy() {
super.onDestroy();
//*******
}
}
Sir,
thanks for your help, here is the complete code of my program.
hi I am getting error "cannot resolve method locationmanager.requestLocationUpdates();
public class MainActivity extends Activity implements LocationListener {
public Button button;
private TextView textView;
public LocationManager locationmanager;
Context context;
private double lat,lon;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
locationmanager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
button = (Button) findViewById(R.id.button);
textView = (TextView) findViewById(R.id.textView);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//error !!!!!!
**Error ** locationmanager.requestLocationUpdates(LocationManager.GPS_PROVIDER,5000,0,this);
}
});
}
#Override
public void onLocationChanged(Location location) {
if(location!=null)
{
lat=location.getLatitude();
lon=location.getLongitude();
textView.append("\n lat:"+lat+" lon:"+lon);
}
}
#Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
#Override
public void onProviderEnabled(String provider) {
}
#Override
public void onProviderDisabled(String provider) {
}
}
First, do not use "gps". Use LocationManager.GPS_PROVIDER.
Second, this does not match any signature for requestLocationUpdates(). this is a View.OnClickListener, not a LocationListener or a PendingIntent.
If this code is from an activity or fragment, and that activity or fragment is implementing LocationListener, this MyActivity.this instead of this, where MyActivity is the name of the activity or fragment that is implementing LocationListener.
I´d like to implement a LocationListener in this way.
1 - Activity_first - Call LocationListener, but I have some spinner and this don´t respond when the LocationListener are onStart(). I´d like to implement with a Thread.
2 - When the onLocationChanged() in LocationListener gets called, I call Geocoder, and when the address is different than null, I save it on the shared preferences, and stop it.
Point 2, must be done in the background because the user might be in an Activity when I get the Geocoder's address (about 10 seconds, it depend on connection)
I was testing with IntentService, but of course, I doesn't work.
My Code
public class Carga_1 extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.carga_1);
// Todo Create Spinner, etc
startService(new Intent(Carga_1.this, LocationService2.class));
}
}
public class LocationService2 extends Service {
public static final String BROADCAST_ACTION = "Hello World";
//public LocationManager locationManager; testted
//public MyLocationListener listener; tested
public Location previousBestLocation = null;
Thread my_thread;
#Override
public IBinder onBind(Intent intent) {
Log.d("[GPS_coord]", "????");
return null;
}
#Override
public void onCreate() {
super.onCreate();
}
#Override
public void onStart(Intent intent, int startId) {
Runnable r = new Runnable() {
public void run() {
LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
MyLocationListener listener = new MyLocationListener();
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, listener);
}
};
my_thread = new Thread(r);
my_thread.start();
}
#Override
public void onDestroy() {
super.onDestroy();
my_thread.stop();
}
private void get_address(double lat, double longi) {
Geocoder geocoder = new Geocoder(this, Locale.getDefault());
List<Address> addresses;
try {
addresses = geocoder.getFromLocation(lat, longi, 1);
//SAVE ON SharedPrefereces
this.stopSelf();
}
catch (IOException e) {
Log.d("[GPS_geocoder]", "Decodificacion Erronea");
}
}
public class MyLocationListener implements LocationListener {
public void onLocationChanged(final Location loc) {
Log.i("GPS_Service", "Location changed");
Log.d("GPS_Service", String.valueOf(loc.getLatitude() + " : " + String.valueOf(loc.getLongitude())));
get_address(loc.getLatitude(), loc.getLongitude());
}
public void onProviderDisabled(String provider) {
Log.i("GPS_Service", "DES_habilitado");
}
public void onProviderEnabled(String provider) {
Log.i("GPS_Service", "Habilitado");
}
public void onStatusChanged(String provider, int status, Bundle extras) {
}
}
Simply, I want to get current location and location changes through GPS via AsyncTask.
I have tried many ways out and also gone through many stuff related it, but cant help myself out. Here is my code, please suggest changes to make to the code.
public class MyGps extends Activity {
/** Called when the activity is first created. */
public static EditText txtLatitude;
public static EditText txtLongitude;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
GpsAsyncTask gpsTask = new GpsAsyncTask(this, this);
gpsTask.execute(null);
}
}
public class GpsAsyncTask extends AsyncTask<String[], String, String> {
Context context;
Activity activity;
private LocationManager locationManager;
private Location currentLocation;
Double mlat, mlng;
public GpsAsyncTask(Activity act, Context ctx) {
context = ctx;
activity = act;
}
#Override
protected void onPreExecute() {
super.onPreExecute();
String provider = Settings.Secure.getString (context.getContentResolver(),Settings.Secure.LOCATION_PROVIDERS_ALLOWED);
if(provider.contains("gps") || provider.contains("network"))
Toast.makeText(context, "Trying to connect GPS", Toast.LENGTH_LONG).show();
else
Toast.makeText(context, "GPS is not connected", Toast.LENGTH_LONG).show();
}
#Override
protected String doInBackground(String[]... params) {
locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
locationManager.requestLocationUpdates( LocationManager.GPS_PROVIDER, 0, 0f, new LocationListener()
{
public void onLocationChanged(Location loc)
{
mlat=loc.getLatitude();
mlng=loc.getLongitude();
return;
}
public void onProviderDisabled(String arg0) {
}
public void onProviderEnabled(String arg0) {
}
public void onStatusChanged(String arg0, int arg1, Bundle arg2) {
}
});
return null;
}
#Override
protected void onPostExecute(String result)
{
super.onPostExecute(result);
String provider = Settings.Secure.getString(context.getContentResolver(), Settings.Secure.LOCATION_PROVIDERS_ALLOWED);
if(provider.contains("gps") || provider.contains("network"))
{
MyGps.txtLatitude.setText(Double.toString(mlat));
MyGps.txtLongitude.setText(Double.toString(mlng));
}
}
}`