我有一个意图服务,其中有一个用于不同动作的开关/外壳。在大多数这些操作中,都会执行数据库操作。现在,我创建了一个单独的类,该类在每种操作情况下都可以扩展AsyncTask,但是我不知道这是否是解决此问题的正确方法。
您能给我一些建议吗,您将如何做或对我的代码有何看法?
switch (intent.getAction()) {
case LOGIN_ACTION:
Log.d(TAG, "Habe den Intent: " + LOGIN_ACTION + " erhalten.");
String username = extras.getString(USERNAME_KEY, "");
String password = extras.getString(PASSWORD_KEY, "");
Log.d(TAG, "Habe die Werte: " + username + ", " + password + " erhalten.");
LoginRequest loginRequest = new LoginRequest(username, password);
Call<LoginResponse> callable = service.login(loginRequest);
callable.enqueue(new Callback<LoginResponse>() {
@Override
public void onResponse(Call<LoginResponse> call, Response<LoginResponse> response) {
Log.d(TAG, "onResponse: LOGIN SUCCESS ! => AT: " + response.body().access_token);
// Profile roomProfile = new Profile(...)
class DBInsertTask extends AsyncTask {
@Override
protected Object doInBackground(Object[] objects) {
database.dbdao().insertProfile(roomProfile);
return null;
}
}
DBInsertTask dbInsertTask = new DBInsertTask();
dbInsertTask.execute();
Intent sendLoginBroadCast = new Intent(getApplicationContext(), ALGodObject.ALGodObjectBroadcastReceiver.class);
sendLoginBroadCast.putExtra(ACCESS_TOKEN, response.body().access_token);
sendLoginBroadCast.setAction(ALGodObject.LOGIN_RESPONSE);
sendBroadcast(sendLoginBroadCast);
Log.d(TAG, "Intent: " + sendLoginBroadCast + " gesendet.");
}
@Override
public void onFailure(Call<LoginResponse> call, Throwable t) {
Log.e(TAG, "onFailure: INITAL FILE LOAD FAILED!");
}
});
break;
非常感谢! 祝你有美好的一天!