2011-05-11 57 views
0

這裏是我的代碼:幫助想關於位置,定時器和服務

主要服務類

public class SimpleService extends Service { 
Timer mytimer; 
private String provider; 

//LocationManager locationManager; 
//LocationListener mlocListener; 
//private Handler toasthandler = new Handler(); 

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

@Override 
public void onCreate() { 
    super.onCreate(); 
    Toast.makeText(this,"Service created ...", Toast.LENGTH_LONG).show(); 
    mytimer = new Timer(); 
    mytimer.schedule(new TimerTask() { 

     @Override 
     public void run() { 
      // TODO Auto-generated method stub 
      toastHandler.sendEmptyMessage(0); 


     } 
    },0,1000); 

} 

@Override 
public void onDestroy() { 
    super.onDestroy(); 
    Toast.makeText(this, "Service destroyed ...", Toast.LENGTH_LONG).show(); 
    mytimer.cancel(); 
} 
private final Handler toastHandler=new Handler() 
{ 
    public void handleMessage(Message msg) 
    { 
     getdata(); 
    }; 
}; 
public void getdata() 
{ 
    LocationManager mlocManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); 
    LocationListener mlocListener = new MyLocationListener(); 
    mlocManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, 
      mlocListener); 
    // Initialize the location fields 


} 
public class MyLocationListener implements LocationListener { 
    @Override 
    public void onLocationChanged(Location loc) { 
     loc.getLatitude(); 
     loc.getLongitude(); 
     String Text = "My current location is: " + "Latitude = " 
       + loc.getLatitude() + "Longitude = " + loc.getLongitude(); 
     Toast.makeText(getApplicationContext(), Text, Toast.LENGTH_SHORT) 
       .show(); 
     Log.d("TAG", "Starting.."); 
    } 

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

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

    @Override 
    public void onStatusChanged(String provider, int status, Bundle extras) { 
    } 
} //End of Class MyLocationListener */ 

} 

在這裏,我用在服務計時器相互調用後requestLocationUpdate()方法和每隔1秒,但這種方法不會被調用。當我按下DDMS中的按鈕時,每次都按TAG.D打印。 輸出日誌貓看起來是這樣的:

05-11 15:47:29.763: DEBUG/dalvikvm(308): GC_EXPLICIT freed 47 objects/2264 bytes in 59ms 
05-11 15:47:34.904: DEBUG/dalvikvm(190): GC_EXPLICIT freed 88 objects/3856 bytes in 151ms 
05-11 15:47:36.294: DEBUG/TAG(3677): Starting.. 
05-11 15:47:36.294: DEBUG/TAG(3677): Starting.. 
05-11 15:47:36.294: DEBUG/TAG(2449): Starting.. 
05-11 15:47:36.524: DEBUG/TAG(3677): Starting.. 
05-11 15:47:36.534: DEBUG/TAG(3677): Starting.. 
05-11 15:47:36.544: DEBUG/TAG(3677): Starting.. 
05-11 15:47:36.554: DEBUG/TAG(3677): Starting.. 
05-11 15:47:36.564: DEBUG/TAG(3677): Starting.. 
05-11 15:47:36.574: DEBUG/TAG(3677): Starting.. 
05-11 15:47:36.594: DEBUG/TAG(3677): Starting.. 
05-11 15:47:36.604: DEBUG/TAG(3677): Starting.. 
05-11 15:47:36.614: DEBUG/TAG(3677): Starting.. 
05-11 15:47:36.624: DEBUG/TAG(3677): Starting.. 
05-11 15:47:36.635: DEBUG/TAG(3677): Starting.. 
05-11 15:47:36.644: DEBUG/TAG(3677): Starting.. 
05-11 15:47:36.664: DEBUG/TAG(3677): Starting.. 
05-11 15:47:36.674: DEBUG/TAG(3677): Starting.. 
05-11 15:47:36.694: DEBUG/TAG(3677): Starting.. 
05-11 15:47:36.704: DEBUG/TAG(3677): Starting.. 
05-11 15:47:36.714: DEBUG/TAG(3677): Starting.. 
05-11 15:47:36.734: DEBUG/TAG(3677): Starting.. 
05-11 15:47:44.394: DEBUG/dalvikvm(58): GREF has increased to 401 
05-11 15:47:47.734: DEBUG/dalvikvm(2449): GC_EXPLICIT freed 372 objects/20424 bytes in 149ms 
05-11 15:47:53.663: DEBUG/SntpClient(58): request time failed: java.net.SocketException: Address family not supported by protocol 

你能告訴這是怎麼回事錯誤的代碼?

回答

1

您不需要使用計時器來請求位置更新。 一旦註冊接收位置更新,您將收到每個位置更改事件的通知。

考慮下面的示例:

public class SimpleService extends Service { 
    Timer mytimer; 
    private String provider; 

    LocationListener mlocListener = new LocationListener { 
     @Override 
     public void onLocationChanged(Location loc) { 
      loc.getLatitude(); 
      loc.getLongitude(); 
      String Text = "My current location is: " + "Latitude = " 
       + loc.getLatitude() + "Longitude = " + loc.getLongitude(); 
      Toast.makeText(getApplicationContext(), Text, Toast.LENGTH_SHORT) 
       .show(); 
      Log.d("TAG", "Starting.."); 
     } 

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

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

     @Override 
     public void onStatusChanged(String provider, int status, Bundle extras) { 
     } 
    } //End of Class MyLocationListener */; 


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

    @Override 
    public void onCreate() { 
     super.onCreate(); 
     Toast.makeText(this,"Service created ...", Toast.LENGTH_LONG).show(); 


     LocationManager mlocManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); 

     mlocManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, 
      mlocListener); 
    } 

    @Override 
    public void onDestroy() { 
     super.onDestroy(); 
     LocationManager mlocManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); 

     mlocManager.removeUpdates(mlocListener); 
     Toast.makeText(this, "Service destroyed ...", Toast.LENGTH_LONG).show(); 
    } 
}