android让应用程序继续运行

时间:2011-10-19 09:33:44

标签: android crash httpconnection

我有这个代码可以检测你的行走速度,它会记录纬度和经度以及你的步行速度,或者如果你的站点没有走在sqlite表格中,那么它应该调用表并使用httpconnect将它们发送到服务器

由于某种原因,它没有将信息发送到服务器,并且一段时间后它也崩溃了 是什么导致了这个问题,我在网上看到我应该把代码作为服务 你能告诉我如何修复代码吗?计时器是否有任何问题

注意:我的代码中没有错误

提前谢谢

        class WalkingData{
            public double lng=0.0;
            public double lat=0.0;
            public long gpsTime=0;
            public double Walking=0.0;

            public WalkingData (double ilng, double ilat, long iGPSTime, double iWalking){
               lng=ilng;
               lat=ilat;
               gpsTime=iGPSTime;
               Walking=iWalking;

            }

        }


        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);

            LocationManager locationManager;
            String context = Context.LOCATION_SERVICE;
            locationManager = (LocationManager) getSystemService(context);

            Criteria criteria = new Criteria();
            criteria.setAccuracy(Criteria.ACCURACY_FINE);
            criteria.setAltitudeRequired(false);
            criteria.setBearingRequired(false);
            criteria.setCostAllowed(true);
            criteria.setPowerRequirement(Criteria.POWER_LOW);
            String provider = locationManager.getBestProvider(criteria, true);

            Location location = locationManager.getLastKnownLocation(provider);
            updateWithNewLocation(location);

            locationManager.requestLocationUpdates(provider, 2000, 10, locationListener);
        }

        private final LocationListener locationListener = new LocationListener() {
            public void onLocationChanged(Location location) {
                updateWithNewLocation(location);
            }

            public void onProviderDisabled(String provider) {
                updateWithNewLocation(null);
            }

            public void onProviderEnabled(String provider) {
            }

            public void onStatusChanged(String provider, int status, Bundle extras) {
            }
        };

        private void updateWithNewLocation(Location location) {
            String latLongString;
            TextView myLocationText;
            myLocationText = (TextView) findViewById(R.id.myLocationText);

            //String addressString = "No address found";

            if (location != null) {
                double lat = location.getLatitude();
                double lng = location.getLongitude();
                latLongString = "Lat:" + lat + "\nLong:" + lng;

                MyWalk =(double)location.getSpeed();
                MyGPSTime = location.getTime();
                long currentTimestamp = System.currentTimeMillis();
                myLocationText.setText("Your Current Walk: " + (MyWalk) +"\nYour Current Position is: " + latLongString + "\n" + "Time is: " + currentTimestamp+"\n\n");
                trakingWalking (MyWalk, lat, lng, MyGPSTime);


            }

             else {
                latLongString = "No location found";
            }
        }




    public void trakingWalking (double Walking, double lat, double lng, long GPSTime) {

        final double ChangedWalking = Walking;
        final double ChangedLat = lat;
        final double ChangedLng = lng;
        final long ChangedGPSTime = GPSTime;
        Timer updateTimer = new Timer("pedometer");
        updateTimer.scheduleAtFixedRate(new TimerTask() {
          public void run() {
           checkWalking(ChangedWalking, ChangedLat, ChangedLng, ChangedGPSTime);
           }
        }, 0, WalkingSampleRate);
        }

    private void checkWalking( double ChangedWalking1, double ChangedLat1, double ChangedLng1, long ChangedGPSTime1) {


        final double ChangedWalking2 = ChangedWalking1;
        final double ChangedLat2 = ChangedLat1;
        final double ChangedLng2 = ChangedLng1;
        final long ChangedGPSTime2 = ChangedGPSTime1;

         WalkingHandler.post(new Runnable() {
         public void run() {

             if(WalkingState==0){
                 //TODO
Log.v("log_tag", "+-+-+-+-+-+-+-+-+- you are standing");
             } //state = 0

             else(WalkingState==1){      

                 StoreWalking();
                Sticket();          
            }

              }


      });

    }


    public void Sticket(){
        Log.v("log_tag", "???????????????????????????????? Sticket()");
        elhgsdatabase db = new elhgsdatabase(this);
        db.open();
        z = db.getAllTitles(); //narrow down to 20 last seconds

       if (z.moveToFirst())
        {
            do {          
            SticketFunction(); 
           } while (z.moveToNext());
        }

       db.close();
    }

    public void  SticketFunction(){

        //HttpClient
        HttpClient WalkingWalking = new DefaultHttpClient();
        //Response handler
        ResponseHandler<String> res = new BasicResponseHandler();

        HttpPost postMethod = new HttpPost("www.some-site.com");

        try {
            List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(5);
            //Walking
            nameValuePairs.add(new BasicNameValuePair("Walking", z.getString(1)+""));
            //Longitude
            nameValuePairs.add(new BasicNameValuePair("lat", z.getString(2)+""));
            //Latitude
            nameValuePairs.add(new BasicNameValuePair("lon", z.getString(3)+""));
            //GPS Time
            nameValuePairs.add(new BasicNameValuePair("GPSTime", z.getString(4)+""));
            //Encode and set entity
            postMethod.setEntity(new UrlEncodedFormEntity(nameValuePairs, HTTP.UTF_8));
            //Execute 
            String response = WalkingWalking.execute(postMethod, res).replaceAll("<(.|\n)*?>","");
            if (response.equals("Done")){

                //Log.v("log_tag", "!!!!!!!!!!!!!!!!!! SticketFunction got a DONE!");

            }
            else Log.v("log_tag", "!!!!!!!?????????? SticketFunction Bad or no response: ");

        } catch (ClientProtocolException e) {  
            // TODO Auto-generated catch block 
            //Log.v("log_tag", "???????????????????? SticketFunction Client Exception");
        } catch (IOException e) {  
            // TODO Auto-generated catch block
            //Log.v("log_tag", "???????????????????? IO Exception");
        } 
    }






    /*
     * 
     */

    public void StoreWalking(){

    long idy;
            elhgsdatabase db = new elhgsdatabase(this);
            db.open();
            while (!WalkingLog.isEmpty())
            {
                WalkingData tmp = WalkingLog.poll();// .pollFirst();



                //TODO: insert log_cat markups here


                idy = db.insertWalkingInfo( tmp.Walking, tmp.lat, tmp.lng, tmp.gpsTime);


        }

            db.close();
    }

    }

0 个答案:

没有答案