在SplashScreen ANDROID STUDIO之后启动白屏

时间:2017-05-31 13:13:53

标签: screen startup

我有一个WebView应用程序,可以在移动版本中打开我的网站。 我的问题在我创建启动画面时开始,就像我启动应用程序时显示启动画面一样,但是在显示启动画面时它不会加载页面。 因此,只有当启动画面消失时,应用程序才会开始加载页面,这意味着屏幕会变为白色,直到网站加载...

我该如何解决这个问题? 以下是我的应用程序的代码和屏幕截图。

溅射屏幕:

SplashScreen之后:

代码:

的AndroidManifest.xml

    <manifest
    package="com.sherdle.lancamentoimoveis2"
    xmlns:android="http://schemas.android.com/apk/res/android">

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
    <uses-permission android:name="android.permission.VIBRATE" />
    <uses-permission android:name="android.permission.WAKE_LOCK" />
    <uses-permission android:name="android.permission.GET_ACCOUNTS" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" 
    />

    <permission 
    android:name="com.sherdle.lancamentoimoveis2.permission.C2D_MESSAGE" 
    android:protectionLevel="signature" />
    <uses-permission android:name="com.sherdle.webtoapp.permission.C2D_MESSAGE" 
    />

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme.Toolbar"
        android:name="com.sherdle.lancamentoimoveis2.App">
        <meta-data android:name="com.google.android.gms.version"
            android:value="@integer/google_play_services_version"/>


        <activity

            android:name="com.sherdle.lancamentoimoveis2.activity.Splashscreen" android:theme="@style/Theme.Transparent"
            android:label="@string/app_name">

            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>


        </activity>


        <activity
            android:name="com.sherdle.lancamentoimoveis2.activity.MainActivity"
            android:configChanges="keyboardHidden|keyboard|orientation|screenSize"
            android:hardwareAccelerated="true"
            android:windowSoftInputMode="adjustResize"
            android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
            <!--
            <intent-filter>
                <action android:name="android.intent.action.VIEW"></action>
                <category android:name="android.intent.category.DEFAULT"></category>
                <category android:name="android.intent.category.BROWSABLE"></category>
                <data android:scheme="http" android:host="mysite.com" ></data>
                <data android:scheme="http" android:host="*.mysite.com" ></data>
                <data android:scheme="https" android:host="mysite.com" ></data>
                <data android:scheme="https" android:host="*.mysite.com" ></data>
            </intent-filter>-->
        </activity>

        <!-- ADS -->
        <activity android:name="com.google.android.gms.ads.AdActivity"
            android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"/>

        <!-- Analytics-->
        <receiver android:name="com.google.android.gms.analytics.AnalyticsReceiver"
            android:enabled="true">
            <intent-filter>
                <action android:name="com.google.android.gms.analytics.ANALYTICS_DISPATCH" />
            </intent-filter>
        </receiver>
        <service android:name="com.google.android.gms.analytics.AnalyticsService"
            android:enabled="true"
            android:exported="false"/>

        <receiver android:name="com.google.android.gms.analytics.CampaignTrackingReceiver"
            android:exported="true">
            <intent-filter>
                <action android:name="com.android.vending.INSTALL_REFERRER" />
            </intent-filter>
        </receiver>
        <service android:name="com.google.android.gms.analytics.CampaignTrackingService" />

    </application>

</manifest>

SplashScreen.java

public class Splashscreen extends Activity {
public void onAttachedToWindow() {
    super.onAttachedToWindow();
    Window window = getWindow();
    window.setFormat(PixelFormat.RGBA_8888);
}
/** Called when the activity is first created. */
Thread splashTread;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_splashscreen);

    StartAnimations();
}
private void StartAnimations() {
    Animation anim = AnimationUtils.loadAnimation(this, R.anim.alpha);
    anim.reset();
    LinearLayout l=(LinearLayout) findViewById(R.id.lin_lay);
    l.clearAnimation();
    l.startAnimation(anim);

    anim = AnimationUtils.loadAnimation(this, R.anim.translate);
    anim.reset();
    ImageView iv = (ImageView) findViewById(R.id.splash);
    iv.clearAnimation();
    iv.startAnimation(anim);

    splashTread = new Thread() {
        @Override
        public void run() {
            try {
                int waited = 0;
                // Splash screen pause time
                while (waited < 10000) {
                    sleep(100);
                    waited += 100;
                }
                Intent intent = new Intent(Splashscreen.this,
                        MainActivity.class);
                intent.setFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
                startActivity(intent);
                Splashscreen.this.finish();
            } catch (InterruptedException e) {
                // do nothing
            } finally {
                Splashscreen.this.finish();
            }

        }
    };
    splashTread.start();



}

MainActivity.java

public class MainActivity extends AppCompatActivity implements DrawerFragment.DrawerFragmentListener{

//Views
public Toolbar mToolbar;
public View mHeaderView;
public TabLayout mSlidingTabLayout;
public SwipeableViewPager mViewPager;

private NavigationAdapter mAdapter;
private DrawerFragment drawerFragment;

private WebFragment CurrentAnimatingFragment = null;
private int CurrentAnimation = 0;

private static int NO = 0;
private static int HIDING = 1;
private static int SHOWING = 2;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    mToolbar = (Toolbar) findViewById(R.id.toolbar);
    mHeaderView = (View) findViewById(R.id.header_container);
    mSlidingTabLayout = (TabLayout) findViewById(R.id.tabs);
    mViewPager = (SwipeableViewPager) findViewById(R.id.pager);

    setSupportActionBar(mToolbar);

    mAdapter = new NavigationAdapter(getSupportFragmentManager(), this);

    final Intent intent = getIntent();
    final String action = intent.getAction();

    if (Intent.ACTION_VIEW.equals(action)) {
        String data = intent.getDataString();
        ((App) getApplication()).setPushUrl(data);
    }

    if (Config.HIDE_ACTIONBAR)
        getSupportActionBar().hide();

    if (getHideTabs())
        mSlidingTabLayout.setVisibility(View.GONE);

    RelativeLayout.LayoutParams lp = (RelativeLayout.LayoutParams) mViewPager.getLayoutParams();
    if ((Config.HIDE_ACTIONBAR && getHideTabs()) || ((Config.HIDE_ACTIONBAR || getHideTabs()) && getCollapsingActionBar())){
        lp.topMargin = 0;
    } else if ((Config.HIDE_ACTIONBAR || getHideTabs()) || (!Config.HIDE_ACTIONBAR && !getHideTabs() && getCollapsingActionBar())){
        lp.topMargin = getActionBarHeight(this);
    } else if (!Config.HIDE_ACTIONBAR && !getHideTabs()){
        lp.topMargin = getActionBarHeight(this) * 2;
    }

    if (Config.USE_DRAWER) {
        getSupportActionBar().setDisplayShowHomeEnabled(true);

        drawerFragment = (DrawerFragment)
                getSupportFragmentManager().findFragmentById(R.id.fragment_navigation_drawer);
        drawerFragment.setUp(R.id.fragment_navigation_drawer, (DrawerLayout) findViewById(R.id.drawer_layout), mToolbar);
        drawerFragment.setDrawerListener(this);
    } else {
        ((DrawerLayout) findViewById(R.id.drawer_layout)).setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED);
    }

    mViewPager.setLayoutParams(lp);

    mViewPager.setAdapter(mAdapter);
    mViewPager.setOffscreenPageLimit(mViewPager.getAdapter().getCount() - 1);

    mSlidingTabLayout.setSelectedTabIndicatorColor(getResources().getColor(R.color.accent));
    mSlidingTabLayout.setupWithViewPager(mViewPager);
    mSlidingTabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
        @Override
        public void onTabSelected(TabLayout.Tab tab) {
            if (getCollapsingActionBar()) {
                showToolbar(getFragment());
            }
            mViewPager.setCurrentItem(tab.getPosition());
        }

        @Override
        public void onTabUnselected(TabLayout.Tab tab) {

        }

        @Override
        public void onTabReselected(TabLayout.Tab tab) {

        }
    });

    for (int i = 0; i < mSlidingTabLayout.getTabCount(); i++) {
        if (Config.ICONS.length > i  && Config.ICONS[i] != 0) {
            mSlidingTabLayout.getTabAt(i).setIcon(Config.ICONS[i]);
        }
    }

    // admob
    if (!getResources().getString(R.string.ad_unit_id).equals("")) {
        // Look up the AdView as a resource and load a request.
        AdView adView = (AdView) findViewById(R.id.adView);
        AdRequest adRequest = new AdRequest.Builder().build();
        adView.loadAd(adRequest);
    } else {
        AdView adView = (AdView) findViewById(R.id.adView);
        adView.setVisibility(View.GONE);
    }

    // application rating
    AlertDialog.Builder builder = new AlertDialog.Builder(this)
            .setTitle(getString(R.string.rate_title))
            .setMessage(String.format(getString(R.string.rate_message), getString(R.string.app_name)))
            .setPositiveButton(getString(R.string.rate_yes), null)
            .setNegativeButton(getString(R.string.rate_never), null)
            .setNeutralButton(getString(R.string.rate_later), null);

    new AppRate(this)
            .setShowIfAppHasCrashed(false)
            .setMinDaysUntilPrompt(2)
            .setMinLaunchesUntilPrompt(2)
            .setCustomDialog(builder)
            .init();

    // showing the splash screen
    if (Config.SPLASH) {
        findViewById(R.id.imageLoading1).setVisibility(View.VISIBLE);
        //getFragment().browser.setVisibility(View.GONE);
    }
}

// using the back button of the device
@Override
public void onBackPressed() {
    View customView = null;
    WebChromeClient.CustomViewCallback customViewCallback = null;
    if (getFragment().chromeClient != null) {
        customView = getFragment().chromeClient.getCustomView();
        customViewCallback = getFragment().chromeClient.getCustomViewCallback();
    }

    if ((customView == null)
            && getFragment().browser.canGoBack()) {
        getFragment().browser.goBack();
    } else if (customView != null
            && customViewCallback != null) {
        customViewCallback.onCustomViewHidden();
    } else {
        super.onBackPressed();
    }
}

@Override
public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.menu, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    WebView browser = getFragment().browser;
    if (item.getItemId() == (R.id.next)) {
        browser.goForward();
        return true;
    } else if (item.getItemId() == R.id.previous) {
        browser.goBack();
        return true;
    } else if (item.getItemId() == R.id.share) {
        getFragment().shareURL();
        return true;
    } else if (item.getItemId() == R.id.about) {
        AboutDialog();
        return true;
    } else if (item.getItemId() == R.id.home) {
        browser.loadUrl(getFragment().mainUrl);
        return true;
    } else if (item.getItemId() == R.id.close) {
        finish();
        Toast.makeText(getApplicationContext(),
                getText(R.string.exit_message), Toast.LENGTH_SHORT).show();
        return true;
    }
    return super.onOptionsItemSelected(item);
}

@Override
protected void onPause() {

    super.onPause();
}

@Override
protected void onResume() {
    super.onResume();
}

// showing about dialog
private void AboutDialog() {
    // setting the dialogs text, and making the links clickable
    final TextView message = new TextView(this);
    // i.e.: R.string.dialog_message =>
    final SpannableString s = new SpannableString(
            this.getText(R.string.dialog_about));
    Linkify.addLinks(s, Linkify.WEB_URLS);
    message.setTextSize(15f);
    message.setPadding(20, 15, 15, 15);
    message.setText(Html.fromHtml(getString(R.string.dialog_about)));
    message.setMovementMethod(LinkMovementMethod.getInstance());

    // creating the actual dialog

    AlertDialog.Builder AlertDialog = new AlertDialog.Builder(this);
    AlertDialog.setTitle(Html.fromHtml(getString(R.string.about)))
            // .setTitle(R.string.about)
            .setCancelable(true)
                    // .setIcon(android.R.drawable.ic_dialog_info)
            .setPositiveButton("ok", null).setView(message).create().show();
}

public void setTitle(String title) {
    if (mAdapter != null &&
            mAdapter.getCount() == 1 &&
            !Config.USE_DRAWER &&
            !Config.STATIC_TOOLBAR_TITLE &&
            getSupportActionBar() != null)
        getSupportActionBar().setTitle(title);
}

public WebFragment getFragment(){
    return (WebFragment) mAdapter.getCurrentFragment();
}

public void hideSplash() {
    if (Config.SPLASH) {
        Handler mHandler = new Handler();
        mHandler.postDelayed(new Runnable() {
            public void run() {
                // hide splash image
                if (findViewById(R.id.imageLoading1).getVisibility() == View.VISIBLE) {
                    findViewById(R.id.imageLoading1).setVisibility(
                            View.GONE);
                    // show webview
                }
            }
            // set a delay before splashscreen is hidden
        }, Config.SPLASH_SCREEN_DELAY);
    }
}

public void hideToolbar() {
    if (CurrentAnimation != HIDING) {
        CurrentAnimation = HIDING;
        AnimatorSet animSetXY = new AnimatorSet();

        ObjectAnimator animY = ObjectAnimator.ofFloat(getFragment().rl, "y", 0);
        ObjectAnimator animY1 = ObjectAnimator.ofFloat(mHeaderView, "y", -getActionBarHeight(this));
        animSetXY.playTogether(animY, animY1);

        animSetXY.start();
        animSetXY.addListener(new Animator.AnimatorListener() {
            @Override
            public void onAnimationStart(Animator animation) {
            }

            @Override
            public void onAnimationEnd(Animator animation) {
                CurrentAnimation = NO;
            }

            @Override
            public void onAnimationCancel(Animator animation) {
            }

            @Override
            public void onAnimationRepeat(Animator animation) {
            }
        });

    }
}

public void showToolbar(WebFragment fragment) {
    if (CurrentAnimation != SHOWING || fragment != CurrentAnimatingFragment) {
        CurrentAnimation = SHOWING;
        CurrentAnimatingFragment = fragment;
        AnimatorSet animSetXY = new AnimatorSet();
        ObjectAnimator animY = ObjectAnimator.ofFloat(fragment.rl, "y", getActionBarHeight(this));
        ObjectAnimator animY1 = ObjectAnimator.ofFloat(mHeaderView, "y", 0);
        animSetXY.playTogether(animY, animY1);

        animSetXY.start();
        animSetXY.addListener(new Animator.AnimatorListener() {
            @Override
            public void onAnimationStart(Animator animation) {
            }

            @Override
            public void onAnimationEnd(Animator animation) {
                CurrentAnimation = NO;
                CurrentAnimatingFragment = null;
            }

            @Override
            public void onAnimationCancel(Animator animation) {
            }

            @Override
            public void onAnimationRepeat(Animator animation) {
            }
        });

    }
}

public static int getActionBarHeight(Context context) {
    TypedValue typedValue = new TypedValue();
    int[] textSizeAttr = new int[]{R.attr.actionBarSize};

    int indexOfAttrTextSize = 0;

    TypedArray a = context.obtainStyledAttributes(typedValue.data, textSizeAttr);
    int actionBarSize = a.getDimensionPixelSize(indexOfAttrTextSize, -1);
    a.recycle();

    return actionBarSize;
}

boolean getHideTabs(){
    if (mAdapter.getCount() == 1 || Config.USE_DRAWER){
        return true;
    } else {
        return Config.HIDE_TABS;
    }
}

public static boolean getCollapsingActionBar(){
    if (Config.COLLAPSING_ACTIONBAR && !Config.HIDE_ACTIONBAR){
        return true;
    } else {
        return false;
    }
}

@Override
public void onDrawerItemSelected(View view, int position) {
    getFragment().browser.loadUrl("about:blank");
    getFragment().setBaseUrl(Config.URLS[position]);
}

我对此问题的主要想法是SplashScreen.java在MainActivity.java之前启动;但是,我不确定如何解决这个问题。

0 个答案:

没有答案