上次我的問題是:「java.lang.RuntimeException:無法在未調用Looper.prepare()」的線程中創建處理程序。之後,我修改並使用HandlerThread調用位置更新功能。沒有錯誤,但位置不刷新。任何人都可以提一些建議?如何分別在定時器中執行兩個任務?
MyService.java(編輯)
public class MyService extends Service{
int counter = 0;
static final int UPDATE_INTERVAL = 15000;
private Timer timer = new Timer();
private Timer timer2 = new Timer();
DefaultHttpClient httpclient;
HttpPost httppost;
String line,result;
HttpResponse response;
InputStream is;
BufferedReader reader;
StringBuilder sb;
final static String MY_ACTION = "MY_ACTION";
LocationManager lm;
LocationListener locationListener;
String s = "";
static final int READ_BLOCK_SIZE = 100;
List<NameValuePair> nameValuePairs;
private HandlerThread refreshThread = null;
@Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
return null;
}
public int onStartCommand(Intent intent, int flags, int startId){
doSomethingRepeatedly();
doUpdateLocation();
return START_STICKY;
}
private void doSomethingRepeatedly(){
timer.scheduleAtFixedRate(new TimerTask(){
public void run(){
...
}
},10000,UPDATE_INTERVAL);
}
private void doUpdateLocation(){
timer2.scheduleAtFixedRate(new TimerTask(){
public void run(){
onClickLoad();
lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
locationListener = new MyLocationListener();
refreshThread = new HandlerThread("GPS Thread");
refreshThread.start();
new Handler(refreshThread.getLooper()).post(
new Runnable() {
@Override
public void run() {
lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 60000, 1000, locationListener);
}
}
);
}
},10000,UPDATE_INTERVAL);
}
private class MyLocationListener implements LocationListener{
@Override
public void onLocationChanged(Location arg0) {
if(arg0!=null){
final String lat = String.valueOf(arg0.getLatitude());
final String lon = String.valueOf(arg0.getLongitude());
new Thread(){
public void run(){
try{
DefaultHttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://www.kryptoquest.com/tracker/getLocation.php");
ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(3);
nameValuePairs.add(new BasicNameValuePair("Username", s));
nameValuePairs.add(new BasicNameValuePair("Latitude", lat));
nameValuePairs.add(new BasicNameValuePair("Longitude", lon));
Log.d("Latitude",lat);
Log.d("Longitude",lon);
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
httpclient.execute(httppost);
}catch(Exception e){
Log.e("log_tag", "Error:"+e.toString());
}
}
}.start();
lm.removeUpdates(locationListener);
refreshThread.getLooper().quit();
}
}
@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
}
}
public void onClickLoad(){
try
{
FileInputStream fIn = openFileInput("user.txt");
InputStreamReader isr = new InputStreamReader(fIn);
char[] inputBuffer = new char[READ_BLOCK_SIZE];
int charRead;
while((charRead = isr.read(inputBuffer))>0)
{
String readString = String.copyValueOf(inputBuffer,0,charRead);
s += readString;
inputBuffer = new char[READ_BLOCK_SIZE];
}
}catch(IOException ioe){
ioe.printStackTrace();
}
}
public void onDestroy(){
super.onDestroy();
if(timer != null){
timer.cancel();
}
Toast.makeText(this, "Service Destroyed", Toast.LENGTH_SHORT).show();
}
}
什麼是行'MyService.java:125'?我想你是從後臺線程調用一些'UI'調用。 –
MyService.java:125 >> lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,60000,1000,locationListener); –
使用處理程序來調用此! –