Lint使IntentService构造函数成为私有

时间:2016-04-08 10:46:55

标签: android lint

如果我使用模板

public class EditProfileIntentService extends IntentService {
  private static final String USENAME = "username";
  private static final String LASTNAME = "lastname";
  private static final String PHONE = "phone";
  private static final String EMAIL = "email";
  private Handler handler;
  private Context mContext;

  public EditProfileIntentService() {
    super("EditProfileIntentService");
  }

  public static void edit(Context context, String username, String lastname, String email,
      String phone) {
    Intent intent = new Intent(context, EditProfileIntentService.class);
    intent.putExtra(USENAME, username);
    intent.putExtra(LASTNAME, lastname);
    intent.putExtra(EMAIL, email);
    intent.putExtra(PHONE, phone);

    context.startService(intent);
  }

  @Override public int onStartCommand(Intent intent, int flags, int startId) {
    handler = new Handler();
    mContext = this;
    return super.onStartCommand(intent, flags, startId);
  }

  @Override protected void onHandleIntent(Intent intent) {

    if (intent != null) {
      final String username = intent.getStringExtra(USENAME);
      final String lastname = intent.getStringExtra(LASTNAME);
      final String email = intent.getStringExtra(EMAIL);
      final String phone = intent.getStringExtra(PHONE);

      try {
        final ResponseContainer container =
            RestClient.get().editProfile(Token.getToken(this), username, lastname, phone, email);
        if (container.getSucess().equals(1)) {
          ProfileEntity profileEntity = Token.getProfile(this);
          profileEntity.setEmail(email);
          profileEntity.setPhone(phone);
          profileEntity.setUsername(username);
          profileEntity.setLastname(lastname);
          Token.saveProfile(this, profileEntity);
          handler.post(new Runnable() {
            @Override public void run() {
              Toast.makeText(mContext, R.string.edit_profile_success, Toast.LENGTH_SHORT).show();
            }
          });
        } else {
          handler.post(new Runnable() {
            @Override public void run() {
              Toast.makeText(mContext, container.getMessage(), Toast.LENGTH_SHORT).show();
            }
          });
        }
      } catch (RetrofitError e) {
        handler.post(new Runnable() {
          @Override public void run() {
            Toast.makeText(mContext, R.string.auth_rg_network_error, Toast.LENGTH_SHORT).show();
          }
        });
      }
      EventBus.getDefault().postSticky(new FinishProfileEdit());
    }
  }
}

然后lint建议将构造函数设为私有,这将使服务无法访问并抛出

java.lang.RuntimeException: Unable to instantiate service ua.in.femid.controller.services.EditProfileIntentService: java.lang.IllegalAccessException: access to constructor not allowed

这是一个错误吗?如何使lint永远不建议使compulsury公共领域受到保护或私有?

编辑该服务仅通过方法edit启动 EDIT2
将初始化器从构造函数移动到onStartCommand,lint仍然希望使构造函数成为私有的 EDIT3
Lint建议对那些IntentServices进行构造函数私有,它遵循android studio模板,并且有静态方法将参数设置为bandle并像编辑方法一样调用startService

0 个答案:

没有答案