我怎样才能让这个应用程序在后台运行?我无法实现服务类,因为我已经在使用 MapActivity。我应该怎么办?
public class GoogleMaps extends MapActivity implements LocationListener {
LocationManager locationManager;
MapView map;
long start;
long stop;
MyLocationOverlay myLocation;
MapController controller;
List<Overlay> overlayList;
int x, y;
GeoPoint touchedPoint;
Drawable d;
String towers;
int lat;
int longi;
double midllat;
double midlongi;
NotificationManager nm;
static final int uniqueID = 1394885;
// Location location;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.googlemaps);
map = (MapView) findViewById(R.id.mvGoogleMaps);
ourLocation();
Touchy t = new Touchy();
overlayList = map.getOverlays();
overlayList.add(t);
d = getResources().getDrawable(R.drawable.round_pointer);
controller.setZoom(18);
Criteria crit = new Criteria();
towers = locationManager.getBestProvider(crit, false);
Location location = locationManager.getLastKnownLocation(towers);
nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
nm.cancel(uniqueID);
}
public void ourLocation()
{
// Pin der blinker på lokation
myLocation = new MyLocationOverlay(this, map);
map.getOverlays().add(myLocation);
myLocation.enableMyLocation();
// Fin nuværende lokation
locationManager = (LocationManager) this
.getSystemService(Context.LOCATION_SERVICE);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,
20000, 0, this);
controller = map.getController();
}
这里有更多代码,但与此案例无关。只是一个功能,让我确定一个点。
@Override
protected void onPause() {
// TODO Auto-generated method stub
myLocation.disableCompass();
super.onPause();
locationManager.removeUpdates(this);
}
@Override
protected void onResume() {
// TODO Auto-generated method stub
myLocation.enableCompass();
super.onResume();
locationManager.requestLocationUpdates(towers, 500, 1, this);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
@Override
protected boolean isRouteDisplayed() {
return false;
}
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
}
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
}
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
}
@Override
public void onLocationChanged(Location location1) {
// TODO Auto-generated method stub
// Updating location and zoom
controller.setCenter(new GeoPoint(
(int) (location1.getLatitude() * 1000000), (int) (location1
.getLongitude() * 1000000)));
controller.setZoom(18);
// Trying to get MyPostion.
Location myLocation = new Location("point A");
myLocation.setLongitude(location1.getLongitude());
myLocation.setLatitude(location1.getLatitude());
Location locationB = new Location("point B");
locationB.setLatitude(midllat);
locationB.setLongitude(midlongi);
float distance = myLocation.distanceTo(locationB);
if (distance < 100) {
String hej = formatDist(distance);
AudioManager am = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
am.setRingerMode(AudioManager.RINGER_MODE_SILENT);
Intent intent = new Intent(this, GoogleMaps.class);
PendingIntent pi = PendingIntent.getActivity(this, 0, intent, 0);
String body = "You are now in the silentzone";
String title = "IRemember";
Notification n = new Notification(R.drawable.round_pointer, body,
System.currentTimeMillis());
n.setLatestEventInfo(this, title, body, pi);
n.defaults = Notification.DEFAULT_ALL;
nm.notify(uniqueID, n);
}
}
public String formatDist(float meters) {
if (meters < 1000) {
return ((int) meters) + "m";
} else
return formatDec(meters / 1000f, 1) + "km";
}
public String formatDec(float val, int dec) {
int factor = (int) Math.pow(10, dec);
int front = (int) (val);
int back = (int) Math.abs(val * (factor)) % factor;
return front + "." + back;
}
}