我为Android设计了一个应用程序,我在主活动开始之前显示一个启动画面,但应用程序需要5-7秒才能在低端设备上启动。我想把时间缩短到尽可能低。我一直在努力减少onCreate()
中要做的事情,但现在我无法从中删除任何更多的东西。我粘贴了用于显示来自MainActivity的启动和代码的代码。请帮我缩短申请的启动时间。
Splash.java
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_splash);
txtLoad = (TextView) findViewById(R.id.txtLoading);
txtLoad.setText("v1.0");
new Thread() {
public void run() {
try {
sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
finish();
Intent intent = new Intent(SplashActivity.this,MainActivity.class);
startActivity(intent);
}
}
}.start();
}
MainActivity.java
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
editType1UserName = (EditText) findViewById(R.id.editTextType1UserName);
editType1Password = (EditText) findViewById(R.id.editTextType1Password);
editType2UserName = (EditText) findViewById(R.id.editTextType2UserName);
editType2Password = (EditText) findViewById(R.id.editTextType2Password);
editType3UserName = (EditText) findViewById(R.id.editTextType3UserName);
editType3Password = (EditText) findViewById(R.id.editTextType3Password);
editType4UserName = (EditText) findViewById(R.id.editTextType4UserName);
editType4Password = (EditText) findViewById(R.id.editTextType4Password);
mTxtPhoneNo = (AutoCompleteTextView) findViewById(R.id.mmWhoNo);
mTxtPhoneNo.setThreshold(1);
editText = (EditText) findViewById(R.id.editTextMessage);
spinner1 = (Spinner) findViewById(R.id.spinnerGateway);
btnsend = (Button) findViewById(R.id.btnSend);
btnContact = (Button) findViewById(R.id.btnContact);
btnsend.setOnClickListener((OnClickListener) this);
btnContact.setOnClickListener((OnClickListener) this);
mPeopleList = new ArrayList<Map<String, String>>();
PopulatePeopleList();
mAdapter = new SimpleAdapter(this, mPeopleList, R.layout.custcontview,
new String[] { "Name", "Phone", "Type" }, new int[] {
R.id.ccontName, R.id.ccontNo, R.id.ccontType });
mTxtPhoneNo.setAdapter(mAdapter);
mTxtPhoneNo.setOnItemClickListener((OnItemClickListener) this);
readPerson();
Panel panel;
topPanel = panel = (Panel) findViewById(R.id.mytopPanel);
panel.setOnPanelListener((OnPanelListener) this);
panel.setInterpolator(new BounceInterpolator(Type.OUT));
getLoginDetails();
}
答案 0 :(得分:2)
您放缓的原因是您最有可能在手机上查询联系人提供商,从这些查询中提取一些数据,将其放入mPeopleList
,然后将其设置为SimpleAdapter
。因此,您的活动的onCreate
方法会等到PopulatePeopleList()
完成其工作。我不知道你是如何查询联系人提供者,但看看你是否不能调整你的代码使用CursorLoader
(通过兼容包在较旧的Android版本中可用)。这意味着您必须切换到基于Cursor
的适配器,可能会有其他更改,具体取决于您的代码。
如果您仍然想要使用非基础SimpleAdapter
,则需要覆盖它们,然后实现您自己的AsyncTaskLoader
(通过兼容包再次在较旧的Android版本中提供):
public class ContactsDataLoader extends
AsyncTaskLoader<ArrayList<Map<String, String>>> {
public ContactsDataLoader(Context context) {
super(context);
}
@Override
public ArrayList<Map<String, String>> loadInBackground() {
// here do what you do in the PopulatePeopleList() method
// this will be done in another thread so the activity will initially
// start empty(set an empty mPeoples list to the SimpleAdapter) and as
// this loader finishes its job you'll have the list filled with the
// data that is returned here
return data;
}
@Override
protected void onStartLoading() {
super.onStartLoading();
forceLoad();
}
}
然后,您将拥有需要此数据的活动,并实施LoaderManager.LoaderCallbacks<ArrayList<Map<String, String>>>
:
public class MainActivity implements LoaderManager.LoaderCallbacks<ArrayList<Map<String, String>>>
需要定义此方法的接口:
@Override
public Loader<ArrayList<Map<String, String>>> onCreateLoader(int id,
Bundle args) {
return new ContactsDataLoader(context);
}
@Override
public void onLoadFinished(Loader<ArrayList<Map<String, String>>> loader,
ArrayList<Map<String, String>> data) {
// your custom adapter will need a method to update its data
adapter.changeData(data);
// you always have the option of using a normal SimpleAdapter and create
// a new instance each time the data changes
// mPeopleList = data;
// mAdapter = new SimpleAdapter(this, mPeopleList,
// R.layout.custcontview,
// new String[] { "Name", "Phone", "Type" }, new int[] {
// R.id.ccontName, R.id.ccontNo, R.id.ccontType });
// mTxtPhoneNo.setAdapter(mAdapter);
}
@Override
public void onLoaderReset(Loader<ArrayList<Map<String, String>>> loader) {
// your custom adapter will need a method to update its data
adapter.changeData(null); // or an empty list of data
// you always have the option of using a normal SimpleAdapter and create
// a new instance each time the data changes
// mPeopleList = new ArrayList<Map<String, String>>;
// mAdapter = new SimpleAdapter(this, mPeopleList,
// R.layout.custcontview,
// new String[] { "Name", "Phone", "Type" }, new int[] {
// R.id.ccontName, R.id.ccontNo, R.id.ccontType });
// mTxtPhoneNo.setAdapter(mAdapter);
}
然后你所要做的就是打电话:
// mPeopleList will have to be initialized to an empty list in the `onCreate` method
getLoaderManager().initLoader(0, null, this);
在您的onCreate
方法中。应用程序启动速度非常快,但最初会将列表清空,直到加载程序设法完成工作并从联系人处获取数据并将其设置为适配器。