Android 从另一个类调用字符串变量

Android 从另一个类调用字符串变量,android,latitude-longitude,Android,Latitude Longitude,我有下面的代码,当打开屏幕并显示layout activity_gpsnetworklocation.xml,按下相应的按钮时,这些代码可以很好地工作。 我不希望用户按下这些按钮,但希望从另一个类获取变量。 为此,我创建了静态变量,并从另一个类调用 Example eLatitudeGPS = gpsnetworklocation.getLatGPS(); 我从模拟器和物理设备上都得到了空值。有人能帮忙解决这个问题吗 package com.test.myapp; import androi

我有下面的代码,当打开屏幕并显示layout activity_gpsnetworklocation.xml,按下相应的按钮时,这些代码可以很好地工作。 我不希望用户按下这些按钮,但希望从另一个类获取变量。 为此,我创建了静态变量,并从另一个类调用

Example eLatitudeGPS = gpsnetworklocation.getLatGPS();
我从模拟器和物理设备上都得到了空值。有人能帮忙解决这个问题吗

package com.test.myapp;

import android.Manifest;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.provider.Settings;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class gpsnetworklocation extends AppCompatActivity {

private TextView txtViewLatGPS;
private TextView txtViewLongGPS;
private TextView txtViewAltGPS;

private TextView txtViewLatNetwork;
private TextView txtViewLongNetwork;
private TextView txtViewAltNetwork;

public static String LatGPS;
public static String LongGPS;
public static String AltGPS;
public static String LatNetwork;
public static String LongNetwork;
public static String AltNetwork;
final Handler handler = new Handler();

private LocationManager mLocationManagerGPS;
private LocationListener mLocationListenerGPS;

private LocationManager mLocationManagerNetwork;
private LocationListener mLocationListenerNetwork;

private Button btnGPS;
private Button btnNetwork;


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

    txtViewLatGPS = (TextView) findViewById(R.id.txtViewLatGPS);
    txtViewLongGPS = (TextView) findViewById(R.id.txtViewLonGPS);
    txtViewAltGPS = (TextView) findViewById(R.id.txtViewAltGPS);

    txtViewLatNetwork = (TextView) findViewById(R.id.txtViewLatNetwork);
    txtViewLongNetwork = (TextView) findViewById(R.id.txtViewLonNetwork);
    txtViewAltNetwork = (TextView) findViewById(R.id.txtViewAltNetwork);

    btnGPS = (Button) findViewById(R.id.btnGPSLoc);
    btnNetwork = (Button) findViewById(R.id.btnNetworkLoc);

    //get data from Network and then from GPS
    getPositionNetwork();
    handler.postDelayed(new Runnable() {
        @Override
        public void run() {
            // Display responses from Google
            getPositionGPS();
        }
    }, 6000);

    btnGPS.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            getPositionGPS();
        }
    });

    btnNetwork.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            getPositionNetwork();
        }
    });
}

private void getPositionGPS() {
    mLocationManagerGPS = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);

    mLocationListenerGPS = new LocationListener() {
        public void onLocationChanged(Location location) {
            txtViewLatGPS.setText(Double.toString(location.getLatitude()));
            txtViewLongGPS.setText(Double.toString(location.getLongitude()));
            txtViewAltGPS.setText(Double.toString(location.getAltitude()));
            LatGPS = String.valueOf(location.getLatitude());
            LongGPS = String.valueOf(location.getLongitude());

        }

        public void onStatusChanged(String provider, int status, Bundle extras) {
        }

        public void onProviderEnabled(String provider) {
        }

        public void onProviderDisabled(String provider) {
            showAlert(R.string.GPS_disabled);
        }
    };

    if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
            requestLocationPermission();
        } else {
            btnGPS.setEnabled(false);
            mLocationManagerGPS.requestLocationUpdates(LocationManager.GPS_PROVIDER, 5, 0, mLocationListenerGPS);
        }
    }
}

private void getPositionNetwork() {
    mLocationManagerNetwork = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);

    mLocationListenerNetwork = new LocationListener() {
        public void onLocationChanged(Location location) {
            txtViewLatNetwork.setText(Double.toString(location.getLatitude()));
            txtViewLongNetwork.setText(Double.toString(location.getLongitude()));
            txtViewAltNetwork.setText(Double.toString(location.getAltitude()));
            LatNetwork = Double.toString(location.getLatitude());
            LongNetwork = String.valueOf(location.getLongitude());
        }

        public void onStatusChanged(String provider, int status, Bundle extras) {
        }

        public void onProviderEnabled(String provider) {
        }

        public void onProviderDisabled(String provider) {
            showAlert(R.string.Network_disabled);
        }
    };

    if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
            requestLocationPermission();
        } else {
            btnNetwork.setEnabled(false);
            mLocationManagerNetwork.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 5, 0, mLocationListenerNetwork);
        }
    }
}

private void showAlert(int messageId) {
    final AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setMessage(messageId).setCancelable(false).setPositiveButton(R.string.btn_yes, new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            startActivity(new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS));
        }
    }).setNegativeButton(R.string.btn_no, new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            dialog.cancel();
        }
    });
    final AlertDialog alertDialog = builder.create();
    alertDialog.show();
}

private void requestLocationPermission() {
    if(ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.ACCESS_FINE_LOCATION)) {
        final AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setMessage(R.string.GPS_permissions).setCancelable(false).setPositiveButton(R.string.btn_yes, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.cancel();
                ActivityCompat.requestPermissions(gpsnetworklocation.this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, 1);
            }
        }).show();
    } else {
        final AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setMessage(R.string.GPS_permissions).setCancelable(false).setPositiveButton(R.string.btn_watch_permissions, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.cancel();
                startActivity(new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS, Uri.parse("package:" + getPackageName())));
            }
        }).setNegativeButton(R.string.btn_close, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.cancel();
            }
        }).show();
    }
}

@Override
protected void onPause() {
    super.onPause();

    if (mLocationManagerGPS != null) {
        mLocationManagerGPS.removeUpdates(mLocationListenerGPS);
    }

    if (mLocationManagerNetwork != null) {
        mLocationManagerNetwork.removeUpdates(mLocationListenerNetwork);
    }

    txtViewLatGPS.setText(null);
    txtViewLongGPS.setText(null);
    txtViewAltGPS.setText(null);

    txtViewLatNetwork.setText(null);
    txtViewLongNetwork.setText(null);
    txtViewAltNetwork.setText(null);
}

@Override
protected void onResume() {
    super.onResume();

    if (!btnGPS.isEnabled()) {
        btnGPS.setEnabled(true);
    }

    if (!btnNetwork.isEnabled()) {
        btnNetwork.setEnabled(true);
    }

}
//provide variables for other processes
public static String getLatGPS() { return LatGPS;}
public static String getLongGPS() { return LongGPS;}
public static String getAltGPS() { return AltGPS;}
public static String getLatNetwork() { return LatNetwork;}
public static String getLongNetwork() { return LongNetwork;}
public static String getAltNetwork() { return AltNetwork;}
}

当你按照你的方式去做的时候,你并不是在经历生命周期方法 因此,实际上您没有运行gpsnetworklocation,您只是在创建类的实例,没有触发除getLatGPS之外的任何方法,当然这些值将为null(它们没有初始化)

如前所述。。 您的架构中有些地方不正确-尝试使用活动的活动/片段获取相关GPS位置,然后从那里获取它


希望它能有所帮助,祝你好运。

按照你的方式进行时,你不会经历活动生命周期方法 因此,实际上您没有运行gpsnetworklocation,您只是在创建类的实例,没有触发除getLatGPS之外的任何方法,当然这些值将为null(它们没有初始化)

如前所述。。 您的架构中有些地方不正确-尝试使用活动的活动/片段获取相关GPS位置,然后从那里获取它


希望有帮助,祝你好运。

对于哪个变量,你得到的是空值?对于LatGPS、LongGPS、LATNERNET和LongNetwork。了解服务和广播接收器对于哪个变量,你得到的是空值?对于LatGPS、LongGPS、LATNERWORK和LongNetwork。了解服务和广播接收器。你能建议我怎么做吗?-活动活动/碎片“Example ElativeGPS=gpsnetworklocation.getLatGPS()”发生在哪里?我是从OnCreate部分的另一个活动调用它的,所以请在该活动中使用上述代码,或者使用自定义管理器类使其对整个应用程序具有全局性记住当您尝试在活动中使用代码时-该活动应处于活动状态(已加载startActivity方法)或您将无法使用该代码。您能建议我如何执行该操作吗?“活动/碎片”示例elativeGPS=gpsnetworklocation.getLatGPS()在哪里happend?我在OnCreate部分的另一个活动中调用它,所以请在该活动中使用上面的代码,或者使用自定义管理器类使其成为整个应用程序的全局代码记住,当您尝试在活动中使用代码时,该活动应该是活动的(使用startActivity方法加载),否则您将无法使用该代码。。