2013-01-31 153 views
0

LocationManager注册为LocationManager.NETWORK_PROVIDER后,回调onStatusChanged()当数据或网络中注册周期接通/断开不会被调用甚至。这种方法对基于网络的提供者无效吗?网络位置提供商的状态从未改变?

+0

哪个android API?它适用于GPS吗? –

+0

@Mr_and_Mrs_D上次检查果冻豆。是的,它适用于GPS,只有NETWORK_PROVIDER没有改变其状态。 –

+0

奇怪 - 原因GPS被报道[](http://stackoverflow.com/questions/2021176/how-can-i-check-the-current-status-of-the-gps-receiver/3712727#3712727)为不在较旧的设备/ API(?)上工作 - 也请参阅[这里](http://code.google.com/p/android/issues/detail?id=9433)。也许发布一个问题(并在此处添加)? –

回答

0

试试这个:

package com.mytest; 

import android.R; 
import android.app.Activity; 
import android.content.Context; 
import android.location.Location; 
import android.location.LocationListener; 
import android.location.LocationManager; 
import android.location.LocationProvider; 
import android.os.Bundle; 
import android.widget.TextView; 

public class NetworkTest extends Activity { 

    private LocationManager locationManager; 
    private TextView textView; 
    private final LocationListener networkLocationListener=new LocationListener(){ 

     @Override 
     public void onStatusChanged(String provider, int status, Bundle extras) { 
      switch (status) { 
      case LocationProvider.AVAILABLE: 
       textView.setText(textView.getText().toString() 
         + "Network location available again\n"); 
       break; 
      case LocationProvider.OUT_OF_SERVICE: 
       textView.setText(textView.getText().toString() 
         + "Network location out of service\n"); 
       break; 
      case LocationProvider.TEMPORARILY_UNAVAILABLE: 
       textView.setText(textView.getText().toString() 
         + "Network location temporarily unavailable\n"); 
       break; 
      } 
     } 

     @Override 
     public void onProviderEnabled(String provider) { 
      textView.setText(textView.getText().toString() 
        + "Network Provider Enabled\n"); 
     } 

     @Override 
     public void onProviderDisabled(String provider) { 
      textView.setText(textView.getText().toString() 
        + "Network Provider Disabled\n"); 
     } 

     @Override 
     public void onLocationChanged(Location location) { 
      textView.setText(textView.getText().toString() 
        + "New network location: " 
        + String.format("%9.6f", location.getLatitude()) + ", " 
        + String.format("%9.6f", location.getLongitude()) + "\n"); 
     } 
    }; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activitynetwork); 
     textView = (TextView) findViewById(R.id.textview); 
     locationManager = (LocationManager) 
       getSystemService(Context.LOCATION_SERVICE); 
    } 

    @Override 
    protected void onResume() { 
     super.onResume(); 
     locationManager.requestLocationUpdates(
       LocationManager.NETWORK_PROVIDER, 5000, 0, 
       networkLocationListener); 
    } 

    @Override 
    protected void onPause() { 
     super.onPause(); 
     locationManager.removeUpdates(networkLocationListener); 
    } 
} 

这对我的作品。

+0

感谢您的努力,但是,当我打开/关闭网络或切换到飞行模式并返回时,不会调用'onStatusChanged()'。 –

+0

@Singularity如果你用'Broadcast Receiver'来监视网络变化,它会好吗? –

+0

我可以这么做,或者可能是另一种解决方法。我认为如果Android将网络列为提供商之一,它也必须更新我的状态,就像使用GPS等一样。 –

相关问题