android服务常驻后台,android-如何始终在后台运行服务?

您可以通过一些简单的实现来做到这一点:

public class LocationTrace extends Service implements LocationListener{

// The minimum distance to change Updates in meters

private static final long MIN_DISTANCE_CHANGE_FOR_UPDATES = 10; // 10 meters

private static final int TWO_MINUTES = 100 * 10;

// The minimum time between updates in milliseconds

private static final long MIN_TIME_BW_UPDATES = 1000 * 10; // 30 seconds

private Context context;

double latitude;

double longitude;

Location location = null;

boolean isGPSEnabled = false;

boolean isNetworkEnabled = false;

protected LocationManager locationManager;

@Override

public int onStartCommand(Intent intent, int flags, int startId) {

this.context = this;

get_current_location();

// Toast.makeText(context, "Lat"+latitude+"long"+longitude,Toast.LENGTH_SHORT).show();

return START_STICKY;

}

@Override

public void onLocationChanged(Location location) {

if((location != null) && (location.getLatitude() != 0) && (location.getLongitude() != 0)){

latitude = location.getLatitude();

longitude = location.getLongitude();

if (!Utils.getuserid(context).equalsIgnoreCase("")) {

Double[] arr = { location.getLatitude(), location.getLongitude() };

// DO ASYNCTASK

}

}

}

@Override

public void onStatusChanged(String provider, int status, Bundle extras) {

}

@Override

public void onProviderEnabled(String provider) {

}

@Override

public void onProviderDisabled(String provider) {

}

/*

* Get Current Location

*/

public Location get_current_location(){

locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);

isGPSEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);

isNetworkEnabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);

if(!isGPSEnabled && !isNetworkEnabled){

}else{

if (isGPSEnabled) {

if (location == null) {

locationManager.requestLocationUpdates(

LocationManager.GPS_PROVIDER,

MIN_TIME_BW_UPDATES,

MIN_DISTANCE_CHANGE_FOR_UPDATES, this);

if (locationManager != null) {

location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);

if (location != null) {

latitude = location.getLatitude();

longitude = location.getLongitude();

// Toast.makeText(context, "Latgps"+latitude+"long"+longitude,Toast.LENGTH_SHORT).show();

}

}

}

}

if (isNetworkEnabled) {

locationManager.requestLocationUpdates(

LocationManager.NETWORK_PROVIDER,

MIN_TIME_BW_UPDATES,

MIN_DISTANCE_CHANGE_FOR_UPDATES, this);

if (locationManager != null) {

if (location != null) {

latitude = location.getLatitude();

longitude = location.getLongitude();

// Toast.makeText(context, "Latgps1"+latitude+"long"+longitude,Toast.LENGTH_SHORT).show();

}

}

}

}

return location;

}

public double getLatitude() {

if(location != null){

latitude = location.getLatitude();

}

return latitude;

}

public double getLongitude() {

if(location != null){

longitude = location.getLongitude();

}

return longitude;

}

@Nullable

@Override

public IBinder onBind(Intent intent) {

return null;

}

@Override

public void onDestroy() {

if(locationManager != null){

locationManager.removeUpdates(this);

}

super.onDestroy();

}

}

您可以通过以下方式启动服务:

/*--Start Service--*/

startService(new Intent(Splash.this, LocationTrace.class));

清单中:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值