2013-04-30 30 views
0

注意:在Worklight 6.2中,控制本地/網絡關係要容易得多,所以現在這個問題已經過時。Worklight Android Splash

工作燈工作室5.0.6,Android模擬器4.0.2

關於Android的閃屏問題。下面的代碼是從this question的答案得出的。它似乎工作得很好。我首先考慮的是硬編碼的延遲

super.loadUrl(getWebMainFilePath(), 10000); 

如果省略10000(10秒)延遲那麼我們看到本機啓動畫面和第一網頁之間的空白屏幕。我的猜測是,根據設備的CPU速度,10秒鐘可以向下調整,或者在慢速設備上可能需要增加。所以我想知道是否有不同的回調,我們可以使用,而不是取決於硬編碼的10秒。

其次,假設我想要少一些靜態初始屏幕,在調用super.loadUr()之前添加代碼是否足以按照this article添加代碼?我對使用JavaScript調用本地頁面的材料很熟悉,但在這裏我們將從本地頁面開始,並且希望將控制權交給JavaScript世界,而JavaScript世界以前沒有初始化過。

import android.os.Bundle; 
import com.worklight.androidgap.WLDroidGap; 

public class App02 extends WLDroidGap { 

    @Override 
    public void onCreate(Bundle savedInstanceState){ 

      super.onCreate(savedInstanceState); 
      super.setIntegerProperty("splashscreen", R.drawable.splash); 
        // active code here?? 
      super.bindBrowser(appView); 
    } 

    /** 
    * onWLInitCompleted is called when the Worklight runtime framework initialization is complete 
    */ 
    @Override 
    public void onWLInitCompleted(Bundle savedInstanceState){ 
     super.loadUrl(getWebMainFilePath(), 10000); 
    } 
} 
+1

說實話,我認爲在當前Worklight版本的Android環境下(如果您確實管理 - 榮譽,共享!),在Worklight應用程序中處理啓動畫面沒有任何好方法。這是應該/將在未來版本中更好地處理的東西... – 2013-05-05 19:10:59

+0

謝謝Idan,很高興知道我不會錯過明顯的。 – djna 2013-05-05 21:21:14

回答

4

你可以這樣做。

1)。將此代碼添加到您的Android主類java類中:

/* 
* Shows the splash screen over the full Activity 
*/ 
protected void showSplashScreen() { 
    mThisapp.runOnUiThread(new Runnable() { 
     public void run() { 
      // Get reference to display 
      final Display display = getWindowManager().getDefaultDisplay(); 

      // Get current orientation 
      final int rotation = display.getRotation(); 
      final String orientation; 
      switch (rotation) { 
      default: 
      case Surface.ROTATION_0: 
       orientation = "Portrait"; 
       break; 
      case Surface.ROTATION_90: 
       orientation = "Landscape"; 
       break; 
      case Surface.ROTATION_180: 
       orientation = "Reverse Portrait"; 
       break; 
      case Surface.ROTATION_270: 
       orientation = "Reverse Landscape"; 
       break; 
      } 
      Log.i(TAG, "Orientation :: " + orientation); 

      // Create the layout for the dialog 
      final LinearLayout root = new LinearLayout(mThisapp.getActivity()); 
      // This method was deprecated in API level 13: display.getHeight and display.getWidth 
      // Gets the size of the display, in pixels 
      final Point outSize = new Point(); 
      display.getSize(outSize); 
      root.setMinimumHeight(outSize.y); 
      root.setMinimumWidth(outSize.x); 
      root.setOrientation(LinearLayout.VERTICAL); 
      root.setBackgroundColor(mThisapp.getIntegerProperty("backgroundColor", 
        Color.WHITE)); 
      root.setLayoutParams(new LinearLayout.LayoutParams(
        ViewGroup.LayoutParams.MATCH_PARENT, 
        ViewGroup.LayoutParams.MATCH_PARENT, 0.0F)); 
      //root.setBackgroundResource(mThisapp.splashscreen); 

      // ImageView Setup 
      final ImageView imageView = new ImageView(mBaseContext); 
      // Setting image resource 
      imageView.setImageResource(mThisapp.splashscreen); 
      // Setting image position 
      imageView.setLayoutParams(new LayoutParams(
        LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT)); 
      root.addView(imageView); 

      // Create and show the dialog 
      splashDialog = new Dialog(mThisapp, 
        android.R.style.Theme_Translucent_NoTitleBar); 
      // Check to see if the splash screen should be full screen 
      if ((getWindow().getAttributes().flags & WindowManager.LayoutParams.FLAG_FULLSCREEN) == WindowManager.LayoutParams.FLAG_FULLSCREEN) { 
       splashDialog.getWindow().setFlags(
         WindowManager.LayoutParams.FLAG_FULLSCREEN, 
         WindowManager.LayoutParams.FLAG_FULLSCREEN); 
      } 
      splashDialog.setContentView(root); 
      splashDialog.setCancelable(false); 
      splashDialog.show(); 
     } 
    }); 
} 

2)。在公共無效的onCreate(最終捆綁savedInstanceState),增加線路:

super.onCreate(savedInstanceState); 
    mThisapp = this; 
    mBaseContext = getBaseContext(); 
    // Add splash screen 
    super.setIntegerProperty("splashscreen", R.drawable.splash); 
... 
    // Show the splash dialog 
    this.splashscreen = this.getIntegerProperty("splashscreen", 0); 
    showSplashScreen(); 
    // End of apache/cordova-android/DroidGap 
... 
} 

是的,你必須有在res文件夾啓動圖像(以.png)。 3)。修改的onMessage

@Override 
/** 
* Called when a message is sent to plugin. 
* 
* @param id   The message id 
* @param data   The message data 
* @return    Object or null 
*/ 
public Object onMessage(final String id, final Object data) { 
    Log.d(TAG, "onMessage(" + id + "," + data + ")"); 
    if ("splashscreen".equals(id)) { 
     if (data != null && "hide".equals(data.toString())) { 
      if (mThisapp.appView.getVisibility() != View.VISIBLE) { 
       mThisapp.runOnUiThread(new Runnable() { 
        public void run() { 
         final Animation animationIn = AnimationUtils 
           .loadAnimation(mBaseContext, R.anim.zoom_in);       animationIn.setAnimationListener(new Animation.AnimationListener() { 
          @Override 
          public void onAnimationEnd(
            final Animation animation) { 
          } 

          @Override 
          public void onAnimationRepeat(
            final Animation animation) { 
          } 

          @Override 
          public void onAnimationStart(
            final Animation animation) { 
           if (splashDialog != null) 
            mThisapp.removeSplashScreen(); 
          } 
         }); 
         mThisapp.appView.setVisibility(View.VISIBLE); 
         mThisapp.appView.requestFocus(); 
         mThisapp.appView.startAnimation(animationIn); 
        } 
       }); 
      } else { 
       this.removeSplashScreen(); 
      } 
     } 
     return null; 
    } else if ("spinner".equals(id)) { 
     if (data != null && "stop".equals(data.toString())) { 
      this.spinnerStop(); 
      return null; 
     } 
    } 
    return super.onMessage(id, data); 
} 

4)。驗證config.xnl是否有此行

< plugin name="SplashScreen" value="org.apache.cordova.SplashScreen" /> 

5)。在您的工作燈初始化完成塊,添加此類似:

if (navigator && navigator.splashscreen) 
      navigator.splashscreen.hide(); 

然後,你必須與後端加載一個完整的啓動畫面。準備好後,顯示第一個屏幕。