0

我为 android 设计了一个应用程序,其中我在主要活动启动之前显示了一个启动屏幕,但该应用程序需要 5-7 秒才能在低端设备上启动。我想将时间减少到尽可能低。我一直在努力减少要做的事情,onCreate()但现在我不能再从中删除任何东西了。我正在粘贴用于显示启动画面的代码和 MainActivity 中的代码。请帮助我减少应用程序的启动时间。

飞溅.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();

}
4

1 回答 1

2

速度变慢的原因是您很可能在手机上查询联系人提供程序,从这些查询中提取一些数据,将它们放入mPeopleList,然后将其设置为您的SimpleAdapter. 因此,您的活动的onCreate方法一直等到PopulatePeopleList()完成其工作。我不知道您如何查询该联系人提供者,但看看您是否无法调整您的代码以使用 a 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方法中。该应用程序将很快启动,但最初将其列表为空,直到加载器设法完成它的工作并从联系人获取数据并将其设置到您的适配器。

于 2012-10-07T08:49:30.847 回答