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()その連絡先プロバイダーにクエリを実行する方法はわかりませんが、コードを使用するように適応できないかどうかを確認してください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 に答える