【发布时间】:2016-10-25 21:02:56
【问题描述】:
我正在开发一个使用共享首选项文件并且还包含广告的应用。当我第一次打开我的应用程序(从 android studio 运行)时,我的主要活动需要 14-16 秒才能加载。缓存后需要 2 秒才能加载。我意识到我在 onCreate() 方法中放置了太多操作。然后我尝试使用 onResume(),但它仍然需要相同的时间。我想知道如何减少这个启动时间。我的应用程序使用共享首选项文件,其中包含广告。我还注意到我的应用程序缓存为 20MB。 代码如下
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
EditText nme_key = (EditText)findViewById(R.id.name_key);
EditText cls_key = (EditText)findViewById(R.id.class_key);
EditText num_key = (EditText)findViewById(R.id.number_key);
SharedPreferences sharedPreferences = getSharedPreferences(getString(R.string.preference_file_key),MODE_PRIVATE);
nme_key.setText(sharedPreferences.getString("name","name"));
cls_key.setText(sharedPreferences.getString("class","class"));
num_key.setText(sharedPreferences.getString("number","number"));
MobileAds.initialize(getApplicationContext(), "");
mAdView = (AdView) findViewById(R.id.adView);
AdRequest adRequest = new AdRequest.Builder().addTestDevice("").build();
mAdView.loadAd(adRequest);
}
我有 3 个问题
如何减少我的应用程序的启动时间(线程?)
如何减少我的应用程序的缓存大小
如何提高应用性能
【问题讨论】:
-
尝试删除广告,看看会发生什么。
-
花了 12 秒,还是很多不是吗
-
这也取决于手机。对于第一次启动,您可以使用SplashScreen。还有很多文章展示了如何减小尺寸和提高性能。
-
你在使用自定义的
Application类吗? -
就像@njzk2 说的,你需要弄清楚什么是花时间。注释掉所有内容并一次添加一个,直到您看到加载时间的跳跃。然后您就知道这就是花费时间的原因,您可以深入了解原因。
标签: android performance oncreate