2013-06-25 81 views
1

我想加載我的webview,而啓動畫面處於活動狀態? 這比直接問題更直觀,但是當WebView加載到我的應用程序中時,屏幕在空白區域顯示2-4秒,直到內容完全加載。時間取決於正在加載的內容的大小。 有人能幫助我嗎?啓動畫面處於活動狀態時加載webview?

這是MainActivity

public class MainActivity extends Activity 

{ 

    private static final String MIME_TYPE_EMAIL = null; 
    final Activity activity = this; 

    @SuppressWarnings("deprecation") 
    @Override 
    public void onCreate(Bundle savedInstanceState) 
    { 
     super.onCreate(savedInstanceState); 

     this.getWindow().requestFeature(Window.FEATURE_PROGRESS); 
     setContentView(R.layout.activity_main); 
     webView = (WebView) findViewById(R.id.webview); 
     webView.getSettings().setJavaScriptEnabled(true); 
     webView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true); 
     webView.getSettings().setPluginsEnabled(true); 
     webView.setWebChromeClient(new WebChromeClient()); 
     String scandinavianCharacters = null; 
     webView.loadDataWithBaseURL(null, scandinavianCharacters, "text/html", "UTF-8", null); 
     webView.getSettings().setUserAgentString("Mozilla/5.0 (Windows; U; Windows NT 6.1; en-GB; rv:1.9.1.2) Gecko/20090729 Firefox/3.5.2 (.NET CLR 3.5.30729)"); 
     webView.setWebChromeClient(new WebChromeClient() { 


      public void onProgressChanged(WebView view, int progress) 
      { 
       activity.setTitle("Učitavanje..."); 
       activity.setProgress(progress * 100); 

       if(progress == 100) 
        activity.setTitle(R.string.app_name); 
      } 
     }); 

     webView.setWebViewClient(new WebViewClient() { 
      @Override 
      public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) 
      { 
       try { 
        webView.stopLoading(); 
       } catch (Exception e) { 
       } 
       try { 
        webView.clearView(); 
       } catch (Exception e) { 
       } 
       if (webView.canGoBack()) { 
        webView.goBack(); 
       } 
       webView.loadUrl("file:///android_asset/greska/greska.html"); 
       super.onReceivedError(webView, errorCode, description, failingUrl); 
      } 

      @Override 
      public boolean shouldOverrideUrlLoading(WebView view, String url) 
      { 
       view.loadUrl(url); 
       return true; 
      } 
     }); 

     webView.loadUrl("http://mysite.com"); 

    } 
    public boolean shouldOverrideUrlLoading(WebView webview, String url) 
    { 
     webview.loadUrl(url); 
     return true; 
    } 


    WebView webView; 
    @Override 
    public void onBackPressed(){ 

     if (webView.isFocused() && webView.canGoBack()) { 
       webView.goBack();  
     } 
     else { 
       super.onBackPressed(); 
     } 
    } 
    public boolean shouldOverrideUrlLoading(webView view, String url) { 
     if(url.startsWith("mailto:")){ 
      MailTo mt = MailTo.parse(url); 
      Intent i = IntentSupport.newEmailIntent(MainActivity.this, mt.getTo(), mt.getSubject(), mt.getBody(), mt.getCc()); 
      startActivity(i); 
      view.reload(); 
      return true; 
     } 

      else{ 
       view.loadUrl(url); 
      } 
      return true; 
     } 
    ; 


public static Intent newEmailIntent(Context context, String address, String subject, String body, String cc) { 
    Intent intent = new Intent(Intent.ACTION_SEND); 
    intent.putExtra(Intent.EXTRA_EMAIL, new String[] { address }); 
    intent.putExtra(Intent.EXTRA_TEXT, body); 
    intent.putExtra(Intent.EXTRA_SUBJECT, subject); 
    intent.putExtra(Intent.EXTRA_CC, cc); 
    intent.setType(MIME_TYPE_EMAIL); 
    return intent; 
} 
    } 

這是濺活動

public class SplashActivity extends Activity { 

    private static String TAG = SplashActivity.class.getName(); 
    private static long SLEEP_TIME = 8; // 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(); 
     } 
    } 
} 

清單文件

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

    <uses-sdk 
     android:minSdkVersion="8" 
     android:targetSdkVersion="17" /> 

    <uses-permission android:name="android.permission.INTERNET" /> 

    <application 
     android:allowBackup="true" 
     android:hardwareAccelerated="true" 
     android:icon="@drawable/ic_launcher" 
     android:label="@string/app_name" 
     android:theme="@android:style/Theme.NoTitleBar" > 
     <activity 
      android:name="com.emirnp.next.press.SplashActivity"> 
     <intent-filter> 
      <action android:name="android.intent.action.MAIN" /> 
      <category android:name="android.intent.category.LAUNCHER" /> 
       </intent-filter> 
       </activity> 
     <activity 

      android:name="com.emirnp.next.press.MainActivity" 
      android:configChanges="keyboardHidden|orientation|screenSize" 
      android:screenOrientation="portrait" 
      android:hardwareAccelerated="true"> 
      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 
</intent-filter> 


     </activity> 

    </application> 

</manifest>  
+0

這是個好問題。 –

回答

0

我不太明白是什麼做你的閃屏。當我想在打開緩慢頁面時顯示加載屏幕時,我使用以下代碼:

ProgressDialog browse_progressDialog; 
... 

//before load 
browse_progressDialog = new ProgressDialog(YourClass.this); 
browse_progressDialog.setMessage(getString(R.string.browse_progressbar_working)); 
browse_progressDialog.setIndeterminate(true); 
browse_progressDialog.setCancelable(false); 
browse_progressDialog.show(); 

//on load complete 
browse_progressDialog.dismiss(); 
相關問題