Apache Cordova App Reloader

Apache Cordova 

简介

cordova.apache.org

Cordova项目将mobile app变成了一件很web的事情。使用js,html来生成native的mobile app.

其核心是提供了一个带有plugin的“浏览器”。plugin处理了native的api,比如获取device信息,照相等。

cordova源码(Android): https://github.com/apache/cordova-android

装载

cordova android 中,使用一个加强的Android WebView来封装应用。

核心类:

https://github.com/apache/cordova-android/blob/master/framework/src/org/apache/cordova/CordovaActivity.java

https://github.com/apache/cordova-android/blob/master/framework/src/org/apache/cordova/CordovaWebView.java


核心方法:

loadUrl - 用户可以指定一个位置,当这个网络里面的js调用了cordova.js 提供的API时,在容器里,它就能成功调用。


日前,我写了一个方法,可以在android上,长按 back键,弹出对话框,选择load另外一个网址。



package com.foo;

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.KeyEvent;
import android.widget.Toast;

import org.apache.cordova.*;
import java.util.Timer;
import java.util.TimerTask;
import android.view.MotionEvent;
import android.app.AlertDialog;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.content.Context;
import android.content.DialogInterface;

/**
 * when press screen for a long time, give a choice to exit.
 * 
 */
public class ReloadAppOptionWebView extends CordovaWebView {

	private boolean isBackLongPress = false;
	private final int TOAST_MSG_DURATION = 3000;
	private CordovaInterface xCordova;
    private static Context context;
    private static String currentAppUrl;

	public ReloadAppOptionWebView(Context context) {
		super(context);
        this.context=context;
	}

	public ReloadAppOptionWebView(Context context, AttributeSet attrs) {
		super(context, attrs);
        this.context=context;
	}

	public ReloadAppOptionWebView(Context context, AttributeSet attrs,
			int defStyle) {
		super(context, attrs, defStyle);
        this.context=context;
	}

	public ReloadAppOptionWebView(Context context, AttributeSet attrs,
			int defStyle, boolean privateBrowsing) {
		super(context, attrs, defStyle, privateBrowsing);
        this.context=context;
	}
	/**
	 * store the current app url, use it when reloading this app afterward 
	 */
    @Override
    public void loadUrl(String url){
        super.loadUrl(url);
        this.currentAppUrl=url;
    }

	@Override
	public boolean onKeyLongPress(int keyCode, KeyEvent event) {
		if ((keyCode == KeyEvent.KEYCODE_BACK)) {
			if (event.getRepeatCount() == 1) {
				toastMessage("Press again to load the original app.",
						TOAST_MSG_DURATION);
				shiftBackLongPressFlagWithTimer(TOAST_MSG_DURATION);
			}
			return true;
		}
		return super.onKeyLongPress(keyCode, event);
	}

	/**
	 * Detect actions of BACK key reload the app if clicks of BACK happens
	 * during Reload App Window
	 */
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if ((keyCode == KeyEvent.KEYCODE_BACK) && (isBackLongPress)) {
			if (event.getRepeatCount() == 0) {
				// user click back again during Reload App Window
				// super.loadUrl(Config.getStartUrl());
                popUpChoices(context);
			}
			return true;
		} else if (keyCode == KeyEvent.KEYCODE_BACK) {
			event.startTracking();
			return true;
		}
		return super.onKeyDown(keyCode, event);
	}

	/**
	 * During Reload App Window, all keyup actions are blocked.
	 */
	@Override
	public boolean onKeyUp(int keyCode, KeyEvent event) {
		if (isBackLongPress) {
			return true;
		}
		return super.onKeyUp(keyCode, event);
	}

	public void toastMessage(String msg, int duration) {
		Toast.makeText(this.getContext(), msg, duration).show();
	}

	/**
	 * Reload App Window
	 */
	private void shiftBackLongPressFlagWithTimer(int sleepTime) {
		Timer tExit = null;
		if (isBackLongPress == false) {
			// open Reload Option Window
			isBackLongPress = true;
			tExit = new Timer();
			tExit.schedule(new TimerTask() {
				@Override
				public void run() {
					// close Reload Option Window since no BACK Key event
					// happens.
					isBackLongPress = false;
				}
			}, sleepTime);
		}
	}
    // popup alert 
    private void popUpChoices(Context context){
        AlertDialog.Builder alert = new AlertDialog.Builder(context); 
        // more icon http://androiddrawableexplorer.appspot.com/
        alert.setIcon(android.R.drawable.ic_menu_directions);
        alert.setTitle("TestApp");

        WebView wv = new WebView(context);
        //TODO add app options directly
        wv.loadUrl("file:///android_asset/www/mini_applist.html");
        wv.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                view.loadUrl(url);
                return true;
            }
        });

        alert.setView(wv);
        alert.setPositiveButton("Reload", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int id) {
                dialog.dismiss();
                loadUrl(currentAppUrl);
            }
        });
        alert.setNeutralButton("Home", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int id) {
                dialog.dismiss();
                loadUrl(Config.getStartUrl());
            }
        });
        alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int id) {
                dialog.dismiss();
            }
        });
        alert.show();
    }
}



发布了386 篇原创文章 · 获赞 19 · 访问量 82万+

猜你喜欢

转载自blog.csdn.net/watson243671/article/details/22920113