Capturing system event in an Activity - android

I'm planning to have a button in an activity which will start a service when clicked. However, GPS needs to be enabled for the service to do its work so I'd like to have the button disabled if GPS is disabled. Is there a way to get android to notify my activity when GPS is enabled/disabled so that I can enable/disable the button accordingly?

This link describes how to create a location listener:
http://blog.doityourselfandroid.com/2010/12/25/understanding-locationlistener-android/
I've copied the important parts down below in case the site goes down in the future. The first step is to create a LocationListener:
private final class MyLocationListener implements LocationListener {
#Override
public void onLocationChanged(Location locFromGps) {
// called when the listener is notified with a location update from the GPS
}
#Override
public void onProviderDisabled(String provider) {
// called when the GPS provider is turned off (user turning off the GPS on the phone)
// Dim the button here!
}
#Override
public void onProviderEnabled(String provider) {
// called when the GPS provider is turned on (user turning on the GPS on the phone)
// Brighten the button here!
}
#Override
public void onStatusChanged(String provider, int status, Bundle extras) {
// called when the status of the GPS provider changes
}
}
Then you'll want to register that listener. This should probably go in your onCreate()
LocationListener locationListener = new MyLocationListener();
LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 35000, 10, this.locationListener);
The second and third parameters in requestlocationupdates you should probably make huge so you don't get locationupdates since you don't really care about those, only provider enabled/disabled changes.

Please use this code to get the GPS status. use this code in the onResume of the activity
private LocationManager mLocationManager = (LocationManager)context.getSystemService(Context.LOCATION_SERVICE);
boolean GPSprovider = mLocationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
and according to the GPSProvider you can enable and disable the button.

try this
final String GpsProvider = Settings.Secure.getString(
getContentResolver(),
Settings.Secure.LOCATION_PROVIDERS_ALLOWED);
if (GpsProvider.equals(""){
//No GPS
}else{
//GPS available
}

Related

How to be informed that Location setting has changed?

I've read the Google doc and this post notably the interesting #bendaf's answer, about managing Location settings, and everything works well.
Anyway one problem remains if the user first decide to not use its position and then decide later to activate it, the application is not trigged by this action so I don't know that I can request for periodic updates.
What did I miss here?
You can provide an AlertDialog every time user launches an app so that user can enable the location. But this will be a bit annoying because every time you have to deny to same thing.
Alternatively, you can use Preferences so that user can enable/disable the location
There is another better way to do it. Use LocationListener. It is for the sole purpose that you want to achieve.
public class XYZ Activity implements LocationListener{
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER,1000,10,this);
}
#Override
public void onLocationChanged(Location location) {
}
#Override
public void onStatusChanged(String s, int i, Bundle bundle) {
}
#Override
public void onProviderEnabled(String s) {
}
#Override
public void onProviderDisabled(String s) {
}
}
I think this is the right thing that you are looking for. It's the event way.
Happy Coding :)
#fralbo Although this thread is a tiny bit old, I just wanted to provide a solution for you, as I recently had to do something similar myself.
I would recommend implementing a BroadcastReceiver into your App, with the intent filter PROVIDERS_CHANGED..
This will trigger every time the Location/GPS Provider state changes - and you can use an if statement inside of the BroadcastReceiver's onReceive method, to determine whether or not your required conditions are met, and proceed accordingly.. For example, inside the onReceive method of the BroadcastReceiver, you can determine whether the PROVIDERS_CHANGED event has made the GPS become available (and to what degree) - and if it now meets the needs of your App, you can then call whichever Method within your App that is responsible for starting the required calls to the GPS engine, etc.
Here's an example of what the code might look like:
public class LocationReceiver extends BroadcastReceiver {
private final static String TAG = "[ LocationReceiver ]:";
#Override
public void onReceive(Context context, Intent intent) {
Log.i(TAG, "PROVIDERS_CHANGED has been detected - Firing onReceive");
// Retrieve the LocationManager
LocationManager locationManager =
(LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
// Provide boolean references for Location availability types
boolean isGpsEnabled;
boolean isNetworkEnabled;
// Provide values to retrieve the Location availability
isGpsEnabled =
locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
isNetworkEnabled =
locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
Log.i(TAG, "Detected - GPS: "+ isGpsEnabled + "NET: "+ isNetworkEnabled);
// If (for example), the GPS is ENABLED, start one of your Activities, etc.
if (isGpsEnabled) {
Intent startYourActivity =
new Intent(context.getApplicationContext(), YourActivity.class);
context.startActivity(startYourActivity);
}
}
}
I hope this helps! Better late than never :)
Happy coding!

how to know Gps is off using FusedLocationProviderApi periodicaly

I am working in app where user location tracking all time and updated into server in background service, i update location to the server from onLocationChanged(Location location) #Override method. I want hit server when location is not changed also. is there any #Override method who is call when Gps is off?Mean,i want to notify the server that user's gps is off.
You can detect GPS on/off event/status with GpsStatus.Listener and register it with the LocationManager.
LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
lm.addGpsStatusListener(new android.location.GpsStatus.Listener()
{
public void onGpsStatusChanged(int event)
{
switch(event)
{
case GPS_EVENT_STARTED:
// do your tasks
break;
case GPS_EVENT_STOPPED:
// do your tasks
break;
}
}
});

Android: measured distance between locations is wrong

...or most probably, I am doing it wrong. What I want is to display a Toast every one meter I walk inside home. The code below gives me wrong results, as the moment I install the app on my phone I get a Toast without even moving!
public class MainActivity extends Activity {
private LocationListener mLocationListener;
private String mLocationProvider;
private LocationManager mLocationManager;
private Location mCurrentLocation;
private int mCounter = 0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mLocationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
mLocationListener = new MyLocationListener();
Criteria criterion = new Criteria();
criterion.setAccuracy(Criteria.ACCURACY_FINE);
criterion.setCostAllowed(true);
criterion.setPowerRequirement(Criteria.POWER_HIGH);
mLocationProvider = mLocationManager.getBestProvider(criterion, true);
}
#Override
protected void onResume() {
super.onResume();
mCurrentLocation = mLocationManager.getLastKnownLocation(mLocationProvider);
mLocationManager.requestLocationUpdates(mLocationProvider, 5000, 1, mLocationListener);
}
#Override
protected void onPause() {
super.onPause();
mLocationManager.removeUpdates(mLocationListener);
}
private class MyLocationListener implements LocationListener {
#Override
public void onLocationChanged(Location newlocation) {
float distance = mCurrentLocation.distanceTo(newlocation);
if (distance >= 1) {
mCounter++;
Toast.makeText(MainActivity.this, String.format("Message #%d: you walked one more meter", mCounter), Toast.LENGTH_SHORT).show();
mCurrentLocation = newlocation;
}
}
}
}
A GPS signal is not precise enough to give exact locations for a 1m radius. There can be deviation peeks up to 50 - 100m in real situations using GPS. This depends much on the environment you are at. GPS will be reflected by buildings, water etc. An average deviation is 10 - 20m. This will get even worse if your inside of a building using a GPS provider instead of a Network provider.
Furthermore you will never get the same coordinates twice in a row, because of this. Even if you don't move! To avoid that you could temporarly save the location and compare it with the new location. If the distance between them hits a defined boarder use the new location.
Change your location provider to GPS. And you have instantiated the LocationListener before you request the new Location(in onResume(); onResume() will be called after onCreate()). This might be the reason for your app showing Toast on start up.. Try to instantiate LocationListener after the requestLocationUpdates()..

How to determine GPS getting unlocked while running the application in android?

I need to check whether GPS is locked in my application. I am able to check whether GPs is enabled in the phone. But once the GPS is locked, while running the application, it gets unlocked and I noticed that the GPS icon of the phone is blinking. I need to show an animation similar to that of phone in my application to acknowledge the user the GPS is unlocked. Anybody who knows the solution please post it.
Thanks in advance....
This is what I use. Start your animation when you requestLocationUpdates and end it when you get the onLocationChanged call. See my code below, it should solve your issue.
public class YourLocationClass implements LocationListener {
private LocationManager lm;
public void getLocation(Context ctx) {
lm = (LocationManager) ctx.getSystemService(Context.LOCATION_SERVICE);
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 15000, 25, this);
//START YOUR ANIMATION
}
public void onLocationChanged(Location location) {
//HERE YOU STOP YOUR ANIMATION AND GET NEW LOCATION
}
public void onProviderDisabled(String provider) {
// DO SOME STUFF
}
public void onProviderEnabled(String provider) {
// DO SOME STUFF
}
public void onStatusChanged(String provider, int status, Bundle extras) {
// DO SOME STUFF
}
}

Android when exactly is onLocationChanged called

It seems for me it is getting called the first time the activity starts, just after onCreate, it then seems to be called at random intervals, whether I move or not???
Regardless of that is it simply called automatically if I have code like this in the onCreate method?
locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
Criteria criteria = new Criteria();
Is that right???
Cheers,
Mike.
Your question is not clear initially.Your code and title are not matching. I am giving answer for your title only.
You have to register Location Listener for your Location Manager, then only onLocationChanged() will be called according the settings you supplied while registering location listener.
See below code how to do that. I used GPS Provider, you can use any provider based on criteria also.
LocationManger lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 0, new LocationListener() {
#Override
public void onLocationChanged(Location location) {
// TODO Auto-generated method stub
}
#Override
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
}
#Override
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
}
#Override
public void onStatusChanged(String provider, int status,
Bundle extras) {
// TODO Auto-generated method stub
}
});
Coming to your question, onLocationChanged() will be called if the current location update is not matching with last known location.
The updated location will be changed for every minTime (in my case 1000 milli sec) and also if device moved minDistance (in my case 0 meters) distance.
I hope you will understand this.
if you want to catch new locations, you have to register a LocationListener like this:
LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
LocationListener listener = new LocationListener() {
...
}
locationManager.requestLocationUpdates(GPS_PROVIDER, intervall, distance, listener);
With intervall and distance you can configure:
If intervall is greater than 0, the LocationManager could potentially rest for intervall milliseconds between location updates
If distance is greater than 0, a location will only be broadcasted if the device moves by distance meters.
When the LocationListener is registered, the LocationManager starts to get your geo location and calls the onLocationChanged(). If the distance is very low, it can happen that the method is called very often in a short period of time. According to the intervall, the LocationManager will rest afterwards.
I think, the LocationManager will only start doing it's work, when a LocationListener is registered.
Hope that helps...
Cheers,
Tobi
public void onLocationChanged(Location location)
the above method gets called automatically once your location has been changed..

Categories

Resources