【问题标题】:Reduce android app startup time减少安卓应用启动时间
【发布时间】: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 个问题

  1. 如何减少我的应用程序的启动时间(线程?)

  2. 如何减少我的应用程序的缓存大小

  3. 如何提高应用性能

【问题讨论】:

  • 尝试删除广告,看看会发生什么。
  • 花了 12 秒,还是很多不是吗
  • 这也取决于手机。对于第一次启动,您可以使用SplashScreen。还有很多文章展示了如何减小尺寸和提高性能。
  • 你在使用自定义的Application 类吗?
  • 就像@njzk2 说的,你需要弄清楚什么是花时间。注释掉所有内容并一次添加一个,直到您看到加载时间的跳跃。然后您就知道这就是花费时间的原因,您可以深入了解原因。

标签: android performance oncreate


【解决方案1】:

我发现广告是导致我的应用程序启动时间过长的原因。

所以,我是这样解决的:

//run in the background
AsyncTask.execute(() -> {
            MobileAds.initialize(this, initializationStatus -> {});
            //Banner Ad
            AdView adView = findViewById(R.id.ad_view);
            AdRequest adRequest = new AdRequest.Builder().build();

            //Interstitial Ad
            mInterstitialAd = new InterstitialAd(this);
            mInterstitialAd.setAdUnitId("ca-app-pub-3940256099942544/1033173712");


            //you have to load the Ads in the foreground
            this.runOnUiThread(() -> {
                adView.loadAd(adRequest);
                mInterstitialAd.loadAd(new AdRequest.Builder().build());

            });
        });

如何展示插页式广告:

button.setOnClickListener(view -> {

            //check first, if it is instantiated and loaded
            if (mInterstitialAd != null && mInterstitialAd.isLoaded()) {
                mInterstitialAd.show();
            }
        });

但请注意,这将使您的应用在加载之前不会显示任何广告。 我知道我来晚了,但我希望这能帮助该功能的人们。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-12-02
    • 2011-05-12
    • 1970-01-01
    • 1970-01-01
    • 2018-04-12
    • 2013-08-29
    • 1970-01-01
    • 2017-05-11
    相关资源
    最近更新 更多