朋友们,
我创建了一个每 2 分钟调用一次的 android 后台服务。当它被调用时,它开始监听 GPS 和 NETWORK 的 locationUpdates 40 秒。提取有用信息(纬度、经度、准确性、提供者……)并将数据发送到服务器。
当我在模拟器上运行应用程序时,它工作正常。(嗯,我没有得到真正的 gps 信息,只是所有变量的默认值,但我知道我的服务每两分钟调用一次并将数据发送到服务器)
因此,当我在真实设备上运行该应用程序时,它可以完美运行 40 分钟。然后,我想,有些东西正在关闭我的服务。(可能是由于内存消耗或其他原因而导致的android本身)
有没有人知道我如何追踪错误,甚至可能是什么错误???
这是我的一些代码:
private Runnable mUpdateTimeTask = new Runnable() {
public void run() {
Log.i(ctx.getString(R.string.app_name),
"HUJIDataCollectionService, mUpdateTimeTask1 start");
setuplistenerandrequestupdates();
mHandler.removeCallbacks(mUpdateTimeTask);
//! Log.i(ctx.getString(R.string.app_name),"HUJIDataCollectionService, mUpdateTimeTask stop");
}
};
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);
//! Log.i(ctx.getString(R.string.app_name),"HUJIDataCollectionService, mUpdateTimeTask2 stop");
}
};
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);
//! Log.i(ctx.getString(R.string.app_name),"HUJIDataCollectionService, onStart");
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() {
//! Log.i(ctx.getString(R.string.app_name),"HUJIDataCollectionService, setupListenerAndRequestUpdates");
managerS = (LocationManager) ctx
.getSystemService(Context.LOCATION_SERVICE);
//! Log.i(ctx.getString(R.string.app_name), "HUJIDataCollectionService, 1");
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();
//Log.i(ctx.getString(R.string.app_name),
// "HUJIDataCollectionService, setupListenerAndRequestUpdates, location Changed");
// 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();
}
}
};
非常感谢各位!!!