我开发了获取GPS数据并发送到服务器的应用程序。所以我想获取GPS数据,而我的应用程序在Android设备中运行背景。 我有两节课。其中一个是Main Class,其他类是GPSTracker。 我想为我的应用使用Android服务。 但我不知道我是怎么做到的。我的以下代码; 主类:
public class Request extends Activity {
public static Float longitude;
public static Float latitude;
public static int ID;
public static String URL="http://xxx.xxx";
GPSTracker gps;
@SuppressLint("NewApi")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_request);
final Button button_send_info = (Button)findViewById(R.id.button_send_info);
final TextView show_infos = (TextView) findViewById (R.id.textView1);
gps = new GPSTracker(Request.this);
if(gps.canGetLocation()){
latitude = (float) gps.getLatitude();
longitude = (float) gps.getLongitude();
// \n is for new line
Toast.makeText(getApplicationContext(), "Your Location is - \nLat: " + latitude + "\nLong: " + longitude, Toast.LENGTH_LONG).show();
}else{
// can't get location
// GPS or Network is not enabled
// Ask user to enable GPS/network in settings
gps.showSettingsAlert();
}
if( Build.VERSION.SDK_INT >= 8){
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
}
button_send_info.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
ArrayList<NameValuePair> postParameters = new ArrayList<NameValuePair>();
postParameters.add(new BasicNameValuePair("latitude", latitude.toString()));
postParameters.add(new BasicNameValuePair("longitude", longitude.toString()));
String response = null;
try
{
response = CustomHttpClient.executeHttpPost(URL, postParameters);
String res=response.toString();
show_infos.setText(res);
}
catch (Exception e)
{
//hata alanı
}
}
});
GPSTracker Class;
public class GPSTracker extends Service implements LocationListener {
protected LocationManager locationManager;
public GPSTracker(Context context) {
this.mContext = context;
getLocation();
}
public Location getLocation() {
try {
locationManager = (LocationManager) mContext
.getSystemService(LOCATION_SERVICE);
// getting GPS status
isGPSEnabled = locationManager
.isProviderEnabled(LocationManager.GPS_PROVIDER);
// getting network status
isNetworkEnabled = locationManager
.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
if (!isGPSEnabled && !isNetworkEnabled) {
// no network provider is enabled
} else {
this.canGetLocation = true;
if (isNetworkEnabled) {
locationManager.requestLocationUpdates(
LocationManager.NETWORK_PROVIDER,
MIN_TIME_BW_UPDATES,
MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
Log.d("Network", "Network");
if (locationManager != null) {
location = locationManager
.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
if (location != null) {
latitude = location.getLatitude();
longitude = location.getLongitude();
}
}
}
// if GPS Enabled get lat/long using GPS Services
if (isGPSEnabled) {
if (location == null) {
locationManager.requestLocationUpdates(
LocationManager.GPS_PROVIDER,
MIN_TIME_BW_UPDATES,
MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
Log.d("GPS Enabled", "GPS Enabled");
if (locationManager != null) {
location = locationManager
.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (location != null) {
latitude = location.getLatitude();
longitude = location.getLongitude();
}
}
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
return location;
}
我在哪个地方定义这个班级的服务?