我正在开发一个应用程序,在该应用程序中,我按下屏幕上的按钮,正在启动asynch任务并执行操作并将经度和纬度上传到服务器端口,然后将纬度和经度上传到服务器。 现在的问题是gps无法找到纬度和经度。它在服务器端口上抛出null。请帮帮我,我的代码如下...... 提前致谢
class Server extends AsyncTask<String, Integer, String> implements LocationListener{
@Override
protected String doInBackground(String... arg0) {
Looper.myLooper();
Looper.prepare();
for(int i=0;i<9898;i++){
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0,this);
sendstring=phonenumber+latitude+longitude+"12345467788";
System.out.println(sendstring+"4532748");
stopUsingGPS();
try {
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitNetwork().build();
StrictMode.setThreadPolicy(policy);
Socket socket = null;
try {
int REDIRECTED_SERVERPORT=31997;
InetAddress serverAddr = InetAddress.getByName(serverIpAddress);
socket = new Socket(serverAddr, REDIRECTED_SERVERPORT);
} catch (UnknownHostException e1) {
e1.printStackTrace();
} catch (IOException e1) {
e1.printStackTrace();
}
PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())),true);
out.println(sendstring);
/*latitude=0.0;
longitude=0.0;*/
}
catch (UnknownHostException e1) {
e1.printStackTrace();
} catch (IOException e1) {
e1.printStackTrace();
}
catch (Exception e) {
}
}
return call1;
}
@Override
public void onLocationChanged(Location location) {
latitude=location.getLatitude();
longitude=location.getLongitude();
}
@Override
public void onProviderDisabled(String provider) {
}
@Override
public void onProviderEnabled(String provider) {
}
@Override
public void onStatusChanged(String provider, int status,
Bundle extras) {
}
public void stopUsingGPS(){
if(locationManager != null){
locationManager.removeUpdates(this);
}
}
答案 0 :(得分:0)
最好的方法是使用CWAC定位服务定位轮询服务已经为我们使用它只需要给出时间间隔来唤醒它
这样做就像你需要jar文件一样,你可以从[这里] https://github.com/commonsguy/cwac-locpoll
获取它从您的活动中启动LocationPoller并将警报重复设置为您想要的时间
AlarmManager alarmMgr = (AlarmManager) getSystemService(ALARM_SERVICE);
Intent i = new Intent(this, LocationPoller.class);
Bundle bundle = new Bundle();
LocationPollerParameter parameter = new LocationPollerParameter(bundle);
parameter.setIntentToBroadcastOnCompletion(new Intent(this,
LocationReceiver.class));
// try GPS and fall back to NETWORK_PROVIDER
parameter.setProviders(new String[] { LocationManager.GPS_PROVIDER });
parameter.setTimeout(120000);
i.putExtras(bundle);
PendingIntent pi = PendingIntent.getBroadcast(this, 0, i, 0);
alarmMgr.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,
SystemClock.elapsedRealtime(), 300000, pi);
创建一个接收器类位置接收器,从那里获取lat n lon
public class LocationReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
try {
Bundle b=intent.getExtras();
LocationPollerResult locationResult = new LocationPollerResult(b);
Location loc=locationResult.getLocation();
String msg;
if (loc==null) {
loc=locationResult.getLastKnownLocation();
if (loc==null) {
msg=locationResult.getError();
}
else {
msg="TIMEOUT, lastKnown="+loc.toString();
}
}
else {
msg=loc.toString();
Log.i("Location Latitude", String.valueOf(loc.getLatitude()));
Log.i("Location Longitude", String.valueOf(loc.getLongitude()));
Log.i("Location Accuracy", String.valueOf(loc.getAccuracy()));
}
Log.i(getClass().getSimpleName(), "received location: " + msg);
}
catch (Exception e) {
Log.e(getClass().getName(), e.getMessage());
}
}
并将其添加到您的清单
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<receiver android:name="com.commonsware.cwac.locpoll.LocationPoller" />
<service android:name="com.commonsware.cwac.locpoll.LocationPollerService" />
以及您将获取所有内容的接收者声明
<receiver android:name="com.RareMediaCompany.Helios.LocationReceiver" />