我想让我的活动等待GPS定位用户,然后转到"新的RemoteDataTask()。execute();"。有什么方法可以做到吗?我尝试使用do()while,Thread.sleep等。他们工作,但严重减慢我的坐标接收过程。有没有更有效的方法呢?
这是我的班级使用位置服务的地方: 包com.fourbox.bocterapp;
//declarations
public class RecentSightings extends Activity {
//more declarations
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
this.setContentView(com.fourbox.bocterapp.R.layout.recent_sightings_design);
if (getIntent().hasExtra("currentUserLatitude")) {
currentUserLatitude = getIntent().getDoubleExtra("currentUserLatitude", 0);
currentUserLongitude = getIntent().getDoubleExtra("currentUserLongitude", 0);
}
locationManager = (LocationManager) RecentSightings.this.getSystemService(Context.LOCATION_SERVICE);
if (locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
if (currentUserLatitude==0) {
MyLocation.LocationResult locationResult = new MyLocation.LocationResult() {
@Override
public void gotLocation(Location location) {
currentUserLatitude = location.getLatitude();
currentUserLongitude = location.getLongitude();
}
};
MyLocation myLocation = new MyLocation();
myLocation.getLocation(this, locationResult);
Toast.makeText(this, "Please wait until we get your coordinates!", Toast.LENGTH_SHORT).show();
}
} else {
showGPSDisabledAlertToUser();
}
new RemoteDataTask().execute();
}
private class RemoteDataTask extends AsyncTask<Void, Void, Void> {
@Override
protected void onPreExecute() {
super.onPreExecute();
// Create a progressdialog
mProgressDialog = new ProgressDialog(RecentSightings.this);
// Set progressdialog title
mProgressDialog.setTitle("Loading recent sightings");
// Set progressdialog message
mProgressDialog.setMessage("Loading...");
mProgressDialog.setIndeterminate(false);
// Show progressdialog
mProgressDialog.show();
}
@Override
protected Void doInBackground(Void... params) {
ParseQuery<ParseObject> query = ParseQuery.getQuery("AlertsClass");
query.orderByDescending("_created_at");
try {
ob = query.find();
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Void result) {
// Locate the listview in listview_main.xml
listview = (ListView) findViewById(com.fourbox.bocterapp.R.id.alertListView);
Location currentUserLocation = new Location("currentUserLocation");
currentUserLocation.setLatitude(currentUserLatitude);
currentUserLocation.setLongitude(currentUserLongitude);
getDataForListView();
CustomAdapter adapter = new CustomAdapter(RecentSightings.this, com.fourbox.bocterapp.R.layout.list_item, alertsList, currentUserLocation);
listview.setAdapter(adapter);
// Close the progressdialog
mProgressDialog.dismiss();
// Capture button clicks on ListView items
listview.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// Send single item click data to SingleItemView Class
Intent i = new Intent(RecentSightings.this,
AlertViewOnMap.class);
// Pass data "name" followed by the position
i.putExtra("coordinatesLatitude", alertsList.get(position).getCoordinates().getLatitude());
i.putExtra("coordinatesLongitude", alertsList.get(position).getCoordinates().getLongitude());
i.putExtra("description", alertsList.get(position).getDescription());
i.putExtra("busNumber", alertsList.get(position).getBusNumber());
i.putExtra("createdAt", alertsList.get(position).getCreatedAt().getTime());
i.putExtra("currentUserLatitude", currentUserLatitude);
i.putExtra("currentUserLongitude", currentUserLongitude);
i.putStringArrayListExtra("busNumberList", busNumberList);
i.putStringArrayListExtra("descriptionList", descriptionList);
i.putStringArrayListExtra("coordinatesLatitudeList", coordinatesLatitudeList);
i.putStringArrayListExtra("coordinatesLongitudeList", coordinatesLongitudeList);
i.putStringArrayListExtra("dateCreatedAtList", dateCreatedAtList);
// Open SingleItemView.java Activity
startActivity(i);
}
});
}
}
期待感谢您的帮助!欢呼声。
编辑#1:添加了MyLocation类:
package com.fourbox.bocterapp;
/**
* Created by Soulstorm on 10/14/2014.
*/
public class MyLocation {
Timer timer1;
LocationManager lm;
LocationResult locationResult;
boolean gps_enabled=false;
boolean network_enabled=false;
public boolean getLocation(Context context, LocationResult result)
{
//I use LocationResult callback class to pass location value from MyLocation to user code.
locationResult=result;
if(lm==null)
lm = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
//exceptions will be thrown if provider is not permitted.
try{gps_enabled=lm.isProviderEnabled(LocationManager.GPS_PROVIDER);}catch(Exception ex){}
try{network_enabled=lm.isProviderEnabled(LocationManager.NETWORK_PROVIDER);}catch(Exception ex){}
//don't start listeners if no provider is enabled
if(!gps_enabled && !network_enabled)
return false;
if(gps_enabled)
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListenerGps);
if(network_enabled)
lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListenerNetwork);
timer1=new Timer();
timer1.schedule(new GetLastLocation(), 50000);
return true;
}
LocationListener locationListenerGps = new LocationListener() {
public void onLocationChanged(Location location) {
timer1.cancel();
locationResult.gotLocation(location);
lm.removeUpdates(this);
lm.removeUpdates(locationListenerNetwork);
}
public void onProviderDisabled(String provider) {}
public void onProviderEnabled(String provider) {}
public void onStatusChanged(String provider, int status, Bundle extras) {}
};
LocationListener locationListenerNetwork = new LocationListener() {
public void onLocationChanged(Location location) {
timer1.cancel();
locationResult.gotLocation(location);
lm.removeUpdates(this);
lm.removeUpdates(locationListenerGps);
}
public void onProviderDisabled(String provider) {}
public void onProviderEnabled(String provider) {}
public void onStatusChanged(String provider, int status, Bundle extras) {}
};
class GetLastLocation extends TimerTask {
@Override
public void run() {
lm.removeUpdates(locationListenerGps);
lm.removeUpdates(locationListenerNetwork);
Location net_loc=null, gps_loc=null;
if(gps_enabled)
gps_loc=lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if(network_enabled)
net_loc=lm.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
//if there are both values use the latest one
if(gps_loc!=null && net_loc!=null){
if(gps_loc.getTime()>net_loc.getTime())
locationResult.gotLocation(gps_loc);
else
locationResult.gotLocation(net_loc);
return;
}
if(gps_loc!=null){
locationResult.gotLocation(gps_loc);
return;
}
if(net_loc!=null){
locationResult.gotLocation(net_loc);
return;
}
locationResult.gotLocation(null);
}
}
public static abstract class LocationResult{
public abstract void gotLocation(Location location);
}
}
答案 0 :(得分:1)
你不能只在你已经拥有的gotLocation回调中移动执行调用吗?
public void gotLocation(Location location) {
currentUserLatitude = location.getLatitude();
currentUserLongitude = location.getLongitude();
new RemoteDataTask().execute();
}
您可能需要发布MyLocation类以确保我了解其工作原理。