朋友,
我创建了一个android后台服务,每2分钟调用一次。当它被呼叫时,它开始监听GPS和NETWORK的位置更新40秒。提取有用的信息(纬度、经度、准确性、提供者…(并将数据发送到服务器。
当我在模拟器上运行应用程序时,它运行得很好。(我没有得到真正的GPS信息,只有所有变量的默认值,但我知道我的服务每两分钟调用一次,并将数据发送到服务器(
因此,当我在真实的设备上运行该应用程序时,它可以完美地工作大约40分钟。然后,我想,有什么东西正在关闭我的服务。(可能是由于内存消耗或其他原因,android本身(
有人知道我如何追踪这个bug吗,甚至知道这个bug可能是什么???
这里是我的一些代码:
private Runnable mUpdateTimeTask = new Runnable() {
public void run() {
Log.i(ctx.getString(R.string.app_name),
"HUJIDataCollectionService, mUpdateTimeTask1 start");
setuplistenerandrequestupdates();
mHandler.removeCallbacks(mUpdateTimeTask);
}
};
private Runnable mUpdateTimeTask2 = new Runnable() {
public void run() {
Log.i(ctx.getString(R.string.app_name),"HUJIDataCollectionService, mUpdateTimeTask2 start");
// Send Data to Server here!!
sendDataToServer();
// Remove LocationUpdates here!!!
managerS.removeUpdates(listenerS);
mHandler.removeCallbacks(mUpdateTimeTask2);
}
};
private TimerTask startServiceTask = new TimerTask() {
@Override
public void run() {
Log.i(ctx.getString(R.string.app_name),"HUJIDataCollectionService, startServiceTask, run()1");
// Start Locationupdates here!!!!
longitude = 0;
latitude = 0;
locationprovider = "";
locationproviderConverted = 0;
locationAccuracy = 100000;
// remove old updates from timer tasks
mHandler.removeCallbacks(mUpdateTimeTask);
mHandler.removeCallbacks(mUpdateTimeTask2);
mHandler.postDelayed(mUpdateTimeTask, 1000);
mHandler.postDelayed(mUpdateTimeTask2,
conf_LocationUpdatePeriodInSec * 1000);
}
};
@Override
public void onStart(Intent intent, int startId) {
super.onStart(intent, startId);
ShowNotification();
LoadPreferences();
startDataCollectionServiceIntervallTimer = new Timer(
"HUJIDataCollectionServiceStartTimer");
startDataCollectionServiceIntervallTimer.schedule(startServiceTask,
1000L, conf_sampleTimeInMin * 60 * 1000L);
// Starting StopTimer(Thread) to stop listening for location updates
mHandler = new Handler();
}
private void setuplistenerandrequestupdates() {
managerS = (LocationManager) ctx
.getSystemService(Context.LOCATION_SERVICE);
listenerS = new LocationListener() {
@Override
public void onStatusChanged(String arg0, int arg1, Bundle arg2) {}
@Override
public void onProviderEnabled(String arg0) {}
@Override
public void onProviderDisabled(String arg0) {}
@Override
public void onLocationChanged(Location location) {
// storing data from the latest location update
float tempLocLongitude = (float) location.getLongitude();
float tempLocLatitude = (float) location.getLatitude();
float tempLocAccuracy = location.getAccuracy();
String tempLocProvider = location.getProvider();
// if accuracy of new location is better than existing, store
// accuracy,latitude,longitude and location provider
if ((tempLocAccuracy < locationAccuracy)
&& (tempLocAccuracy != 0.0)) {
// store current latitude
latitude = tempLocLatitude;
// store current longitude
longitude = tempLocLongitude;
// store current provider
locationprovider = tempLocProvider;
// store current accuracy
locationAccuracy = (int) tempLocAccuracy;
// converts the String value of locationprovider to the
// required integer value for the server
convertLocationProvider();
}
}
};
}
非常感谢大家!!!
可能安卓系统正在扼杀你的服务,因为你的服务寿命太长,消耗了太多资源。
如果你相信用户会高度重视你的应用程序——正如你的应用软件所写的那样,如果他们让你的应用运行,他们的电池寿命只有几个小时——那么使用startForeground()
向Android表明你的服务是前台用户体验的一部分。
就我个人而言,我建议你重新考虑你的应用程序是如何工作的,这样你就不会消耗那么多电力。例如,使用AlarmManager
和类似于我的LocationPoller
的东西,每小时检查一次位置。这不仅会减少你打开GPS的频率和时间,还会使你的服务在两次投票之间失去记忆。
您还应该使用:
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
// TODO Your code here or in onStart as you did...
return START_STICKY;
}