phonegap底层原理学习和研究(三)

              针对Phonegap开发中常用的DroidGap类继承自PhonegapActivity,PhonegapActivity继承自Activity。源代码如下:

/*
 * PhoneGap is available under *either* the terms of the modified BSD license *or* the
 * MIT License (2008). See http://opensource.org/licenses/alphabetical for full text.
 * 
 * Copyright (c) 2005-2010, Nitobi Software Inc.
 * Copyright (c) 2010, IBM Corporation
 */
package com.phonegap.api;

import android.app.Activity;
import android.content.Intent;

/**
 * The Phonegap activity abstract class that is extended by DroidGap.
 * It is used to isolate plugin development, and remove dependency on entire Phonegap library.
 */
public abstract class PhonegapActivity extends Activity {

    /**
     * Add a class that implements a service.
     * 
     * @param serviceType
     * @param className
     */
    abstract public void addService(String serviceType, String className);
    
    /**
     * Send JavaScript statement back to JavaScript.
     * 
     * @param message
     */
    abstract public void sendJavascript(String statement);

    /**
     * Launch an activity for which you would like a result when it finished. When this activity exits, 
     * your onActivityResult() method will be called.
     *  
     * @param command			The command object
     * @param intent			The intent to start
     * @param requestCode		The request code that is passed to callback to identify the activity
     */
    abstract public void startActivityForResult(IPlugin command, Intent intent, int requestCode);

    /**
     * Set the plugin to be called when a sub-activity exits.
     * 
     * @param plugin			The plugin on which onActivityResult is to be called
     */
    abstract public void setActivityResultCallback(IPlugin plugin);

    /**
     * Load the specified URL in the PhoneGap webview.
     * 
     * @param url				The URL to load.
     */
    abstract public void loadUrl(String url);
}

在使用这个类启动Webapp移动项目时候可以配置许多东西,如下:

This class is the main Android activity that represents the PhoneGap
application.  It should be extended by the user to load the specific
html file that contains the application.

As an example:

    package com.phonegap.examples;
    import android.app.Activity;
    import android.os.Bundle;
    import com.phonegap.*;
    
    public class Examples extends DroidGap {
      @Override
      public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
                 
        // Set properties for activity
        super.setStringProperty("loadingDialog", "Title,Message"); // show loading dialog
        super.setStringProperty("errorUrl", "file:///android_asset/www/error.html"); // if error loading file in super.loadUrl().

        // Initialize activity
        super.init();
        
        // Clear cache if you want
        super.appView.clearCache(true);
        
        // Load your application
        super.setIntegerProperty("splashscreen", R.drawable.splash); // load splash.jpg image from the resource drawable directory
        super.loadUrl("file:///android_asset/www/index.html", 3000); // show splash screen 3 sec before loading app
      }
    }

DroidGap中可以配置的属性如下:

Properties: The application can be configured using the following properties:

//加载时候加载对话框的信息

//Displayanativeloadingdialogwhenloadingapp.Formatforvalue="Title,Message".

//(String-default=null)

super.setStringProperty("loadingDialog","Wait,LoadingDemo...");

//加载对话对话框

//Displayanativeloadingdialogwhenloadingsub-pages.Formatforvalue="Title,Message".

//(String-default=null)

super.setStringProperty("loadingPageDialog","Loadingpage...");

//Causealllinksonwebpagetobeloadedintoexistingwebview,

//insteadofbeingloadedintonewbrowser.(Boolean-default=false)

super.setBooleanProperty("loadInWebView",true);

//加载相关的动画信息

//Loadasplashscreenimagefromtheresourcedrawabledirectory.

//(Integer-default=0)

super.setIntegerProperty("splashscreen",R.drawable.splash);

//设置默认的背景色

//Setthebackgroundcolor.

//(Integer-default=0orBLACK)

super.setIntegerProperty("backgroundColor",Color.WHITE);

//设置超时时间

//Timeinmsectowaitbeforetriggeringatimeouterrorwhenloading

//withsuper.loadUrl().(Integer-default=20000)

super.setIntegerProperty("loadUrlTimeoutValue",60000);

//设置请求错误时候的提示页面

//URLtoloadifthere'sanerrorloadingspecifiedURLwithloadUrl().

//ShouldbealocalURLstartingwithfile://.(String-default=null)

super.setStringProperty("errorUrl","file:///android_asset/www/error.html");

//是否在后台运行的功能

//Enableapptokeeprunninginbackground.(Boolean-default=true)

super.setBooleanProperty("keepRunning",false);

Phonegap.xml的配置如下:

PhoneGapusesaconfigurationfileatres/xml/phonegap.xmltospecifythefollowingsettings.

//允许phonegap访问的路径和域

ApprovedlistofURLsthatcanbeloadedintoDroidGap

         <access origin="http://server regexp" subdomains="true" />

   //日志的模式

Loglevel:ERROR,WARN,INFO,DEBUG,VERBOSE(default=ERROR)

<loglevel="DEBUG"/>

Phonegapplugins:

PhoneGapusesafileatres/xml/plugins.xmltolistallpluginsthatareinstalled.

Beforeusinganewplugin,anewelementmustbeaddedtothefile.

nameattributeistheservicenamepassedtoPhoneGap.exec()inJavaScript

valueattributeistheJavaclassnametocall.

<plugins>

<pluginname="App"value="com.phonegap.App"/>

...

</plugins>

相关推荐