gpt4 book ai didi

android - 加载android应用程序时如何删除白屏?

转载 作者:塔克拉玛干 更新时间:2023-11-01 21:45:14 25 4
gpt4 key购买 nike

我是安卓新手。我有一个加载 URL 的 Web View 。问题是,在我打开应用程序后,在加载 Web View 的 URL 后出现 2-3 秒的白屏。

我认为这是应用程序启动的时间。我怎样才能去除白屏并显示我当时的标志?我听说过闪屏,但 Logo 出现 1 秒,然后白屏再出现 2-3 秒,最后加载 Web View 。

我做错了什么?在网页 View 加载时,是启动画面还是其他方式来显示 Logo ?

package com.exampe.dating;

import android.os.Bundle;
import android.app.Activity;
import android.app.ProgressDialog;
import android.view.KeyEvent;
import android.view.Menu;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

public class MainActivity extends Activity {

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

WebView mywebview = (WebView) findViewById(R.id.webview);
mywebview.loadUrl("http://www.example.com/mobile/index.php");
WebSettings webSettings = mywebview.getSettings();
webSettings.setJavaScriptEnabled(true);
mywebview.setWebViewClient(new WebViewClient());
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
WebView mywebview = (WebView) findViewById(R.id.webview);
if ((keyCode == KeyEvent.KEYCODE_BACK) && mywebview.canGoBack()) {
mywebview.goBack();
return true;
}
return super.onKeyDown(keyCode, event);
}
}

splashActivity.java

package com.exampe.dating;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Window;
import android.view.WindowManager;

public class SplashActivity extends Activity {

private static String TAG = SplashActivity.class.getName();
private static long SLEEP_TIME = 10; // Sleep for some time

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

this.requestWindowFeature(Window.FEATURE_NO_TITLE); // Removes title bar
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); // Removes notification bar

setContentView(R.layout.splash);

// Start timer and launch main activity
IntentLauncher launcher = new IntentLauncher();
launcher.start();
}

private class IntentLauncher extends Thread {
@Override
/**
* Sleep for some time and than start new activity.
*/
public void run() {
try {
// Sleeping
Thread.sleep(SLEEP_TIME*1000);
} catch (Exception e) {
Log.e(TAG, e.getMessage());
}

// Start main activity
Intent intent = new Intent(SplashActivity.this, MainActivity.class);
SplashActivity.this.startActivity(intent);
SplashActivity.this.finish();
}
}
}

安卓 list

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.exampe.dating"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="8" />
<uses-permission android:name="android.permission.INTERNET" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".SplashActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>

<activity
android:name="com.exampe.dating.MainActivity"
android:label="@string/app_name"
android:theme="@android:style/Theme.NoTitleBar.Fullscreen" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />

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


</application>

</manifest>

最佳答案

像这样在您的 list 中声明两个启动 Activity 非常奇怪。您应该消除 SplashActivity 的 Intent 过滤器,并从 MainActivityonCreate 中启动 SplashActivity:

编辑 更新以用等待代替 sleep 。当页面加载或发生超时时,启动画面将消失,以先到者为准。

public class MainActivity extends Activity {
public static Object SPLASH_LOCK = new Object();

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


WebView mywebview = (WebView) findViewById(R.id.webview);
mywebview.loadUrl("http://www.example.com/mobile/index.php");
WebSettings webSettings = mywebview.getSettings();
webSettings.setJavaScriptEnabled(true);
mywebview.setWebViewClient(new WebViewClient());

startActivity(new Intent(this, SplashActivity.class));
}
. . .
}

// in your WebViewClient:
public void onPageFinished (WebView view, String url) {
synchronized (SPLASH_LOCK) {
SPLASH_LOCK.notifyAll();
}
}

然后在 SplashActivity 中,等待您想要的延迟,然后调用 finish()。您不需要再次启动 MainActivity,因为它位于 SplashActivity 后面,并且会在 SplashActivity 完成时出现。

public class SplashActivity extends Activity {

private static String TAG = SplashActivity.class.getName();
private static long MAX_SPLASH_TIME = 10000;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

. . .

setContentView(R.layout.splash);

new Thread() {
@Override
public void run() {
synchronized (MainActivity.SPLASH_LOCK) {
// wait for notify or time-out
try { MainActivity.SPLASH_LOCK.wait(MAX_SPLASH_TIME); }
catch (InterruptedException ignored) {}
}
finish();
}
}.start();
}
. . .
}

请注意,您不需要 IntentLauncher 类。

附言你没有问过这个,但如果你将它添加到 list 中的 SplashActivity 标签中:

android:theme="@android:style/Theme.NoTitleBar.Fullscreen"

那么您就不需要调用 requestWindowFeature 或在 onCreate 中设置窗口标志。

关于android - 加载android应用程序时如何删除白屏?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14568610/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com