登录

  • 登录
  • 忘记密码?点击找回

注册

  • 获取手机验证码 60
  • 注册

找回密码

  • 获取手机验证码60
  • 找回
毕业论文网 > 外文翻译 > 计算机类 > 物联网工程 > 正文

基于Android 的应用程序管理器的设计与实现外文翻译资料

 2022-11-27 02:11  

7.2 Web with a View

On your desktop computer, a web browser is a large, complicated, memory-gobbling program with all sorts of features like bookmarks, plug-ins, Flash animations, tabs, scroll bars, printing, and so forth. When I was working on the Eclipse project and someone suggested replacing some common text views with embedded web browsers, I thought they were crazy. Wouldnrsquo;t it make more sense, I argued, to simply enhance the text viewer to do italics or tables or whatever it was that was missing? It turns out they werenrsquo;t crazy because:

bull;A web browser can be (relatively) lean and mean if you strip out everything but the basic rendering engine.

bull; If you enhance a text view to add more and more things that a browser engine can do, you end up with either an overly complicated, bloated text viewer or an underpowered browser.

Android provides a wrapper around the WebKit browser engine called WebView that you can use to get the real power of a browser with as little as 1MB of overhead. Although 1MB is still significant on an embedded device, there are many cases where using a WebView is appropriate.

WebView works pretty much like any other Android view except that it has a few extra methods specific to the browser. Irsquo;m going to show you how it works by doing an embedded version of the previous example. This one will be called BrowserView instead of BrowserIntent, since it uses an embedded View instead of an Intent.

WebView has dozens of other methods you can use to control what is being displayed or get notifications on state changes.

You can find a complete list in the online documentation for WebView, but here are the methods you are most likely to need:

bull; addJavascriptInterface( ): Allows a Java object to be accessed from JavaScript (more on this one in the next section)

bull; createSnapshot( ): Creates a screenshot of the current page

bull; getSettings( ): Returns a WebSettings object used to control the settings

bull; loadData( ): Loads the given string data into the browser

bull; loadDataWithBaseURL( ): Loads the given data using a base URL

bull; loadUrl( ): Loads a web page from the given URL

bull; setDownloadListener( ): Registers callbacks for download events, such as when the user downloads a .zip or .apk file

bull; setWebChromeClient( ): Registers callbacks for events that need to be done outside the WebView rectangle, such as updating the title or progress bar or opening a JavaScript dialog box

bull; setWebViewClient( ): Lets the application set hooks in the browser to intercept events such as resource loads, key presses, and authorization requests

bull; stopLoading( ): Stops the current page from loading One of the most powerful things you can do with the WebView control is to talk back and forth between it and the Android application that contains it. Letrsquo;s take a closer look at this feature now.

Is Allowing JavaScript to Call Java Dangerous?

Whenever you allow a web page to access local resources or call functions outside the browser sandbox, you need to consider the security implications very carefully. For example, you wouldnrsquo;t want to create a method to allow JavaScript to read data from any arbitrary path name because that might expose some private data to a malicious site that knew about your method and your filenames. Here are a few things to keep in mind. First, donrsquo;t rely on security by obscurity. Enforce limits on the pages that can use your methods and on the things those methods can do. And remember the golden rule of security: donrsquo;t rule things out; rule them in. In other words, donrsquo;t try to check for all the bad things that someone can ask you to do (for example, invalid characters in a query). Yoursquo;re bound to miss something. Instead, disallow everything, and pass only the good things you know are safe.

7.3 From JavaScript to Java and Back

Your Android device can do a number of cool things such as store local data, draw graphics, play music, make calls, and determine its location. Wouldnrsquo;t it be nice if you could access that functionality from a web page? With an embedded WebView control, you can.

The key is the addJavascriptInterface( ) method in the WebView class. You can use it to extend the Document Object Model (DOM) inside the embedded browser and to define a new object that JavaScript code can access. When the JavaScript code invokes methods on that object, it will actually be invoking methods in your Android program. You can call JavaScript methods from your Android program too. All you have to do is call the loadUrl( ) method, passing it a URL of the form javascript:code-to-execute. Instead of going to a new page, the browser will execute the given JavaScript expression inside the current page. You can call a method, change JavaScript variables, modify the browser document—anything you need. To demonstrate calls between JavaScript in the WebView and Java inthe Android program, let us now build a program that is half HTML/ JavaScript and half Android (see Figure 7.4). The top part of the application window is a WebView control, and the bottom part is a TextView and Button from the Android user interface. When you click the buttons and links, it makes calls between the two environments.

To do that, we set a listener for button clicks using setOnClickListener( ). When the button is pressed, onClick( ) is called, which turns around and calls WebView.loadUrl( ), passing it a JavaScript expression to evaluate in the browser. The expression is a call to the callJS( ) function defined in index.html. Run the program now, and try it. When you click “Display JavaScript alert,” an Android message window will appear. When you click “Call Android from JavaScript,” the string “Hello from Browser” will be displayed in an Android text control. And finally, when you press the “Call JavaScript from Android” button, the string “Hello from Android” is s

剩余内容已隐藏,支付完成后下载完整资料


7.2带视图的网络

在你的电脑桌面,Web浏览器是一个大型的、复杂的、记忆的程序与各种功能如书签、插件、Flash动画、标签、滚动条、印刷、等等。当我在Eclipse项目上工作时,有人建议用嵌入式浏览器替换一些常见的文本视图,我认为他们很疯狂。岂不是更有意义,我认为,简单地提高文本查看器做斜体或表或不管它是什么,失踪了?原来他们不是疯了,因为:

bull;一个网页浏览器可以(相对)精益并且意味着你去掉一切除了基本渲染的引擎。

bull;如果你提高文本视图添加更多的东西,一个浏览器引擎能做什么,你最终要么是过于复杂,臃肿的文本查看器或是动力不足的浏览器。

安卓提供了包裹着WebKit浏览器的引擎,称为Web视图,可以让你的浏览器的真正力量和1MB的开销一样小。虽然1MB就是仍有显著的嵌入式设备上,但在许多情况下,使用WebView是合适的。

WebView非常像任何其他安卓视图除了它有几个额外的特定于浏览器的方法。我将向您展示如何工作,通过做一个嵌入式版本的前一个例子。这将是一个称为浏览器显示来代替内部浏览器,因为它使用一个嵌入式的观点而不是一个目的。

WebView有几十个其他方法可以用来控制所显示的状态变化通知。

你可以在WebView的在线文档中找到完整的列表,但这是你最有可能需要的方法:

bull;addJavascriptInterface():使一个java对象来访问JavaScript(这个在下一节更多提到)

bull;createsnapshot():创建一个当前页面截图

bull;getsettings():返回一个websettings对象用来控制设置

bull;loadDate():给定字符串数据到浏览器

bull;loaddatawithbaseurl():使用URL加载给定数据

bull;loadurl():从给定的URL的网页负载

bull;setdownloadlistener():下载注册回调事件,例如当用户下载.zip或.apk文件

bull;setwebchromeclient():注册回调需要做外WebView矩形的事件,如更新的标题或进度条或打开一个JavaScript对话框

bull;setwebviewclient():让应用程序设置钩子在浏览器拦截事件,如资源负载,按键,并授权请求

bull;stopLoading():停止当前页从你与WebView控制的最有力的东西加载是言来语去,包含它的Android应用程序之间。现在让我们仔细看看这个功能。

允许JavaScript调用java危险吗?

每当你允许一个网页访问本地资源或浏览器沙盒之外的调用函数时,你需要非常小心地考虑安全含义。例如,你不想创建一个方法允许JavaScript从任意路径名中读取数据,因为那可能会暴露一些隐私数据的恶意网站,知道你的方法,你的文件名。这里有几件事要牢记在心。首先,不要依赖安全。对可以使用你的方法的页面和那些方法所能做的事情施加限制。记住安全的金科玉律:不要把事情排除在外。换句话说,不要试图检查所有的坏东西,有人可以要求你做(例如,无效字符的查询)。你一定会错过什么。相反,禁止一切,并通过只有好的东西你知道是安全的。

7.3 从javascript到java然后回来

您的安卓设备可以做一些酷的东西,如存储本地数据,绘制图形,播放音乐,打电话,并确定其位置。如果你能从网页中访问该功能,那不是很好吗?您还可以嵌入WebView控件。

关键是addJavascriptInterface()在WebView类的方法。您可以使用它来扩展嵌入浏览器中的文档对象模型(DOM),并定义JavaScript代码可以访问的新对象。当JavaScript代码调用该对象的方法时,它实际上会在你的安卓程序中调用方法。你也可以从你的安卓程序中调用JavaScript方法。你所要做的就是定义loadurl()方法,它传递一个URL形式的JavaScript代码执行。浏览器不会执行新页面,而是在当前页内执行给定的JavaScript表达式。你可以调用一个方法,改变JavaScript变量,修改浏览器文档任何你需要的东西。展示了在安卓中 WebView和java程序之间调用的JavaScript,现在让我们建立一个程序,一半是HTML / JavaScript和半机器人(见图7.4)。应用程序窗口的顶部和底部的WebView控件,从Android用户界面的一个TextView和按钮。单击按钮和链接时,会在两个环境之间进行调用。

要做到这一点,我们设置一个监听器的按钮点击使用setonclicklistener()方法。当按钮被按下,onClick()被调用,它立刻调用WebView类。loadurl()方法,通过JavaScript在浏览器中进行表达。表达的是一种在index.html中定义的calljs() 函数。现在运行程序,并尝试运行它。当单击“显示JavaScript警报”时,会出现一个安卓消息窗口。当你点击“从JavaScript调用安卓,”字符串“Hello浏览器”将显示在Android文本控件。最后,当你按下“从Android调用JavaScript”按钮,字符串“你好从Android”被发送到浏览器,并插入在HTML中,它将显示在网页的结尾。有时您不需要显示网页,但您只需要访问某种Web服务或其他服务器端资源。在下一节,我会告诉你怎么做。

7.4 使用web服务

Android提供了一套完整的java标准网络API,如java.net.HttpUrlconnection包,你可以在程序中使用。棘手的部分是异步调用,以便您的程序的用户界面在任何时候都会相应。

考虑如果你只是在主(GUI)线程中阻塞网络调用会发生什么。直到该调用返回(甚至它可能永远不会返回),您的应用程序无法响应任何用户界面事件,如按键或按按钮。它会出现挂到用户。显然,这是你必须避免的。

java.util.concurrent包很适合这种工作。首先由Doug Lea作为一个独立的图书馆,然后并入java 5,这包比普通java线程类更支持并行编程。当你执行服务类管理一个或多个线程,所有你要做的就是提交任务(运行或调用的实例)的执行器来运行它们。返回一个未来类的实例,它是对一些未知的将来值的引用,该值将由你的任务返回(如果有的话)。可以限制创建的线程数,如果必要的话可以中断运行的任务。

为了说明这些概念,让我们创建一个有趣的小程序,称为谷歌翻译API5。你曾经嘲笑奇怪的翻译和外国语言,特别是计算机翻译。这个程序可以让用户在一个语言输入一个短语,问谷歌翻译的第二语言,然后问谷歌翻译的第一语言。理想情况下,刚开始你会得到相同的话,但这并不总是如此,你可以看到在图7.5,在下一页。

若要使用此程序,只需选择起始语言和目标语言,然后键入短语。当您键入时,程序将使用谷歌翻译Web服务将您的文本输入和输出目标语言。

要创建此应用程序,请使用这些参数从“Hello,Android”应用程序开始:

Project name: Translate

图7.5机器翻译仍然是一个正在进行中的工作。

Build Target: Android 1.5

Application name: Translate

Package name: org.example.translate

Create Activity: Translate

由于这个例子将访问Internet进行Web服务调用,我们需要告诉Android授予我们权限。lt;applicationgt; XML标签之前,AndroidManifest.xml加入这一行:

lt;uses-permission android:name='android.permission.INTERNET' /gt;

本例的布局是一个比平常更复杂,所以我们将使用表格布局视图。表格布局让你安排你的观点为行和列,注意横向和纵向来填满整个内容。它类似于在HTML中使用lt;tablegt;和lt;trgt;。

lt;?xml version='1.0' encoding='utf-8'?gt;

lt;ScrollView

xmlns:android=http://schemas.android.com/apk/res/android android:layout_width='fill_parent'

android:layout_height='fill_parent'gt;

lt;TableLayout

android:layout_width='fill_parent'

android:layout_height='fill_parent'

android:stretchColumns='1'

android:padding='10dip'gt;

lt;TableRowgt;

lt;TextView android:text='@string/from_text' /gt;

lt;Spinner android:id='@ id/from_language' /gt;

lt;/TableRowgt;

lt;EditText

android:id='@ id/original_text'

android:hint='@string/original_hint'

android:padding='10dip'

android:textSize='18sp' /gt;

lt;TableRowgt;

lt;TextView android:text='@string/to_text' /gt;

lt;Spinner android:id='@ id/to_language' /gt;

lt;/TableRowgt;

lt;TextView

android:id='@ id/translated_text'

android:padding='10dip'

android:textSize='18sp' /gt;

lt;TextView android:text='@string/back_text' /gt;

lt;TextView

android:id='@ id/retranslated_text'

android:padding='10dip'

android:textSize='18sp' /gt;

lt;/TableLayoutgt;

lt;/ScrollViewgt;

在这个例子中,我们有5行,每行包含一个或两列。值得注意的是,如果有一行只有一个观点,你不必使用TableRow包含它。另外,使用Android是不必要的:比如layout_width =和layout_height =,在一看就像你再用线性布局的地方。

spinner类是一个新类,我们以前没有见过。它类似于其他用户界面工具箱中的组合框。用户选择旋转器(例如,通过触摸它),并列出一个可能的值,为他们挑选。在这个例子中,我们将使用这个控件从语言列表中选择。

实际列表存储为一个Android资源在文件res/values/ arrays.xml下:

lt;?xml version='1.0' encoding='utf-8'?gt;

lt;resourcesgt;

lt;array name='languages'gt;

lt;itemgt;Bulgarian (bg)lt;/itemgt;

lt;itemgt;Chinese Simplified (zh-CN)lt;/itemgt;

lt;itemgt;Chinese Traditional (zh-TW)lt;/itemgt;

lt;itemgt;Catalan (ca)lt;/itemgt;

lt;itemgt;Croatian (hr)lt;/itemgt;

lt;itemgt;Czech (cs)lt;/itemgt;

lt;itemgt;Danish (da)lt;/itemgt;

lt;itemgt;Dutch (nl)lt;/itemgt;

lt;itemgt;English (en)lt;/itemgt;

lt;itemgt;Filipino (tl)lt;/itemgt;

lt;itemgt;Finnish (fi)lt;/itemgt;

lt;itemgt;French (fr)lt;/itemgt;

lt;itemgt;German (de)lt;/itemgt;

lt;itemgt;Greek (el)lt;/itemgt;

lt;itemgt;Indonesian (id)lt;/itemgt;

lt;

剩余内容已隐藏,支付完成后下载完整资料


资料编号:[26033],资料为PDF文档或Word文档,PDF文档可免费转换为Word

您需要先支付 30元 才能查看全部内容!立即支付

企业微信

Copyright © 2010-2022 毕业论文网 站点地图