0

注意:在 Worklight 6.2 中,控制本机/Web 关系要容易得多,所以这个问题现在已经过时了。

Worklight Studio 5.0.6、Android 模拟器 4.0.2

关于 Android 中启动画面的问题。下面的代码来自 这个问题的答案它似乎工作得很好。我首先关心的是硬编码延迟

 super.loadUrl(getWebMainFilePath(), 10000);

如果省略 10000(10 秒)延迟,那么我们会在本机启动屏幕和第一个网页之间看到一个空白屏幕。我的猜测是,根据设备的 CPU 速度,可以向下调整 10 秒,或者在慢速设备上可能需要增加。所以我想知道我们是否可以使用不同的回调,而不是依赖硬编码的 10 秒。

其次,假设我想要一些不那么静态的初始屏幕,在调用 super.loadUr() 之前按照本文添加代码是否足够?我熟悉有关从 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);
    }
}
4

1 回答 1

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)。在 public void onCreate(final Bundle 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)。在您的 Worklight 初始化完成块中,添加如下内容:

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

然后你有一个带有后端加载的完整初始屏幕。准备好后,显示第一个屏幕。

于 2013-06-10T15:54:55.370 回答