我创建了一个应用程序,它将用户位置发送到服务器......并且它使用异步任务(doInBackground和onPostExecute方法)在后台运行...但它运行速度非常慢..在编码中我使用了Thread.sleep(300000) )对于5分钟的间隔......我认为应用程序停止运行..
YasarKhan.java
Location location; // location
double latitude; // latitude
double longitude; // longitude
// The minimum distance to change Updates in meters
private static final long MIN_DISTANCE_CHANGE_FOR_UPDATES = 1000; // 1000
// meters
// The minimum time between updates in milliseconds
private static final long MIN_TIME_BW_UPDATES = 1000 * 60 * 20; // 20 minute
// Declaring a Location Manager
protected LocationManager locationManager;
boolean done = true;
// public YasarKhan(Context context) {
// this.mContext = context;
//
//
// }
public void gpsTraking() {
// check if GPS enabled
if (true) {
stAdd = "";
double latitude = getLatitude();
double longitude = getLongitude();
Geocoder gc = new Geocoder(getBaseContext());
try
{
List<Address> list = gc.getFromLocation(latitude, longitude, 2);
Address a = list.get(0);
for (int i = 0; i <= a.getMaxAddressLineIndex(); i++) {
stAdd = stAdd + "\n " + a.getAddressLine(i);
}
stAdd = stAdd + " " + a.getLocality() + "\n"
+ a.getFeatureName();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void passing()
{
new SendFeedback().execute(imei,simSerialNumber,stAdd,currentTime);
}
class SendFeedback extends AsyncTask<String, Void, String>
{
@Override
protected String doInBackground(String... paramArrayOfParams)
{
SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
request.addProperty("strIMEINumber", paramArrayOfParams[0]);
request.addProperty("strSIMNumber", paramArrayOfParams[1]);
request.addProperty("strAddress", paramArrayOfParams[2]);
request.addProperty("strTime", paramArrayOfParams[3]);
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.dotNet = true;
envelope.encodingStyle = SoapSerializationEnvelope.ENC;
envelope.setOutputSoapObject(request);
System.setProperty("http.keepAlive", "false");
try
{
HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
androidHttpTransport.call(SOAP_ACTION, envelope);
// SoapObject result = (SoapObject) envelope.getResponse();
SoapPrimitive response = (SoapPrimitive) envelope.getResponse();
String resultData = response.toString();
Log.d("response", resultData);
return resultData;
}
catch (Exception e)
{
Log.d("My Error", "" + e.getMessage());
}
return resultData;
}
@Override
protected void onPostExecute(String result)
{
Log.d("strResult", result);
super.onPostExecute(result);
}
}
}
MyReceiver.java
package com.example.yasar;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
public class MyReceiver extends BroadcastReceiver {
public static final String TAG = "com.example.gpstraking";
@Override
public void onReceive(Context context, Intent intent)
{
try
{
Intent serviceIntent = new Intent(context, YasarKhan.class);
context.startService(serviceIntent);
}
catch(Exception e)
{
Log.e("My Service Error",e.getMessage());
}
}
}
MainActivity.java
package com.example.yasar;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.util.Log;
import android.view.Menu;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.d("naveeeen","kushwaaahaACTIVITY");
Intent serviceIntent = new Intent(this, YasarKhan.class);
this.startService(serviceIntent);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
//getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
答案 0 :(得分:2)
在onStartCommand()中使用service:
使用requestLocationUpdates。
locationManager.requestLocationUpdates(provider, 5000, 0, myLocationListener);
调用此方法检查onStartCommand()中的提供程序
public void testProviders()
{
//Choose only one provider
Location location = locationManager.getLastKnownLocation(provider);
if (location != null)
{
lat = location.getLatitude();
lng = location.getLongitude();
str = "Test Provider method says :"+" Latitude = " +lat + " and " + " Longitude = " +lng;
System.err.println("Current Latitude and Longitude:"+lat+","+lng);
}
else
{
str = " No Location ";
}
System.err.println(str);
Toast.makeText(getApplicationContext(), str, Toast.LENGTH_SHORT).show();
}
使用监听器
LocationListener myLocationListener = new LocationListener()
{
@Override
public void onLocationChanged(Location location)
{
if (location != null)
{
Clat = location.getLatitude();
Clng = location.getLongitude();
System.out.println("Change latitude and Longitude:"+Clat+", "+Clng+"timer: " +System.currentTimeMillis());
String str = " LocationListener : Change latitude and Longitude: " +Clat+ ", " +Clng+ " timer: " +System.currentTimeMillis();
System.err.println(str);
Toast.makeText(getApplicationContext(), str, Toast.LENGTH_SHORT).show();
new SendPGLocationToServer().execute();
}
else
{
str = " No Location. ";
Toast.makeText(getApplicationContext(), str, Toast.LENGTH_SHORT).show();
}
}
最后,使用AsyncTask将位置发送到服务器。