public class LocationService extends Service{
private LocationManager locationMangaer=null;
private LocationListener locationListener=null;
private static final String TAG = "Debug";
private Boolean flag = false;
public static String cityName=null;
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
@Override
public void onCreate() {
super.onCreate();
Log.d("Location Serive","Started");
locationMangaer = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
locationListener = new MyLocationListener();
flag = displayGpsStatus();
boolean c=isOnline();
if(flag)
{
locationMangaer.removeUpdates(locationListener);
Criteria crit2 = new Criteria();
crit2.setAccuracy(Criteria.ACCURACY_FINE);
String provider2 = locationMangaer.getBestProvider(crit2, false);
Toast.makeText(getApplicationContext(), provider2, Toast.LENGTH_LONG).show();
locationMangaer.requestLocationUpdates(provider2,1, 1,locationListener);
}
else if(c)
{
locationMangaer.removeUpdates(locationListener);
Criteria crit = new Criteria();
crit.setPowerRequirement(Criteria.POWER_LOW);
crit.setAccuracy(Criteria.ACCURACY_COARSE);
String provider = locationMangaer.getBestProvider(crit, false);
Toast.makeText(getApplicationContext(), provider, Toast.LENGTH_LONG).show();
locationMangaer.requestLocationUpdates(provider,1, 1,locationListener);
}
else
{
//Nothing Available
}
Log.d("Internet",Boolean.toString(c));
}
/*net Enabled or Not*/
public boolean isOnline() {
ConnectivityManager cm =
(ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo netInfo = cm.getActiveNetworkInfo();
return netInfo != null && netInfo.isConnectedOrConnecting();
}
/*----------Method to Check GPS is enable or disable ------------- */
@SuppressWarnings("deprecation")
private Boolean displayGpsStatus() {
ContentResolver contentResolver = getBaseContext().getContentResolver();
boolean gpsStatus = Settings.Secure.isLocationProviderEnabled(contentResolver, LocationManager.GPS_PROVIDER);
if (gpsStatus) {
return true;
} else {
return false;
}
}
/*----------Listener class to get coordinates ------------- */
private class MyLocationListener implements LocationListener {
@Override
public void onLocationChanged(Location loc) {
Log.d("Location Changed", "location:");
Log.d("Latitude", Double.toString(loc.getLatitude()));
Log.d("Longitude",Double.toString(loc.getLongitude()));
Toast.makeText(getApplicationContext(), Double.toString(loc.getLatitude()), Toast.LENGTH_LONG).show();
double p=loc.getLongitude();
double q=loc.getLatitude();
LocationChecker(p,q);
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
}
@Override
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
}
@Override
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
}
}
从此Activtiy我调用了该服务 但是,只要通过滑动从最近的应用程序中删除我的应用程序,位置监听器就会停止工作,并且不会显示任何位置更改更新,但如果应用程序正在运行,那么它可以正常工作。
public class Dashboard extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_dashboard);
Intent l = new Intent(getApplicationContext(),LocationService.class);
getApplicationContext().startService(l);
}
答案 0 :(得分:0)
您是否停止了活动的onDestroy服务?
您还可以覆盖方法onStartCommand并使用START_STICKY值。
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.i(TAG, "Received start id " + startId + ": " + intent);
// We want this service to continue running until it is explicitly
// stopped, so return sticky.
return START_STICKY;
}
<强> UPDATE1 强>
您可以在单独的流程中启动服务。见文档: http://developer.android.com/guide/topics/manifest/service-element.html#proc
如果进程名称以小写字符开头,则为该服务 将在该名称的全局进程中运行,前提是它具有 允许这样做。
例如:
<service
android:name=".LocationService"
android:process="myLocationService" [...] >
[...]
</service>