简体   繁体   English

当 GPS 在设备中启用/禁用时,应用程序中的 setText

[英]setText in app when GPS enabled/disabled in device

I'm using the following code to change two text views based on when the user turns on/off their network and their GPS.我正在使用以下代码根据用户打开/关闭网络和 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:更新 2:

Realized that it is registering boolean changes of the LocationManager.GPS_provider, but not initially.意识到它正在注册 LocationManager.GPS_provider 的 boolean 更改,但最初不是。 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.只有在应用程序启动后手动更改位置设置后,文本才会更改,这与应用程序启动后立即更改文本的网络检查不同。

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:但我在想是否使用LocationManagerLocationListener是一个好主意,我的意思是, LocationListener有那些被覆盖的方法:

@Override
public void onProviderEnabled(String provider) {

}

@Override
public void onProviderDisabled(String provider) {

}

I leave it to your consideration我留给你考虑

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM