AsyncTask(update) works only when app get opened first time, but it is not updating after 5 sec

Question!

Also when I change the screen orientation only then data get updated. When I Toasts the data then it's changing after 5 sec but don't why data doesn't getting updated.And onPreExecute() is also working after every 5sec

1.Here is my update.java File

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

    }
}

Please help me, I'm at beginning level.



Answers

A task can only be run once. To run the same code again, you need to make a new AsyncTask object. It throws an exception to tell you this, but you're ignoring all exceptions (usually a bad idea).



I use V8 binary package from a PPA. in my .travis.yml file. It looks like this:

before_install:
  - sudo add-apt-repository ppa:pinepain/libv8-5.2 -y
  - sudo apt-get update -q
  - sudo apt-get install libv8-dev -y
By : pmed


select  h.VendorID,
        SUM(CASE WHEN Datename(year,h.OrderDate) = 2011 THEN h.TotalDue else 0 END) as "TotalPay IN 2011", 
        SUM(CASE WHEN Datename(year,h.OrderDate) = 2011 THEN d.OrderQty else 0 END) as "TotalOrder IN 2011"
from    PurchaseOrderHeader h
left    join (
        select  t.PurchaseOrderID,
                sum(t.OrderQty) as OrderQty
        from    PurchaseOrderDetail t
        group   by t.PurchaseOrderID
        ) d on d.PurchaseOrderID = h.PurchaseOrderID
group by h.VendorID
order by VendorID
By : CodeArhat


This video can help you solving your question :)
By: admin