我有两个Activities
名为LoginActivity
和RegistrationActivity
。我使用RegistrationActivity
点击按钮,从LoginActivity
开始Intent
。但问题是加载RegistrationActivity
的延迟2到3秒。可能是什么问题?
LoginActivity
:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_login);
setup();
}
private void setup() {
layout = (RelativeLayout) findViewById(R.id.loginLayout);
register = (Button) findViewById(R.id.registerText);
email = (AppCompatEditText) findViewById(R.id.loginEmail);
password = (AppCompatEditText) findViewById(R.id.loginPassword);
submit = (Button) findViewById(R.id.loginSubmit);
submit.setOnClickListener(listener);
register.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(LoginActivity.this, RegistrationActivity.class);
ActivityOptionsCompat options = ActivityOptionsCompat.makeScaleUpAnimation(view, 0, 0, view.getWidth(), view.getHeight());
ActivityCompat.startActivity(LoginActivity.this,
intent, options.toBundle());
}
});
}
RegistrationActivity
:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_registration);
}
@Override
protected void onStart() {
super.onStart();
setup();
new getLatLongAsync().execute();
}
private class getLatLongAsync extends AsyncTask<String, Void, String> {
@Override
protected void onPreExecute() {
}
@Override
protected String doInBackground(String... args) {
mGoogleApiClient = new GoogleApiClient.Builder(RegistrationActivity.this)
// The next two lines tell the new client that “this” current class will handle connection stuff
.addConnectionCallbacks(RegistrationActivity.this)
.addOnConnectionFailedListener(RegistrationActivity.this)
//fourth line adds the LocationServices API endpoint from GooglePlayServices
.addApi(LocationServices.API)
.build();
mLocationRequest = LocationRequest.create()
.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY)
.setInterval(10 * 1000) // 10 seconds, in milliseconds
.setFastestInterval(1000); // 1 second, in milliseconds
return null;
}
protected void onPostExecute(String args) {
mGoogleApiClient.connect();
}
}
getLatLong()
获取纬度和经度:
private void getLatLong() {
mGoogleApiClient = new GoogleApiClient.Builder(this)
// The next two lines tell the new client that “this” current class will handle connection stuff
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
//fourth line adds the LocationServices API endpoint from GooglePlayServices
.addApi(LocationServices.API)
.build();
mLocationRequest = LocationRequest.create()
.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY)
.setInterval(10 * 1000) // 10 seconds, in milliseconds
.setFastestInterval(1000); // 1 second, in milliseconds
}
答案 0 :(得分:2)
实际上你正试图在onCreate
方法上加载很多东西。由于您的活动需要时间来创作。
对于你的案例,将所有setup()
和你的asyntask放在onStart或任何其他适当的方法上。
非常感谢你。
答案 1 :(得分:1)
RegistrationActivity:更新代码
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_registration);
setup();
//calling asynctask..
new getLatLongAsync().execute();
}
private void setup() {
layout = (RelativeLayout) findViewById(R.id.registerLayout);
language = (Spinner) findViewById(R.id.lang);
email = (EditText) findViewById(R.id.regEmail);
password = (EditText) findViewById(R.id.regPassword);
nickname = (EditText) findViewById(R.id.regNickname);
username = (EditText) findViewById(R.id.regUsername);
dob = (EditText) findViewById(R.id.regDOB);
submit = (Button) findViewById(R.id.regSubmit);
prefs = new SecurePreferences(this);
progressDialog = new ProgressDialog(this);
progressDialog.setMessage("Loading....");
language.setOnItemSelectedListener(listener);
submit.setOnClickListener(onClickListener);
languages = new ArrayList<>();
languages.add("Select Language");
languages.add("English");
languages.add("Tamil");
languages.add("Malayalam");
adapter = new ArrayAdapter<>(this, android.R.layout.simple_spinner_item, languages);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
language.setAdapter(adapter);
}
class getLatLongAsync extends AsyncTask<String, Void, String> {
@Override
protected void onPreExecute() {
}
@Override
protected String doInBackground(String... args) {
getLatLong();
return null;
}
protected void onPostExecute(String args) {
}
}
private void getLatLong() {
mGoogleApiClient = new GoogleApiClient.Builder(this)
// The next two lines tell the new client that “this” current class will handle connection stuff
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
//fourth line adds the LocationServices API endpoint from GooglePlayServices
.addApi(LocationServices.API)
.build();
mLocationRequest = LocationRequest.create()
.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY)
.setInterval(10 * 1000) // 10 seconds, in milliseconds
.setFastestInterval(1000); // 1 second, in milliseconds
}