AsyncTask(update) 仅在应用程序第一次打开时有效,但在 5 秒后不更新
Posted
技术标签:
【中文标题】AsyncTask(update) 仅在应用程序第一次打开时有效,但在 5 秒后不更新【英文标题】:AsyncTask(update) works only when app get opened first time, but it is not updating after 5 sec 【发布时间】:2017-02-07 00:05:39 【问题描述】:此外,当我更改屏幕方向时,数据才会更新。
当我敬酒时,数据会在 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
请帮帮我,我还处于初级阶段。
【问题讨论】:
所以您无法将最新的纬度和经度位置更新到您的服务器,因为“doInBackground”无法正常工作? 请告诉我该怎么做? 我正在尝试了解您的问题..您无法实现什么? 你的意思是什么 - “当我改变屏幕方向时,只有数据才会更新。” ?你想在哪里更新数据?在你的 UI 线程或服务器中? 【参考方案1】:一个任务只能运行一次。要再次运行相同的代码,您需要创建一个新的 AsyncTask 对象。它会抛出一个异常来告诉您这一点,但您忽略了所有异常(通常是个坏主意)。
【讨论】:
那我该怎么办?请帮帮我 创建一个新的 AsyncTask。现在您正在创建 1 并将其存储在值 makePost 中。相反,每次都通过 new 分配一个新的。 你能告诉我怎么做吗?如果你输入几行代码会更好以上是关于AsyncTask(update) 仅在应用程序第一次打开时有效,但在 5 秒后不更新的主要内容,如果未能解决你的问题,请参考以下文章