douzhe9075 2016-09-28 20:34
浏览 71
已采纳

AsyncTask(更新)仅在第一次打开应用程序时起作用,但在5秒后不会更新

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.

  • 写回答

1条回答 默认 最新

  • doubi4814 2016-09-28 20:37
    关注

    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).

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码