位置监听器即服务?

时间:2013-03-04 14:36:28

标签: android

我有一个服务,该应用程序是第一次启动时,也是启动手机时启动的。这似乎工作正常,服务正在启动。但是当我移动位置时,我没有得到任何命令。这是物理移动位置,以获得网络提供的位置而不是GPS的变化。我知道我的代码在我测试它时发送的位置完全正常。它似乎从未被调用过onLocationChange。

这是我的班级:

public class SendLocationToServer extends Service implements LocationListener {

        LocationManager lm;



    private SerializeAndSend serialSend = new SerializeAndSend();

       @Override
          public IBinder onBind(Intent arg0) {
                return null;
          }
          @Override
          public void onCreate() {
                super.onCreate();
                Log.w("ServiceINFO", "Service Started");                
                Toast.makeText(this,"Service created ...", Toast.LENGTH_LONG).show();
                lm = (LocationManager)this.getSystemService(Context.LOCATION_SERVICE);
            lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, this);
          }

          @Override
          public void onDestroy() {
                super.onDestroy();
                Toast.makeText(this, "Service destroyed ...", Toast.LENGTH_LONG).show();
          }


        public void onLocationChanged(Location lk) {

            SendLocation asyncTask = new SendLocation();
                asyncTask.execute(lk);
        }

        private class SendLocation extends AsyncTask<Location, Void, Void> {

            @Override
            protected void onPreExecute() {
            }

            @Override
            protected Void doInBackground(Location ... passed) {
                Location lk = passed[0];
                                //Here is send the location to the server which
                                // I know works, I've removed the code used just to make this a little easier to read. 

                return null;
            }

            @Override
            protected void onPostExecute(Void location) {
            }

        }


        public void onProviderDisabled(String provider) {
            // TODO Auto-generated method stub

        }
        public void onProviderEnabled(String provider) {
            // TODO Auto-generated method stub

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

        }       

}

这是调用服务的广播:

public class BroadcastControl extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        Intent service = new Intent(context, SendLocationToServer.class);
        context.startService(service);
    }

}

我想我错过了请求位置服务的电话。我现在已经添加了。但是,如果没有实际移动,我无法测试。所以我希望你现在可以告诉我这是否正确?

TIA

0 个答案:

没有答案