这是我的闪屏 Activity 。
public class Splash extends Activity {
private static int SPLASH_TIME_OUT=10000;
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash);
Timer r=new Timer();
new Handler().postDelayed(r,SPLASH_TIME_OUT);
}
class Timer implements Runnable{
@Override
public void run() {
// TODO Auto-generated method stub
Intent i=new Intent(Splash.this,MainActivity.class);
startActivity(i);
finish();
}
}
}
这是我的 MainActivity。此 Activity 应在后台执行,而闪屏在前面。是否建议使用 AsyncTask。如何做到这一点?
如果不是 AsyncActivity,我可以使用什么?
public class MainActivity extends Activity {
private WebView webview;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
webview = (WebView)findViewById(R.id.output);
webview.getSettings().setJavaScriptEnabled(true);
webview.getSettings().setLoadsImagesAutomatically(true);
webview.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);
webview.getSettings().setAppCacheEnabled(false);
webview.setWebViewClient(new WebViewClient(){
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
if(url.startsWith("tel:")) {
Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse(url));
startActivity(intent);
return true;
}
return false;
}
}
);
webview.loadUrl("http://www.example.com");
}
//this is to go back to previous pages if exists
public boolean onKeyDown(int keyCode, KeyEvent event) {
if(event.getAction() == KeyEvent.ACTION_DOWN){
switch(keyCode)
{
case KeyEvent.KEYCODE_BACK:
if(webview.canGoBack()){
webview.goBack();
}else{
finish();
}
return true;
}
}
return super.onKeyDown(keyCode, event);
}
}
如果可以,我可以设置启动画面的时间,只要加载 url 不给时间就可以查看吗?
第二次尝试
public class MainActivity extends Activity {
WebView webview;
private boolean isSplashOn = true;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
webview.setBackgroundColor(0);
webview.setBackgroundResource(R.drawable.activity_splash);
webview = (WebView)findViewById(R.id.output);
webview.getSettings().setJavaScriptEnabled(true);
webview.getSettings().setLoadsImagesAutomatically(true);
webview.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);
webview.getSettings().setAppCacheEnabled(false);
webview.setWebViewClient(new WebViewClient(){
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
if(url.startsWith("tel:")) {
Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse(url));
startActivity(intent);
return true;
}
return false;
}
public void onPageFinished(WebView view, String url) {
if(isSplashOn) {
webview.setBackgroundDrawable(null);
webview.setBackgroundColor(0);
isSplashOn = false;
}
super.onPageFinished(view, url);
}
}
);
webview.loadUrl("http://www.example.com");
}
public boolean onKeyDown(int keyCode, KeyEvent event) {
if(event.getAction() == KeyEvent.ACTION_DOWN){
switch(keyCode)
{
case KeyEvent.KEYCODE_BACK:
if(webview.canGoBack()){
webview.goBack();
}else{
finish();
}
return true;
}
}
return super.onKeyDown(keyCode, event);
}
}
伙计们。问题还是没有解决。需要一些帮助
最佳答案
您可以使用简单的技巧 - 我一直在展示一个加载微调器,直到页面在后台加载。 因此我使用了两个 WebView 。 在您的情况下,您可以使用 ImageView 或任何其他 View 。
XML 文件会像这样 -
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="${relativePackage}.${activityClass}" >
//Actual webview
<WebView
android:id="@+id/actual_webView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
//Any view you want to show while webpage is loading
<WebView
android:id="@+id/spinner_webView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true" />
...
</RelativeLayout>
现在,在您的 JAVA 文件中-
//initially, make actual_webview invisible
actual_webview.setVisibility(View.INVISIBLE);
还有,在
@Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
// make spinner_webview invisible
spinner_webview.setVisibility(View.INVISIBLE);
// make actual_webview visible
actual_webview.setVisibility(View.VISIBLE);
}
此外,
shouldOverrideUrlLoading(...)
onPageStarted(...)
onPageFinished(...)
在所有这些方法中,您都可以获取 URL 并可以检查正在加载或完成加载的 URL。 您可以根据 URL 决定是否显示/隐藏启动画面。
关于java - 显示启动画面时如何在后台加载 webview 中的 url?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32829635/