2012-11-23 25 views
3

我正在創建一個使用GPS的應用程序。我第一次在onCreate()我正在檢查GPS是否啓用。如果它未啓用,則我將用戶發送到設置菜單以設置GPS已啓用。如何在android中禁用用戶的gps時得到通知?

GPS啓用後,我開始執行我的東西。 但是,如果在用戶之間停止通知管理器上的GPS滾動,那麼我怎麼會知道這一點?

因爲我的方法只在GPS未啓用時執行一次。之後,它再次被禁用時不會執行。

I just want to know whenever in between the work the GPS is disabled by the user. Just this information is required.

這裏是我的代碼。

class LocationFinderService extends Service{ 

    boolean gps_enabled; 
    LocationManager locationManager; 
    @Override 
    public void onCreate() { 
     // TODO Auto-generated method stub 
     super.onCreate(); 
     locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE); 
     if(!isGpsEnabled()) 
     { 
      startGpsForLocation(); 
      stopSelf(); 
     } 

    } 
    @Override 
    public IBinder onBind(Intent intent) { 
     // TODO Auto-generated method stub 
     return null; 
    } 

    public boolean isGpsEnabled() 
    { 
     gps_enabled = (locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)&& locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)); 
     Log.i("Log", "Gps: "+gps_enabled); 
     return gps_enabled; 
    } 

    public void startGpsForLocation() 
    { 
     Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS); 
     intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK) ; 
     startActivity(intent); 
     Toast.makeText(this, "Please start the GPS", Toast.LENGTH_LONG).show(); 
    } 
} 

我呼籲Activity類的按鈕,點擊此服務。

任何幫助,將不勝感激。

+0

可能重複[Android的 - 有沒有辦法聽,如果GPS已啓用或禁用(http://stackoverflow.com/questions-7521509/android-is-there-a-way-to-listen-if-gps-was-enabled-or-disabled) –

回答

2
 lmGps.requestLocationUpdates(LocationManager.GPS_PROVIDER,0,0,gpsLocationListener); 
     lmGps.addGpsStatusListener(gpsStatusListener); 

gpsLocationListner是

LocationListener gpsLocationListener=new LocationListener(){ 
     public void onLocationChanged(Location location){ 
      dGpsLat=location.getLatitude(); 
      dGpsLng=location.getLongitude(); 


     dLat=dGpsLat; 
     dLng=dGpsLng; 
     try{ 
     SaveLocation(); 
    } 
     catch (Exception e) { 
      sResponse=e.toString(); 
      return; 
     } 
    } 
    public void onStatusChanged(String provider,int status,Bundle extras){} 
    public void onProviderEnabled(String provider){} 
    public void onProviderDisabled(String provider){} 
}; 

gpsStatusListener

GpsStatus.Listener gpsStatusListener=new GpsStatus.Listener(){ 
     @Override 
     public void onGpsStatusChanged(int event){ 
      if(event==GpsStatus.GPS_EVENT_SATELLITE_STATUS){ 
       try{ 
        GpsStatus status=lmGps.getGpsStatus(null); 
        sats=status.getSatellites(); 
        Iterator satI=sats.iterator(); 

        int count=0; 
        while(satI.hasNext()){ 
         GpsSatellite gpssatellite=(GpsSatellite) satI.next(); 
         if(gpssatellite.usedInFix()){ 
          count++; 
         } 
        } 
        iSalelliteCount=count; 
       } 
       catch(Exception ex){} 
      } 
     } 
    }; 
+0

位置偵聽器沒有被調用 –

+0

它應該調用它,請再次檢查 –

+0

我想在onLocationChanged()內的logcat jsu中打印。但它不打印。請幫助我 –

2

你只需要創建一個廣播接收器

public class InternetConnectionBroadcast extends BroadcastReceiver 
{ 
    private Handler handler = new Handler(); 
    public static String state; 
    private Context netcontext; 
    public void onReceive(Context context, Intent intent) 
    { 
    netcontext = context; 
    NetworkInfo info = intent.getParcelableExtra(ConnectivityManager.EXTRA_NETWORK_INFO); 

    if(null != info) 
    { 
      state = getNetworkStateString(info.getState()); 
      Log.i("----------Network State",state); 
    } 
} 

private String getNetworkStateString(NetworkInfo.State state) 
{ 
    String stateString = "Unknown"; 
    switch(state) 
    { 
      case CONNECTED:   stateString = "Connected";  break; 

      case CONNECTING:  stateString = "Connecting";  break; 

      case DISCONNECTED:  

       stateString = "Disconnected"; 
       handler.removeCallbacks(sendUpdatesToUI); 
       handler.post(sendUpdatesToUI); 

       break; 

      case DISCONNECTING:  stateString = "Disconnecting"; break; 

      case SUSPENDED:   stateString = "Suspended";  break; 

      default:    stateString = "Unknown";  break; 
    } 
    return stateString; 
} 


private Runnable sendUpdatesToUI = new Runnable() 
{ 
    public void run() 
    { 
      Toast.makeText(netcontext,"Please Check Your Internet Connection",Toast.LENGTH_SHORT).show(); 
    } 
}; } 

也註冊到的manifest.xml現在,當

<receiver android:name="com.example.utility.InternetConnectionBroadcast"> 
    <intent-filter> 
    <action android:name="android.net.conn.CONNECTIVITY_CHANGE" /> 
    </intent-filter> 
    </receiver> 

你的連接將會丟失,它會通知用戶消息。

+3

GPS,不是互聯網...... –

相關問題