另外,当我仅更改屏幕方向时,数据就会更新。
当我敬酒数据时,它会在5秒钟后发生变化,但这并不是为什么数据没有更新的原因。onPreExecute()每5秒钟也会工作一次

1.这是我的update.java文件

public class update extends Activity implements LocationListener {
    protected String mLatitudeText;
    protected String mLongitudeText;
    protected String mCode = "1001";
    protected java.net.URL url;
    protected HttpURLConnection conn;
    android.os.Handler customHandler;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
        requestPermissions(new String[]{android.Manifest.permission.ACCESS_FINE_LOCATION}, 0);
        return;
    }
    LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
    lm.requestLocationUpdates(LocationManager.PASSIVE_PROVIDER, 5, 0, (android.location.LocationListener) this);
    Location mLastLocation = lm.getLastKnownLocation(LocationManager.PASSIVE_PROVIDER);

    mLatitudeText = (String.valueOf(mLastLocation.getLatitude()));
    mLongitudeText = (String.valueOf(mLastLocation.getLongitude()));

    final android.os.Handler handler = new android.os.Handler();
    Timer timer = new Timer();
    TimerTask backtask = new TimerTask() {
        @Override
        public void run() {
            handler.post(new Runnable() {
                public void run() {
                    try {
                        LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
                        Location mLastLocation = lm.getLastKnownLocation(LocationManager.PASSIVE_PROVIDER);

                        mLatitudeText = (String.valueOf(mLastLocation.getLatitude()));
                        mLongitudeText = (String.valueOf(mLastLocation.getLongitude()));
                        String[] s = new String[3];
                        s[0]=mCode;
                        s[1]=mLatitudeText;
                        s[2]=mLongitudeText;
                        Toast.makeText(getApplicationContext(), mLatitudeText + " and " + mLongitudeText, Toast.LENGTH_LONG).show();
                        makePOST.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR,s);

                    } catch (Exception e) {
                        // TODO Auto-generated catch block
                    }
                }
            });
        }
    };
    timer.schedule(backtask, 0, 5000);
}
    AsyncTask<String, Void, String> makePOST=new AsyncTask<String, Void, String>(){
    protected void onPreExecute() {
        Toast.makeText(update.this, "onPreExecute", Toast.LENGTH_LONG).show();
    }
    protected String doInBackground(String... params) {
            try {
                mCode=params[0];
                mLongitudeText=params[1];
                mLongitudeText=params[2];
                url = new URL("http://student.96.lt/Android/update.php");
                conn = (HttpURLConnection) url.openConnection();
                conn.setDoOutput(true);
                conn.setDoInput(true);
                conn.setRequestMethod("POST");
                String postdat = "code" + "=" + URLEncoder.encode(mCode, "UTF-8") + "&" + "latitude" + "=" + URLEncoder.encode(mLatitudeText, "UTF-8") + "&" + "longitude" + "=" + URLEncoder.encode(mLongitudeText, "UTF-8");
                //String postdat=mLatitudeText;
                conn.setFixedLengthStreamingMode(postdat.getBytes().length);
                conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
                OutputStream OS = conn.getOutputStream();
                BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(OS, "UTF-8"));
                bufferedWriter.write(postdat);
                bufferedWriter.flush();
                // Get output
                bufferedWriter.close();
            } catch (java.net.MalformedURLException ex) {
                //Toast.makeText(this, ex.toString(), duration ).show();
            } catch (java.io.IOException ex) {
                //Toast.makeText(this, ex.toString(), duration).show();
            }
            return null;
        }

    };
@Override
    public void onLocationChanged (Location location){



    }

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

    }

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

    }

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

    }
}


请帮助我,我才刚刚入门。

最佳答案

一个任务只能运行一次。要再次运行相同的代码,您需要创建一个新的AsyncTask对象。它会抛出一个异常来告诉您,但是您忽略了所有异常(通常是一个坏主意)。

09-27 19:04