public class locationservice extends Service implements LocationListener
{
boolean isGPSEnabled = false;
boolean isNetworkEnabled = false;
protected Context mContext;
boolean canGetLocation = false;
protected LocationManager locationManager;
Location location;
double latitude;
double longitude;
@Override
public IBinder onBind(Intent arg0) {
return null;
}
public locationservice() {
//this.mContext = context;
getLocation();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
new start().execute();
scheduleNextUpdate();
return START_STICKY;
}
@Override
public void onDestroy() {
super.onDestroy();
Toast.makeText(this, "Service Destroyed", Toast.LENGTH_LONG).show();
}
private void scheduleNextUpdate()
{
Intent intent = new Intent(this, this.getClass());
PendingIntent pendingIntent =
PendingIntent.getService(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
// The update frequency should often be user configurable. This is not.
long currentTimeMillis = System.currentTimeMillis();
long nextUpdateTimeMillis = currentTimeMillis + 1* DateUtils.MINUTE_IN_MILLIS;
Time nextUpdateTime = new Time();
nextUpdateTime.set(nextUpdateTimeMillis);
/* if (nextUpdateTime.hour < 8 || nextUpdateTime.hour >= 18)
{
nextUpdateTime.hour = 8;
nextUpdateTime.minute = 0;
nextUpdateTime.second = 0;
nextUpdateTimeMillis = nextUpdateTime.toMillis(false) + DateUtils.DAY_IN_MILLIS;
}*/
AlarmManager alarmManager = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC, nextUpdateTimeMillis, pendingIntent);
}
public void getLocation()
{
try
{
locationManager = (LocationManager)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;
//First get location from Network Provider
if (isNetworkEnabled)
{
locationManager.requestLocationUpdates(
LocationManager.NETWORK_PROVIDER,
0,
0, (android.location.LocationListener) this);
Log.d("Network", "Network");
if (locationManager != null)
{
location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
updateGPSCoordinates();
}
}
if (isGPSEnabled)
{
if (location == null)
{
locationManager.requestLocationUpdates(
LocationManager.GPS_PROVIDER,
0,
0, (android.location.LocationListener) this);
Log.d("GPS Enabled", "GPS Enabled");
if (locationManager != null)
{
location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
updateGPSCoordinates();
}
}
}
}
}
catch (Exception e)
{
//e.printStackTrace();
Log.e("Error : Location", "Impossible to connect to LocationManager", e);
}
}
public void updateGPSCoordinates()
{
if (location != null)
{
latitude = location.getLatitude();
longitude = location.getLongitude();
}
}
public double getLatitude()
{
if (location != null)
{
latitude = location.getLatitude();
}
return latitude;
}
public double getLongitude()
{
if (location != null)
{
longitude = location.getLongitude();
}
return longitude;
}
class start extends AsyncTask<String, Void,String> {
private Exception exception;
protected void onPreExecute() {
}
protected String doInBackground(String... urls) {
try {
// open a connection to the site
URL url = new URL("http://192.168.1.2/mylocation/update_location.php");
URLConnection con = url.openConnection();
// activate the output
con.setDoOutput(true);
PrintStream ps = new PrintStream(con.getOutputStream());
// send your parameters to your site
ps.print("latlong="+latitude+"$"+longitude);
ps.print("&mobile=7803214029");
// we have to get the input stream in order to actually send the request
con.getInputStream();
// close the print stream
ps.close();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
protected void onPostExecute() {
// TODO: check this.exception
// TODO: do something with the feed
}
}
@Override
public void onLocationChanged(Location location) {
// TODO Auto-generated method stub
}
}
我试图从服务中获取经度和纬度,但我得到一个空指针表达式,它还说无法连接到位置manager.at android.content.ContextWrapper.getSystemService(ContextWrapper.java:526)