Apache Cordova App Reloader
2014-04-07 09:58
246 查看
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();
}
}
简介
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();
}
}
相关文章推荐
- macos+apache+php+phpmyadmin 的整合过程梳理
- Apache Hadoop 2.x.x Installation
- 第一篇:编译安装apache
- Linux下设置apache开机启动
- 安装xampp后apache不能启动解决方法
- Apache Portable Runtime which allows optimal performance in production environments was not found
- apache安装与配置重定向
- CDH 4.6 Apache Hadoop的下一代mapreduce,yarn
- ubuntu12.04下apt-get install安装Apache+PHP+Mysql
- Ubuntu下安装apache2+PHP+mysql+phhmyadmin成功笔记
- 服务器性能指标:Apache压力测试工具ab
- centos6.4编译apache时的错误
- apache的“按周配置访问日志轮询”,需要安装cronolg软件
- Apache:the requested operation has failed!
- Apache虚拟主机(基于IP、基于端口、基于域名)
- Linux下Apache虚拟主机目录权限问题
- Cacti监控apache的方法
- Apache软件基金会相关项目
- SASYUN排错笔记-apache篇
- CentOS 6.4中yum安装配置LAMP服务器(Apache+MySQL+PHP5)