How to create a status check Activity on Android? - android

I want to create an Activity in Android 2.3 when the application starts that checks some needed things by the application, then puts a check sign and then the text of the thing that is checked.
For example:
"GPS Provider enabled" text is shown, if it is enabled, then a check sign appears on its left (if not a stop sign)
Below the first one, "Network Provider enabled" text is shown, if it is enabled, then a check sign appears on its left (if not a stop
sign)
And so on.
I'd like to make it as an animation, if possible. Any ideas?
Thanks in advance!

In your activity's onResume() write this below code to check if GPS/Network Provider enabled of not and depending on that write your other code.
final LocationManager manager = (LocationManager) getSystemService(LOCATION_SERVICE);
if (!manager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
// available
}
else
{
//not available
}
if (!manager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)) {
// available
}
else
{
//not available
}
Use ToggelButtons to indicate if they are available or not.

Related

How to make sure that getLastLocation().isSuccessful()

Im trying to getLastLocation(), but sometimes it is null. When that happens, I go to google maps just for a second, and return to my app and in 99% that will do. There is also one app that just returns city that you're in and it works even if my app can't getLastLocation(). I've noticed that when I use that other app, or google maps, or weather app, for a short time location icon will appear in status bar, but when I use my app that icon never appears, so I'm guessing that may be the problem?
What I need to do to assure that I get my location to be != null?
One more thing, sometimes I get my location (lat and long), but reverse geocoding goes to catch because List is empty? How to make sure it always is not empty?
The code that I use is just a copy/past from android developers.
If you are using Android Emulator it is expected that the location doesn't get updated unless you open the Maps App.
To ensure you get non-null location you need to request for location updates
You can do something like this
#SuppressLint("MissingPermission")
private fun getLastKnownLocation() {
// get last known location from fusedLocationProviderClient returned as a task
fusedLocationProviderClient.lastLocation
.addOnSuccessListener { lastLoc ->
if (lastLoc != null) {
// initialize lastKnownLocation from fusedLocationProviderClient
lastKnownLocation = lastLoc
} else {
// prompt user to turn on location
showLocationSettingDialog()
// when user turns on location trigger updates to get a location
fusedLocationProviderClient.requestLocationUpdates(
locationRequest, locationCallback, Looper.getMainLooper()
)
}
// in case of error Toast the error in a short Toast message
}
.addOnFailureListener {
Toast.makeText(requireActivity(), "${it.message}", Toast.LENGTH_SHORT).show()
}
}
This is just a stub, you will need to handle permissions, create FusedLocationProviderClient, LocationRequest and LocationCallbackObject.
You may also need to prompt the user to Turn on Location Settings.
Please show us your GeoCoding code to elaborate further.

FusedLocationProviderApi: how does LocationRequest.setPriority work with "Location mode" device setting?

Two questions:
How does setting LocationRequest.setPriority(priority) work with the "Location mode" device setting?
If application calls LocationRequest.setPriority(PRIORITY_HIGH_ACCURACY) is called and device setting is set to "Battery saving", I assume the application won't be able to use GPS?
Another question is, with FusedLocationApi how can I check if the device setting is set to High Accuracy?
Yes, you are right that device settings has more preference than what you are asking for. Hence, if device settings is set to "Battery Saver", application won't be able to use GPS.
You don't need FusedLocationProvider to check for location setting in device. Use below code:-
int locationMode =
Settings.Secure.getInt(activityUnderTest.getContentResolver(),
Settings.Secure.LOCATION_MODE);
Check locationMode for possible return values.
if(locationMode == LOCATION_MODE_HIGH_ACCURACY) {
//request location updates
} else { //redirect user to settings page
startActivity(new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS));
}

Get information of Location Services enabled/disabled using Fused Location Provider

I am working on an app which uses Google Maps and tracks user's location. I want to change the visibility of the "You are here!" marker when the user closes Location Services by hand or services goes to condition of inaccessible. This method can return the information of whether Location Services enabled or not:
private boolean isLocationServicesEnabled() {
LocationManager lm = (LocationManager) getSystemService(LOCATION_SERVICE);
if (lm.isProviderEnabled(LocationManager.GPS_PROVIDER) || lm.isProviderEnabled(LocationManager.NETWORK_PROVIDER)) {
return true;
}
return false;
}
However, I do not want to detect the status of Location Services for once. Whenever the user opens or closes Location Services, I should detect it and change the visibility of marker. In short, this sloppy pseudocode explains what I want to listen:
while application runs
if location services turn off
change marker visibility to false
else
change marker visibility to true
I did not find a way to achieve this task without android.location.LocationListener, want to achieve it just using Fused Location Provider. Do you have any idea? Here is the core structure I used if you want to see:
http://www.androidhive.info/2015/02/android-location-api-using-google-play-services/
(Check the title of "Complete Code")
Doing this the proper way involves a huge amount of code. You have to make use of the location SettingsApi class.
The main entry point for interacting with the location
settings-enabler APIs.
This API makes it easy for an app to ensure that the device's system
settings are properly configured for the app's location needs.
Fortunately there is a full blown sample provided by Google on github
How about implementing a gps listener?
mLocationManager.addGpsStatusListener(new GpsStatus.Listener(){
#Override
public void onGpsStatusChanged(int event) {
if(event==GpsStatus.GPS_EVENT_STARTED){
Log.d(TAG,"Gps Started");
}else if(event==GpsStatus.GPS_EVENT_STOPPED){
Log.d(TAG,"Gps Stopped");
}
}
});
modify the above code to change visibility of your marker. Hope this works.

BroadcastReceiver for Google Play Location Service (setting of the Status Bar)

I'm trying to register a BroadcastReceiver for get the state changes of Location settings
But I didn't find any documentation for doing it. I only found BroadcastReceiver to check the status of certain search providers (GPS and Network providers); but not for checking if this particular option (Location) is active or not in the system preferences.
Somebody can show me the right direction?
NOTE:
I used Google Play Location Service (com.google.android.gms.location.LocationListener interface).
Well, I found one way to check if the "Location" setting of the action bar is enabled or disabled; but without a BroadcastReceiver (a shame, really)
private static final String TAG = getClass().getName();
/* We define the LocationManager */
LocationManager location_Manager= (LocationManager) getSystemService(LOCATION_SERVICE);
/* If the GPS provider or the network provider are enabled; the Location setting is enabled.*/
if(location_Manager.isProviderEnabled(LocationManager.GPS_PROVIDER) || location_Manager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)) {
Log.d(TAG, "The Location setting is enabled");
}else{
/* We send the user to the "Location activity" to enable the setting */
Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
startActivity(intent);
}
I really did not need a BroadcastReceiver; since the "arquitecture" that has my app allows me to do without it; but I would have liked to know how to use it.
NOTICE:
If someone finds the way to make it with a BroadcastReceiver I will change my correct answer by her answer.
You can try to use Location Listener.
onProviderDisabled(String provider)
Called when the provider is disabled by the user.
Compare to provider String such as
provider.equalsIgnoreCase(LocationManager.GPS_PROVIDER)
http://developer.android.com/reference/android/location/LocationListener.html

Allow entering the application only if at least one location provicer is enabled in Android

In my application the user must have at least one location provider enabled. In order to know if one is enabled I use:
isGpsLocationEnabled = ((LocationManager) getSystemService(LOCATION_SERVICE))
.isProviderEnabled(LocationManager.GPS_PROVIDER);
isNetworkLocationEnabled = ((LocationManager) getSystemService(LOCATION_SERVICE))
.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
Now, I created an Activity alerting that none is enabled and if the user selects "Enable GPS" I open the gps options with ACTION_LOCATION_SOURCE_SETTINGS intent.
I want (After the selection of the user) to check if he enabled one, and only let him continue if he did.
I have this code in the button "Enable GPS"
showGpsOptions();
isGpsLocationEnabled = ((LocationManager) getSystemService(LOCATION_SERVICE))
.isProviderEnabled(LocationManager.GPS_PROVIDER);
isNetworkLocationEnabled = ((LocationManager) getSystemService(LOCATION_SERVICE))
.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
if (isGpsLocationEnabled || isNetworkLocationEnabled){
Intent startIntent = new Intent(EnableLocationProviderActivity.this, SensingService.class);
startService(startIntent);
} else {
finish();
}
My problem is that isGpsLocationEnabled and isNetworkLocationEnabled never gets updated with the user selection in the network settings as the code continues its execution after showGpsOptions() (I know it can't block the UI thread, but how can I overpass this situation)??
Is there anyway to execute code after the user selects something in the network settings?
Thanks! Guillermo.
when settings screen come in front of your activity, logically and acc to docs, your activity is paused and then when it is required your activity is resumed... so i guess, you should use the checking code in onResume...

Categories

Resources