2012-10-07 47 views
0

我爲Android設計了一個應用程序,其中我在主要活動啓動之前顯示啓動畫面,但應用程序需要5-7秒才能啓動低端設備。我想把時間縮短到儘可能低的程度。我一直試圖減少onCreate()中的事情,但現在我無法從中刪除任何更多的東西。我正在粘貼我用來顯示啓動畫面和MainActivity代碼的代碼。請幫助我減少應用程序的啓動時間。如何讓android應用加載速度更快?

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

你在'PopulatePeopleList()'方法中做了什麼?如果您正在查詢手機上的聯繫人提供程序(並且按照您的代碼的外觀,這是您的操作),我希望您在「CursorLoader」中執行此操作,否則您可能會遇到與您看到的性能有關的問題。 – Luksprog

+0

@Luksprog是的,我在'PopulatePeopleList'中使用聯繫人提供程序..你可以向我解釋什麼是遊標加載程序,以及我如何將應用程序啓動時間devrease。 –

+0

這可能是一個不錯的主意,只是把一個啓動畫面。然後,應用程序的用戶並不認爲這是一個緩慢的加載,因爲他們專注於你的啓動畫面,一旦完成,應用程序就在那裏所有裝載完美。 – edwoollard

回答

2

你有放緩的原因是因爲你最可能的查詢手機上的聯繫人提供商,提取一些數據從這些查詢中,將它們放入mPeopleList,然後將其設置爲SimpleAdapter。因此,您的活動的onCreate方法將一直等到PopulatePeopleList()完成其工作。我不知道你是如何查詢聯繫人提供商的,但是看看你是否無法調整你的代碼來使用CursorLoader(通過兼容包提供的舊版android版本)。這將意味着您將不得不切換到基於Cursor的適配器,根據您的代碼可能進行其他更改。

如果你仍然想使用基於非SimpleAdapter你需要重寫它它們(通過兼容包再次可用在較舊的Android版本)實現自己的AsyncTaskLoader

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方法。該應用程序的啓動速度相當快,但最初它會列表爲空,直到加載程序管理完成它的工作並從聯繫人獲取數據並將其設置到適配器。

+0

我不明白你的答案的這一部分'然後你會在需要這些數據的地方執行LoaderManager。LoaderCallbacks >>,需要實現此方法的接口:' –

+0

@TapanDesai請參閱我編輯的答案。 – Luksprog

+0

感謝您節省您的時間,但是當我寫這個'公共類MainActivity實現LoaderManager.LoaderCallbacks >>'我得到的錯誤'LoaderManager無法解析到一個類型' –

相關問題