0

I'm using the following code to change two text views based on when the user turns on/off their network and their GPS. I was able to get the network text view to work but not the location text view. I think I am using the wrong filter, but I don't know which to use instead. Any suggestions/answers?

public class MainActivity extends AppCompatActivity {

TextView networkTextView;
TextView locationTextView;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    networkTextView = findViewById(R.id.networkTextView);
    locationTextView = findViewById(R.id.locationTextView);

    IntentFilter filter1 = new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION);
    registerReceiver(broadcastReceiver1, filter1);

    IntentFilter filter2 = new IntentFilter(LocationManager.PROVIDERS_CHANGED_ACTION);
    registerReceiver(broadcastReceiver2, filter2);
}

BroadcastReceiver broadcastReceiver1 = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        if (ConnectivityManager.CONNECTIVITY_ACTION.equals(intent.getAction())) {
            boolean noNetworkConnectivity = intent.getBooleanExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, false);
            if (noNetworkConnectivity) {
                networkTextView.setText("Disconnected");
            } else {
                networkTextView.setText("Connected");
            }
        }
    }
};

BroadcastReceiver broadcastReceiver2 = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        if (LocationManager.PROVIDERS_CHANGED_ACTION.equals(intent.getAction())) {
            boolean noLocationConnectivity = intent.getBooleanExtra(LocationManager.PROVIDERS_CHANGED_ACTION, false);
            if (noLocationConnectivity) {
                locationTextView.setText("Disconnected");
            } else {
                locationTextView.setText("Connected");
            }
        }
    }
};

@Override
protected void onDestroy() {
    super.onDestroy();
    unregisterReceiver(broadcastReceiver1);
    unregisterReceiver(broadcastReceiver2);
}
}

Update:

In the broadcast receiver for the location I replaced

boolean noLocationConnectivity = intent.getBooleanExtra(LocationManager.PROVIDERS_CHANGED_ACTION, false);

with

LocationManager locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
                boolean locationConnectivity = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);

but that didn't work. Maybe need to request location data permission from user?

Update 2:

Realized that it is registering boolean changes of the LocationManager.GPS_provider, but not initially. Only after the Location setting is manually changed after the app is started does the text change, unlike with the network check that changes the text as soon as the app starts.

double-beep
  • 5,031
  • 17
  • 33
  • 41
M A F
  • 291
  • 4
  • 14

1 Answers1

0

I think that this question has already been answered here

But I'm thinking if use LocationManager and LocationListener is a good idea or not, I mean, LocationListener has those overrided methods:

@Override
public void onProviderEnabled(String provider) {

}

@Override
public void onProviderDisabled(String provider) {

}

I leave it to your consideration

  • The two overrides didn't register a change in the location setting for me. From the post, they actually did what I did with the same filter and I could get inside LocationManager.PROVIDERS_CHANGED_ACTION.equals(intent.getAction()) but the boolean noLocationConnectivity = intent.getBooleanExtra(LocationManager.PROVIDERS_CHANGED_ACTION, false); didn't work so I can't conditionally change the text. – M A F Dec 28 '20 at 22:52