如何使Android应用程序加载速度更快?更快、应用程序、加载、速度

2023-09-12 06:00:44 作者:心尖上的刺青

我设计的应用程序为Android,在我显示启动画面的主要活动开始之前,但应用程序需要5-7秒开始在低端设备。我想,以减少时间尽可能低。我一直在试图减少的事情在的onCreate()做,但现在我不能从删除任何东西更多。我贴,我已经习惯了显示启动,并从MainActivity的code中的code。请帮我减少了应用程序的启动时间。

Splash.java

  @覆盖
公共无效的onCreate(包savedInstanceState){
    super.onCreate(savedInstanceState);
    this.requestWindowFeature(Window.FEATURE_NO_TITLE);
    的setContentView(R.layout.activity_splash);
    txtLoad =(TextView中)findViewById(R.id.txtLoading);
    txtLoad.setText(1.0);

    新的Thread(){
        公共无效的run(){
            尝试 {
                睡眠(1000);
            }赶上(InterruptedException异常E){
                e.printStackTrace();
            } 最后 {
                完();
                意向意图=新的意图(SplashActivity.this,MainActivity.class);
                startActivity(意向);
            }
        }
    }。开始();
}
 

MainActivity.java

  @覆盖
公共无效的onCreate(包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 =(微调)findViewById(R.id.spinnerGateway);
    btnsend =(按钮)findViewById(R.id.btnSend);
    btnContact =(按钮)findViewById(R.id.btnContact);
    btnsend.setOnClickListener((OnClickListener)本);
    btnContact.setOnClickListener((OnClickListener)本);
    mPeopleList =新的ArrayList<地图<字符串,字符串>>();
    PopulatePeopleList();
    mAdapter =新SimpleAdapter(这一点,mPeopleList,R.layout.custcontview,
            新的String [] {姓名,电话,类型},新的INT [] {
                    R.id.ccontName,R.id.ccontNo,R.id.ccontType});
    mTxtPhoneNo.setAdapter(mAdapter);
    mTxtPhoneNo.setOnItemClickListener((OnItemClickListener)本);
    readPerson();
    面板面板;
    topPanel = =面板(面板)findViewById(R.id.mytopPanel);
    panel.setOnPanelListener((OnPanelListener)本);
    panel.setInterpolator(新BounceInterpolator(Type.OUT));
    getLoginDetails();

}
 

解决方案

你有增长放缓的原因是因为你最有可能查询手机上的联系人提供商,提取这些查询一些数据,把它们放在 mPeopleList 键,然后将其设置为你的 SimpleAdapter 。所以,你的活动的的onCreate 方法将等待 PopulatePeopleList()完成自己的工作。我不知道你是怎么查询接触供应商,而是看你能不能适应你的code使用的 CursorLoader (通过兼容包在旧的Andr​​oid版本可用)。这将意味着你将有,其它可能的变化取决于您的code切换到光标的适配器。

Android 11 Go版本发布 支持2GB内存 应用启动速度快20

如果你仍然想使用基于非 SimpleAdapter 你需要重写它他们实现自己的 AsyncTaskLoader (在旧的Andr​​oid版本又可以通过兼容包):

 公共类ContactsDataLoader扩展
        AsyncTaskLoader< ArrayList的<地图<字符串,字符串>>> {

    公共ContactsDataLoader(上下文的背景下){
        超(上下文);
    }

    @覆盖
    公众的ArrayList<地图<字符串,字符串>> loadInBackground(){
        //这里做你的PopulatePeopleList做()方法
        //这会在另一个线程来完成这样的活动将开始
        //启动空(设置空mPeoples列表中SimpleAdapter),并作为
        //这个程序结束时它的工作你必须充满名单
        这里返回的//数据
        返回的数据;
    }

    @覆盖
    保护无效onStartLoading(){
        super.onStartLoading();
        的forceload();
    }

}
 

那么你就必须在你需要该数据的活动,实施 LoaderManager.LoaderCallbacks< ArrayList的<地图<字符串,字符串>>>

 公共类MainActivity实现LoaderManager.LoaderCallbacks< ArrayList的<地图<字符串,字符串>>>
 

接口,需要这个方法来定义的:

  @覆盖
    公共装载机< ArrayList的<地图<字符串,字符串>>> onCreateLoader(INT ID,
            捆绑参数){
        返回新ContactsDataLoader(上下文);
    }

    @覆盖
    公共无效onLoadFinished(装载机< ArrayList的<地图<字符串,字符串>>>装载机,
            ArrayList的<地图<字符串,字符串>>数据) {
        //你的自定义适配器将需要一种方法来更新其数据
        adapter.changeData(数据);
        //你总是有使用普通SimpleAdapter的选择和创造
        //每一次一个新的实例中的数据变化
        // mPeopleList =数据;
        // mAdapter =新SimpleAdapter(这一点,mPeopleList,
        // R.layout.custcontview,
        //新的String [] {姓名,电话,类型},新的INT [] {
        // R.id.ccontName,R.id.ccontNo,R.id.ccontType});
        // mTxtPhoneNo.setAdapter(mAdapter);
    }

    @覆盖
    公共无效onLoaderReset(装载机< ArrayList的<地图<字符串,字符串>>>装载机){
        //你的自定义适配器将需要一种方法来更新其数据
        adapter.changeData(空); //或数据的空列表
        //你总是有使用普通SimpleAdapter的选择和创造
        //每一次一个新的实例中的数据变化
        // mPeopleList =新的ArrayList<地图<字符串,字符串>取代;
        // mAdapter =新SimpleAdapter(这一点,mPeopleList,
        // R.layout.custcontview,
        //新的String [] {姓名,电话,类型},新的INT [] {
        // R.id.ccontName,R.id.ccontNo,R.id.ccontType});
        // mTxtPhoneNo.setAdapter(mAdapter);
    }
 

然后,所有你需要做的就是调用:

  // mPeopleList将在`onCreate`方法被初始化为一个空列表
getLoaderManager()initLoader(0,空,这一点)。
 

的onCreate 方法。该应用程序将启动pretty的快,但都会有其表空开始,直到装载机设法做的工作,并从接触该数据并将其设置为您的适配器。

I have designed an application for android, in which i am showing a splash screen before the main activity is started but the application takes 5-7 seconds to start on low-end devices. I want to reduce that time to as low as possible. I have been trying to reduce the things to be done in onCreate() but now i cannot remove any thing more from that. I am pasting the code that i have used to show the splash and the code from MainActivity. Please help me in reducing the startup time of the application.

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();

}

解决方案

The reason you have that slowdown is because you most likely query the contacts provider on the phone, extract some data from those queries, put them in mPeopleList and then set it to your SimpleAdapter. So your activity's onCreate method waits until PopulatePeopleList() finishes its job. I don't know how you query that contacts provider but see if you can't adapt your code to use a CursorLoader(available in older android version through the compatibility package). This will mean that you would have to switch to a Cursor based adapter, possible other changes depending on your code.

If you still want to use a non based SimpleAdapter you'll need to override it them implement your own AsyncTaskLoader(again available in older android version through the compatibility package):

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();
    }

}

Then you'll have the activity where you need this data, implement LoaderManager.LoaderCallbacks<ArrayList<Map<String, String>>> :

public class MainActivity implements LoaderManager.LoaderCallbacks<ArrayList<Map<String, String>>>

interface that needs this methods defined:

@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);
    }

Then all you have to do is call:

// mPeopleList will have to be initialized to an empty list in the `onCreate` method
getLoaderManager().initLoader(0, null, this); 

in your onCreate method. The app will start pretty fast but will have it's list empty initially until the loader manages to do it's work and get the data from the contacts and set it to your adapter.