android 怎么实现后台注册一个service,定时定位的功能? 服务在后台和界面没关系啊,你是不是在activity onpause的时候把服务关了啊 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 服务是bind的还是startService的?如果是bind的Activity finish后Service就停止并且释放了。 public class ServiceLocationGPS extends Service implements AMapLocationListener { private GlobalApp app; private LocationManagerProxy aMapLocManager = null; private AMapLocation aMapLocation;// 用于判断定位超时 public Timer timer; TimerTask task; public Integer period = 10; public Integer count = 0; @Override public IBinder onBind(Intent arg0) { // TODO Auto-generated method stub return null; } @Override public void onCreate() { app = (GlobalApp) getApplication(); start(); super.onCreate(); } @Override public void onDestroy() { stoptime(); super.onDestroy(); } @Override @Deprecated public void onStart(Intent intent, int startId) { // TODO Auto-generated method stub super.onStart(intent, startId); } @Override public int onStartCommand(Intent intent, int flags, int startId) { // TODO Auto-generated method stub return super.onStartCommand(intent, flags, startId); } @Override public void onLocationChanged(Location arg0) { // TODO Auto-generated method stub } @Override public void onProviderDisabled(String arg0) { // TODO Auto-generated method stub } @Override public void onProviderEnabled(String arg0) { // TODO Auto-generated method stub } @Override public void onStatusChanged(String arg0, int arg1, Bundle arg2) { // TODO Auto-generated method stub } public void startLocation() { if (aMapLocManager == null) { aMapLocManager = LocationManagerProxy .getInstance(ServiceLocationGPS.this); System.out.println("startLocation"); aMapLocManager.requestLocationUpdates( LocationProviderProxy.AMapNetwork, -1, 10, this); } else { aMapLocManager.requestLocationUpdates( LocationProviderProxy.AMapNetwork, -1, 10, this); } public void start() { initSchedule(); // 10秒以后启动任务; startSchedule(10); } @Override public void onLocationChanged(AMapLocation location) { if (location != null) { this.aMapLocation = location;// 判断超时机制 Double geoLat = location.getLatitude(); Double geoLng = location.getLongitude(); String cityCode = ""; String desc = ""; Bundle locBundle = location.getExtras(); if (locBundle != null) { cityCode = locBundle.getString("citycode"); desc = locBundle.getString("desc"); } System.out.println("onLocationChanged," + geoLat + "," + geoLng); // aMapLocManager.removeUpdates(this); //stopLocation(); } } /** * 销毁定位 */ public void stopLocation() { if (aMapLocManager != null) { aMapLocManager.removeUpdates(this); aMapLocManager.destory(); } aMapLocManager = null; } /** * 开启定时任务; */ public void startSchedule(int delay) { if (timer != null && task != null) // 10秒以后执行任务; timer.schedule(task, delay * 1000, period * 1000); } public void initSchedule() { if (timer == null) timer = new Timer(); if (task == null) task = new TimerTask() { @Override public void run() { startLocation(); } }; } private void stoptime() { if (timer != null) { timer.cancel(); timer = null; } if (task != null) { task.cancel(); task = null; } }}主要是调用了其他Activity之后 onLocationChanged 不进行回调了。 如果不调用其他Activity 那么 onLocationChanged 就不停在打印。找不到原因。 看代码只是Service的部分,还是不清楚你跳转其他Activity的方法和你开启和关闭服务的方法。。但是估计就这两个地方,或者是你的定时器问题了。 Android新手关于目录res/的问题,请帮忙 Android的Call not sent问题 急!!Time结束后为什么出错!! Android环境,cmwap接入,https速度过慢问题讨论 Android开发中实现跨进程通讯的AIDL接口 关于自定义Launcher的问题 精通Android4.0视频教程,感兴趣就进来 手机连上WIFI后,登不上电脑上的JSP网页。 怎么让Service不被kill掉或能自动重启 为什么android studio这么讨厌。。 安卓手机安装插件后就会黑屏,求解?? 关于不同版本的socket通信问题~
private LocationManagerProxy aMapLocManager = null;
private AMapLocation aMapLocation;// 用于判断定位超时 public Timer timer;
TimerTask task;
public Integer period = 10;
public Integer count = 0; @Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
return null;
} @Override
public void onCreate() {
app = (GlobalApp) getApplication();
start();
super.onCreate();
} @Override
public void onDestroy() {
stoptime();
super.onDestroy();
} @Override
@Deprecated
public void onStart(Intent intent, int startId) {
// TODO Auto-generated method stub
super.onStart(intent, startId);
} @Override
public int onStartCommand(Intent intent, int flags, int startId) {
// TODO Auto-generated method stub
return super.onStartCommand(intent, flags, startId);
} @Override
public void onLocationChanged(Location arg0) {
// TODO Auto-generated method stub } @Override
public void onProviderDisabled(String arg0) {
// TODO Auto-generated method stub } @Override
public void onProviderEnabled(String arg0) {
// TODO Auto-generated method stub } @Override
public void onStatusChanged(String arg0, int arg1, Bundle arg2) {
// TODO Auto-generated method stub } public void startLocation() {
if (aMapLocManager == null) {
aMapLocManager = LocationManagerProxy
.getInstance(ServiceLocationGPS.this);
System.out.println("startLocation");
aMapLocManager.requestLocationUpdates(
LocationProviderProxy.AMapNetwork, -1, 10, this);
} else {
aMapLocManager.requestLocationUpdates(
LocationProviderProxy.AMapNetwork, -1, 10, this);
} public void start() {
initSchedule();
// 10秒以后启动任务;
startSchedule(10);
} @Override
public void onLocationChanged(AMapLocation location) {
if (location != null) {
this.aMapLocation = location;// 判断超时机制
Double geoLat = location.getLatitude();
Double geoLng = location.getLongitude();
String cityCode = "";
String desc = "";
Bundle locBundle = location.getExtras();
if (locBundle != null) {
cityCode = locBundle.getString("citycode");
desc = locBundle.getString("desc");
}
System.out.println("onLocationChanged," + geoLat + "," + geoLng);
// aMapLocManager.removeUpdates(this);
//stopLocation();
} } /**
* 销毁定位
*/
public void stopLocation() {
if (aMapLocManager != null) {
aMapLocManager.removeUpdates(this);
aMapLocManager.destory();
}
aMapLocManager = null;
} /**
* 开启定时任务;
*/
public void startSchedule(int delay) {
if (timer != null && task != null)
// 10秒以后执行任务;
timer.schedule(task, delay * 1000, period * 1000);
} public void initSchedule() {
if (timer == null)
timer = new Timer();
if (task == null)
task = new TimerTask() {
@Override
public void run() {
startLocation();
}
};
} private void stoptime() {
if (timer != null) {
timer.cancel();
timer = null;
}
if (task != null) {
task.cancel();
task = null;
}
}}
主要是调用了其他Activity之后 onLocationChanged 不进行回调了。
如果不调用其他Activity 那么 onLocationChanged 就不停在打印。
找不到原因。