0
我想使用android.app.Service(不是IntentService,不是AsyncTask)來運行我的長時間運行的進程並調用Handler .sendMessage(..)在過程中。Android:如何創建一個在後臺運行的服務並調用Handler.sendMessage(..)
使用下面的代碼就夠了(Service的onCreate方法)?
@Override
public void onCreate() {
new Thread(new Runnable() {
@Override
public void run() {
Log.w(LOG_LOGCAT_TAG, "DEBUG MODE - CallbackImpl handleError() called");
// get the location information (lat,lon,altitude etc..)
// then, call callback methods.
_locationProvider.getLocation(_locationCallbackImpl);
}
}).start();
}
private final LocationCallbackImpl _locationCallbackImpl = new LocationCallbackImpl();
private class LocationCallbackImpl
implements
LocationCallback
{
@Override
public void done() {
Log.i(LOG_LOGCAT_TAG, "LocationCallbackImpl done() called");
// switch to UI thread
_handler.sendMessage(_handler.obtainMessage(DONE_MESSAGE));
}
@Override
public void handleError(ReturnCode returnCode) {
Log.e(LOG_LOGCAT_TAG, "LocationCallbackImpl handleError() called:" + returnCode.name());
// send a message to display the error
_handler.sendMessage(_handler.obtainMessage(ERROR_MESSAGE, returnCode));
}
@Override
public void handleSuccess(Location location) {
Log.i(LOG_LOGCAT_TAG, "LocationCallbackImpl handleSuccess() called");
_handler.sendMessage(_handler.obtainMessage(LOCATION_MESSAGE, location));
}
}
並且還想問你們,如果所有的回調函數都會在上面創建的線程中調用?
如何檢查它們是否真的在該線程上運行?有沒有辦法檢查它?
你會修改我的示例代碼來爲Handler應用getter/setter嗎? – eros