在我的应用程序中,我正在使用后台服务, 这是它的基本代码
<application
android:allowBackup="true"
android:vmSafeMode="true"
android:allowClearUserData="true"
android:hardwareAccelerated="true"
android:largeHeap="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:name=".MyApplication"
android:theme="@style/AppTheme" >
Service contains some timers
public class ConnectionService extends Service
{
private final IBinder mBinder = new ConnectionServiceBinder();
private MyApplication app;
private CountDownTimer mAliveTimer;
public CountDownTimer mPanicTimer;
public CountDownTimer mPerodicTimer;
/* Not using binder anywhere right now */
public class ConnectionServiceBinder extends Binder
{
public ConnectionService getService()
{
return ConnectionService.this;
}
}
@Override
public int onStartCommand(Intent intent, int flags, int startId)
{
//connect();
return START_STICKY;
}
public void createPerodicTimer()
{
if (mPerodicTimer != null)
mPerodicTimer.cancel();
float fastestInterval = Preferences.getFloatSharedPrefValue(app, Constants.USERDEFAULT_SETTINGS_TIME, 5f) * 60 * 1000;
mPerodicTimer = new CountDownTimer((int)fastestInterval, 1000)
{
@Override
public void onTick(long millisUntilFinished)
{}
@Override
public void onFinish()
{
Location myLocation = app.myLocationProvider.getMyLocation();
Location currentBestLocation = null;
String locValue = Preferences.getSharedPrefValue(app, Constants.USERDEFAULT_LOCATION);
if (locValue != null)
{
currentBestLocation = new Gson().fromJson(locValue, Location.class);
}
if (currentBestLocation == null)
{
sendPerodicReporting();
Preferences.saveSharedPrefValue(app, Constants.USERDEFAULT_LOCATION , new Gson().toJson(myLocation));
}
else
{
float displacement = Preferences.getFloatSharedPrefValue(app, Constants.USERDEFAULT_SETTINGS_DISTANCE, 50);
float dist = myLocation.distanceTo(currentBestLocation);
if (dist >= displacement)
{
sendPerodicReporting();
}
else
{
ApiManager.getInstance().mDbHelper.addToLog("System Event", "Perodic Report not sent", "Distance travelled is less than set displacement settings" + dist + " < " + displacement, app);
}
}
Preferences.saveSharedPrefValue(app, Constants.USERDEFAULT_LOCATION , new Gson().toJson(myLocation));
mPerodicTimer.start();
}
};
mPerodicTimer.start();
}
@Override
public void onCreate()
{
super.onCreate();
app = (MyApplication) getApplication();
app.mService = this;
app.addLocationUpdates();
createPerodicTimer();
mAliveTimer = new CountDownTimer(30000 * 60, 1000) // Keep Alive Timer is 30 minutes
{
@Override
public void onTick(long millisUntilFinished)
{}
@Override
public void onFinish()
{
if (Preferences.getBoolSharedPrefValue(app.getApplicationContext(), Constants.USERDEFAULT_SETTINGS_ALIVE, false))
sendAliveResponse();
if (Preferences.getBoolSharedPrefValue(app.getApplicationContext(), Constants.USERDEFAULT_IS_PANIC_ENABLED, false))
sendPanicReporting();
mAliveTimer.start();
}
};
mPanicTimer = new CountDownTimer(30000, 1000) // Keep Alive Timer is 30 seconds
{
@Override
public void onTick(long millisUntilFinished)
{}
@Override
public void onFinish()
{
if (Preferences.getBoolSharedPrefValue(app.getApplicationContext(), Constants.USERDEFAULT_IS_PANIC_ENABLED, false))
{
sendPanicReporting();
mPanicTimer.start();
}
}
};
mAliveTimer.start();
}
@Override
public void onDestroy()
{
super.onDestroy();
}
@Override
public IBinder onBind(Intent arg0)
{
return mBinder;
}
}
有时在申请之后,让我们说在一分钟之后我会崩溃
致命信号11(SIGSEGV)代码= 2 我一直试图寻找解决方案,但找不到任何可行的工作。
如果我从我的应用程序中删除服务,应用程序工作正常。
如何解决此错误?
答案 0 :(得分:0)
我不确定(在没有完整堆栈跟踪的情况下弄清楚发生了什么有点困难),但尝试禁用服务中的共享首选项保存并检查应用程序是否失败。我认为这个问题可能是由Gson序列化产生的字符串引起的。
我检查了行
返回的JsonElement的实现new Gson().toJson(...)
似乎它没有实现 java.io.Serializable 接口。这可能会在对象保存期间导致问题。