2

I found a lot of information on stack about this, but no one has done this to the end.

At the beginning i add to android manifest this:

<receiver android:name=".gps.GpsLocationReceiver">
        <intent-filter>
            <action android:name="android.location.PROVIDERS_CHANGED" />
            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </receiver>

Next i trying to add a lot of code to my app. At the beginning i implements to my activity LocationListner
According to the this: Detecting GPS on/off switch in Android phones LocationListener should override this method:

@Override
public void onReceive(Context context, Intent intent)
{
        if (intent.getAction().matches("android.location.PROVIDERS_CHANGED"))
        { 
            // react on GPS provider change action 
        }
}

But in my app, LocationListner override only this method:

@Override
public void onLocationChanged(Location location) {

}

What is wrong with this androidstudio ? What i should do now ?

Community
  • 1
  • 1
patrick1980
  • 91
  • 1
  • 8

2 Answers2

0

onReceive() is required for the BroadcastReceiver , onLocationChanged() is required and here you can get the changes of geo-location, determined by latitude and longitude:

@Override
public void onLocationChanged(Location location) {
        Long myLongitude = location.getLongitude();
        Long myLatitude = location.getLatitude();
}

if you don´t require this method in your class don´t implement LocationListener.

Jorgesys
  • 124,308
  • 23
  • 334
  • 268
  • i have longitude and latitude from other method in my app, now i want only onReceive() in my app for checking status GPS – patrick1980 Oct 28 '16 at 20:20
  • but i have all in one class: "public class MapsActivity extends FragmentActivity" how now extends BroadcastReceiver ? – patrick1980 Oct 28 '16 at 20:26
0

onReceive() method is from BroadcastReceiver class. You need to extend BroadcastReceiver first to override this method.

Follow the following steps:

1.Make your broadcast receiver class

public class NameOfYourBroadcastReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        final LocationManager lM = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
        if (lM.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
            // do something
        } else {
            // do something else
        }
    }
}

2. Register your BroadcastReceiver. For this add the following lines to your AndroidManifest.xml

<receiver android:name="com.yourpackage.NameOfYourBroadcastReceiver">
        <intent-filter>
            <action android:name="android.location.PROVIDERS_CHANGED" />
        </intent-filter>
    </receiver>
Suresh A
  • 1,178
  • 2
  • 10
  • 21
  • but how to do it in my case ? i have this class:" public class MapsActivity extends FragmentActivity implements LocationListener {" – patrick1980 Oct 28 '16 at 20:22
  • Go through the link [link](https://developer.android.com/reference/android/content/BroadcastReceiver.html) if you really want to use onReceive(). – Suresh A Oct 28 '16 at 20:30
  • I read it from in the morning, but there is no excuse how to extends BroadcastReceiver if i have "public class MapsActivity extends FragmentActivity" – patrick1980 Oct 28 '16 at 20:33
  • You cant extend two classes . Your question is not clear . Please provide whole code and state what you want to do in your app. – Suresh A Oct 28 '16 at 20:45
  • I would like to write a method which check status GPS on/off during all the time when app works, and when user change GPS to ON, i would like to show him information about this. – patrick1980 Oct 28 '16 at 20:55
  • no error but no works, to make sure i need to place this class "public class NameOfYourBroadcastReceiver extends BroadcastReceiver" in my 1st class MapsActivity ? – patrick1980 Oct 28 '16 at 22:15
  • Activity and broadcast receiver are two different things.If your app can listen broadcast receiver it will call onReceiver() method whenever gps status is changed . I think you don't need to place it in your MapsActivity . – Suresh A Oct 28 '16 at 22:48