在设备中启用/禁用gps时在应用程序中设置文本

r8xiu3jd  于 2021-06-29  发布在  Java
关注(0)|答案(1)|浏览(307)

我使用下面的代码根据用户打开/关闭网络和gps的时间来更改两个文本视图。我可以让网络文本视图工作,但不能让位置文本视图工作。我想我用错了过滤器,但我不知道该用哪个来代替。有什么建议/答案吗?

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);
}
}

更新:
在广播接收器里找到我替换的位置

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

具有

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

但那没用。可能需要向用户请求位置数据权限?
更新2:
意识到它正在注册locationmanager.gps\u提供程序的布尔更改,但不是最初的更改。只有在应用程序启动后手动更改位置设置,文本才会更改,这与网络检查不同,网络检查会在应用程序启动后立即更改文本。
~已解决:
必须调用以下代码两次。一次在接收器外面,然后在接收器里面。但仍不清楚为什么它一开始无法识别定位服务是开着还是关着。

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

        if (locationConnectivity) {
            locationTextView.setText("Connected");
        } else {
            locationTextView.setText("Disconnected");
        }
hfyxw5xn

hfyxw5xn1#

我想这个问题已经在这里得到了回答
但我想如果你用 LocationManager 以及 LocationListener 是不是个好主意,我是说, LocationListener 已重写这些方法:

@Override
public void onProviderEnabled(String provider) {

}

@Override
public void onProviderDisabled(String provider) {

}

我让你考虑一下

相关问题