Android 活动恢复时GPS不工作

Android 活动恢复时GPS不工作,android,Android,在用户登录之前,我需要计算用户当前的纬度和经度。我已经在我的移动设备上测试了我的代码,但它似乎不起作用。这是我的密码: LocationManager mlocManager=null; LocationListener mlocListener=null; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceStat

在用户登录之前,我需要计算用户当前的纬度和经度。我已经在我的移动设备上测试了我的代码,但它似乎不起作用。这是我的密码:

     LocationManager mlocManager=null; 
     LocationListener mlocListener=null;

     @Override
     public void onCreate(Bundle savedInstanceState) {

     super.onCreate(savedInstanceState);
     setContentView(R.layout.main);

     mlocManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);        
     mlocListener = new MyLocationListener();
     }

     @Override protected void onResume() {    
     super.onResume();    
     mlocManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, MIN_TIME_BW_UPDATES,MIN_DISTANCE_CHANGE_FOR_UPDATES, mlocListener);
     }

     @Override  protected void onPause() {    
     super.onPause();    
     mlocManager.removeUpdates(mlocListener); //<8>     
     } 

     /* Class My Location Listener */ 
     public class MyLocationListener implements LocationListener 
     { 

      @Override 
      public void onLocationChanged(Location loc) 
      {         
        Toast.makeText(getApplicationContext(),"In onchange", Toast.LENGTH_SHORT).show();
        if(loc!=null){                                                                  

            latitude=loc.getLatitude(); 
            longitude=loc.getLongitude(); 

            if(loc.getLatitude()!=0.0 || loc.getLongitude()!=0.0){       

                Toast.makeText(getApplicationContext(),"Location not null",    Toast.LENGTH_SHORT).show();
                SharedPreferences prefsSaveLatLong = context.getSharedPreferences("prefsSaveLatLong",Context.MODE_PRIVATE);     
                SharedPreferences.Editor e = prefsSaveLatLong.edit();
                e.remove("LAT");
                e.remove("LONG");
                e.putString("LAT",Double.toString(loc.getLatitude()));
                e.putString("LONG",Double.toString(loc.getLongitude()));
                e.commit();

                String Text = "My current location is: " + "Latitude = " +   loc.getLatitude() + "Longitude = " + loc.getLongitude(); 
                Toast.makeText(getApplicationContext(),Text+" "+latitude+" "+longitude, Toast.LENGTH_SHORT).show();

            }else{

                SharedPreferences prefsSaveLatLong = context.getSharedPreferences("prefsSaveLatLong",Context.MODE_PRIVATE);
                if(prefsSaveLatLong.contains("LAT") && prefsSaveLatLong.contains("LONG")){

        SharedPreferences.Editor e1 = prefsSaveLatLong.edit();
                    e1.remove("LAT");
                    e1.remove("LONG");
                    e1.commit();
                }
            }
            // set latitude longitude to label
            setLatLongLabel();

        }else{
            latLongLabel.setTextColor(Color.parseColor("#FF0000"));
            latLongLabel.setText("Latitude-Longitude not available");

            SharedPreferences prefsSaveLatLong = context.getSharedPreferences("prefsSaveLatLong",Context.MODE_PRIVATE);
            if(prefsSaveLatLong.contains("LAT") && prefsSaveLatLong.contains("LONG")){
                SharedPreferences.Editor e1 = prefsSaveLatLong.edit();
                e1.remove("LAT");
                e1.remove("LONG");
                e1.commit();
            }
        }
    } 

    @Override 
    public void onProviderDisabled(String provider) 
    { 
        gpsEnabled=false;               
        if(!gpsEnabled){            
             Toast.makeText( getApplicationContext(),"Gps Disabled", Toast.LENGTH_SHORT ).show();
            showSettingsAlert();
        }    
    } 

    @Override 
    public void onProviderEnabled(String provider) 
    { 
        gpsEnabled=true;        
        Toast.makeText(getApplicationContext(),"Gps Enabled",Toast.LENGTH_SHORT).show(); 
    } 

    @Override 
    public void onStatusChanged(String provider, int status, Bundle extras) 
    { 
        if(status==0){
            Toast.makeText(getApplicationContext(),"OUT_OF_SERVICE",Toast.LENGTH_SHORT).show();
            SharedPreferences prefsSaveLatLong = context.getSharedPreferences("prefsSaveLatLong",Context.MODE_PRIVATE);
            if(prefsSaveLatLong.contains("LAT") && prefsSaveLatLong.contains("LONG")){
                SharedPreferences.Editor e1 = prefsSaveLatLong.edit();
                e1.remove("LAT");
                e1.remove("LONG");
                e1.commit();
            }
        }
        else if(status==1){
            Toast.makeText(getApplicationContext(),"TEMPORARILY_UNAVAILABLE",Toast.LENGTH_SHORT).show();            
        }else if(status==2){
            Toast.makeText(getApplicationContext(),"AVAILABLE",Toast.LENGTH_SHORT).show();
        }    
    }   

    }/* End of Class MyLocationListener */enter code here

`
LocationManager mlocManager=null;
LocationListener mlocListener=null;
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mlocManager=(LocationManager)getSystemService(Context.LOCATION\u服务);
mlocListener=新的MyLocationListener();
}
@重写受保护的void onResume(){
super.onResume();
mlocManager.requestLocationUpdates(LocationManager.GPS\u提供程序、最短时间\u BW\u更新、最短距离\u更改\u更新、mlocListener);
}
@重写受保护的void onPause(){
super.onPause();
mlocManager.removeUpdates(mlocListener);//
} 
/*为我的位置侦听器初始化*/
公共类MyLocationListener实现LocationListener
{ 
@凌驾
位置更改后的公共无效(位置loc)
{         
Toast.makeText(getApplicationContext(),“In onchange”,Toast.LENGTH\u SHORT.show();
如果(loc!=null){
纬度=loc.getLatitude();
经度=loc.getLongitude();
如果(loc.getLatitude()!=0.0 | | loc.getLatitude()!=0.0){
Toast.makeText(getApplicationContext(),“位置不为空”,Toast.LENGTH_SHORT.show();
SharedReferences prefsSaveLatLong=context.getSharedReferences(“prefsSaveLatLong”,context.MODE_PRIVATE);
SharedReferences.Editor e=prefsSaveLatLong.edit();
e、 移除(“LAT”);
e、 删除(“长”);
e、 putString(“LAT”,Double.toString(loc.getLatitude());
e、 putString(“LONG”,Double.toString(loc.getLongitude());
e、 提交();
String Text=“我的当前位置是:“+”纬度=“+loc.getLatitude()+”经度=“+loc.getLatitude()”;
Toast.makeText(getApplicationContext(),Text+“”+纬度+“”+经度,Toast.LENGTH_SHORT).show();
}否则{
SharedReferences prefsSaveLatLong=context.getSharedReferences(“prefsSaveLatLong”,context.MODE_PRIVATE);
if(prefsSaveLatLong.contains(“LAT”)和&prefsSaveLatLong.contains(“LONG”)){
SharedReferences.Editor e1=prefsSaveLatLong.edit();
e1.移除(“LAT”);
e1.移除(“长”);
e1.commit();
}
}
//将纬度经度设置为标签
setLatLongLabel();
}否则{
latLongLabel.setTextColor(Color.parseColor(#FF0000”);
latLongLabel.setText(“经纬度不可用”);
SharedReferences prefsSaveLatLong=context.getSharedReferences(“prefsSaveLatLong”,context.MODE_PRIVATE);
if(prefsSaveLatLong.contains(“LAT”)和&prefsSaveLatLong.contains(“LONG”)){
SharedReferences.Editor e1=prefsSaveLatLong.edit();
e1.移除(“LAT”);
e1.移除(“长”);
e1.commit();
}
}
} 
@凌驾
公共无效onProviderDisabled(字符串提供程序)
{ 
gpsEnabled=false;
如果(!gpsEnabled){
Toast.makeText(getApplicationContext(),“Gps禁用”,Toast.LENGTH_SHORT.show();
showSettingsAlert();
}    
} 
@凌驾
公共无效onProviderEnabled(字符串提供程序)
{ 
gpsEnabled=true;
Toast.makeText(getApplicationContext(),“启用Gps”,Toast.LENGTH\u SHORT.show();
} 
@凌驾
public void onStatusChanged(字符串提供程序、int状态、Bundle extra)
{ 
如果(状态==0){
Toast.makeText(getApplicationContext(),“服务中断”,Toast.LENGTH\u SHORT.show();
SharedReferences prefsSaveLatLong=context.getSharedReferences(“prefsSaveLatLong”,context.MODE_PRIVATE);
if(prefsSaveLatLong.contains(“LAT”)和&prefsSaveLatLong.contains(“LONG”)){
SharedReferences.Editor e1=prefsSaveLatLong.edit();
e1.移除(“LAT”);
e1.移除(“长”);
e1.commit();
}
}
否则如果(状态==1){
Toast.makeText(getApplicationContext(),“暂时不可用”,Toast.LENGTH\u SHORT.show();
}否则如果(状态==2){
Toast.makeText(getApplicationContext(),“可用”,Toast.LENGTH_SHORT.show();
}    
}   
}/*类结束MyLocationListener*/在此处输入代码
`

试试这样的东西

public class ShowLocationActivity extends Activity implements LocationListener {
      private TextView latituteField;
      private TextView longitudeField;
      private LocationManager locationManager;
      private String provider;


    /** Called when the activity is first created. */

      @Override
      public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        latituteField = (TextView) findViewById(R.id.TextView02);
        longitudeField = (TextView) findViewById(R.id.TextView04);

        // Get the location manager
        locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        // Define the criteria how to select the locatioin provider -> use
        // default
        Criteria criteria = new Criteria();
        provider = locationManager.getBestProvider(criteria, false);
        Location location = locationManager.getLastKnownLocation(provider);

        // Initialize the location fields
        if (location != null) {
          System.out.println("Provider " + provider + " has been selected.");
          onLocationChanged(location);
        } else {
          latituteField.setText("Location not available");
          longitudeField.setText("Location not available");
        }
      }

      /* Request updates at startup */
      @Override
      protected void onResume() {
        super.onResume();
        locationManager.requestLocationUpdates(provider, 400, 1, this);
      }

      /* Remove the locationlistener updates when Activity is paused */
      @Override
      protected void onPause() {
        super.onPause();
        locationManager.removeUpdates(this);
      }

      @Override
      public void onLocationChanged(Location location) {
        int lat = (int) (location.getLatitude());
        int lng = (int) (location.getLongitude());
        latituteField.setText(String.valueOf(lat));
        longitudeField.setText(String.valueOf(lng));
      }

      @Override
      public void onStatusChanged(String provider, int status, Bundle extras) {
        // TODO Auto-generated method stub

      }

      @Override
      public void onProviderEnabled(String provider) {
        Toast.makeText(this, "Enabled new provider " + provider,
            Toast.LENGTH_SHORT).show();

      }

      @Override
      public void onProviderDisabled(String provider) {
        Toast.makeText(this, "Disabled provider " + provider,
            Toast.LENGTH_SHORT).show();
      }
    } 

请参阅教程

…它似乎不起作用
没有帮助。它怎么不起作用?有例外吗?代码是否运行但不返回值?你能详细说明一下吗?onResume()上的问题到底是什么。有没有错误或者你没有将位置设为空??Thanx很多!我的大部分问题都解决了。现在我需要清除lat lng,以防位置不可用。我该怎么做?我已经声明lat lng是主类用法的局部变量