万恶的破地,将近一个月断网...补笔记ing
-------------------第一章----------------------
The following desktop functionalities arenot supportedon Android:
ActionScript 2, ContextMenu, DatagramSocket, Dockicon, EncryptedLocalStore, HTMLoader, LocalConnection, NativeMenu, NativeWindow, PrintJob, SecureSocket, ServerSocket, StorageVolumeInfo, SystemTrayIcon, Updater, XMLSignatureValidator
-------------------第5章----------------------
关于屏幕朝向
The default orientation is up and right:
import flash.events.StageOrientationEvent;
if (stage.supportsOrientationChange) {
stage.addEventListener(StageOrientationEvent.ORIENTATION_CHANGE,onChange);
}
function onChange(event:StageOrientationEvent):void {
trace(event.beforeOrientation);
trace(event.afterOrientation);
// default
// rotatedLeft
// rotatedRight
}
检查设备网络是否可连接
Unlike with the desktop experience, you cannot assume your user is always connected when using a mobile device. She might be moving in and out of networks. Be prepared to display a message if your application is not able to function because of this.
You can check the status on URLMonitor for an HTTP request or onSocketMonitor for socket connection request. Both classes extend from the ServiceMonitor class.
import URLMonitor;
import SocketMonitor;
import flash.net.URLRequest;
import flash.events.StatusEvent;
var monitor:URLMonitor = new URLMonitor(new URLRequest("http://www.google.com"));
monitor.pollInterval = 1000;
monitor.addEventListener(StatusEvent.STATUS, onStatus);
monitor.start();
function onStatus(event:StatusEvent):void {
monitor.stop();
trace(event.code); // Service.available or Service.unavailable
trace(event.target.available); // true or false
-------------------第6章----------------------
如何隐藏屏幕下方的状态栏
By default, your application occupies the screen underneath the 38-pixel-tall status bar that displays information such as network signal, battery life, time, and notifications. If you wish to hide this bar and go full screen, in Flash Professional selectFull Screen under File→AIR Android settings→General. In Flash Builder, add the following node as a child of the<initialWindow> node in the application descriptor:
<fullScreen>true</fullScreen>
监听应用是否处于存活状态
NativeApplication dispatches an exiting event when it starts the closing process. You can register for that event and be prepared to act quickly according to your application needs:
import flash.desktop.NativeApplication;
import flash.events.Event;
function onExit(event:Event):void {
// save application's current state
}
However, the Android OS may choose to terminate background processes immediately if RAM is needed. In some cases, such as receiving a phone call, there is
no guarantee that NativeApplication will have a chance to dispatch the exiting event before being shut down. It is therefore a good practice to
save critical data often while the applicationis active. We will discuss how to save data locally later in this chapter.
关于设备的不同目录
File.ApplicationStorageDirectory, the storage directory allocated to your application. Use this location to savefairly small amounts of data and information such as preferences and user settings.
Externally, data can be saved on the device’s SD card under the File.documentsDirectory directory, also referred to asFile.userDirectory orFile.desktopDirectory. Use this approach for any relativelylarge amounts of data, such as images or video or temporary files. Create a directory with your application name to keep it distinct from other applications’ data.
Writing to the card requires a permission, which needs to be added to the descriptor file. If you don’t have this permission, AIR will throw a runtime error:
<uses-permission android:name= "android.permission.WRITE_EXTERNAL_STORAGE" />
Before installing any data, make sure the user’s phone has an SD card:
if (File.userDirectory)
// proceeds with saving data
Here is the list of directories and their equivalent paths:
/data/data/app.appId/app/assets
app:/
File.applicationDirectory
/data/data/app.appID/appID/Local Store
app-storage:/
/data/data/app.appID/appID/Local Store
File.applicationStorageDirectory
/sdcard
File.documentsDirectory
File.userDirectory
File.desktopDirectory
/data/data/app.appId/cache
File.createTempDirectory()
File.createTempFile()
监听shareObject是否将数据存储成功
If you wish to monitor the save process, create a String variable to store what the flush method returns. If the result is pending, set a listener to trace the reason the data
was not stored:
import flash.net.SharedObjectFlushStatus;
import flash.events.NetStatusEvent;
var flushStatus:String = so.flush();
if (flushStatus != null) {
switch(flushStatus) {
case SharedObjectFlushStatus.PENDING:
so.addEventListener(NetStatusEvent.NET_STATUS,
onFlushStatus);
break;
case SharedObjectFlushStatus.FLUSHED:
trace("success");
break;
}
}
读删文件之前要验证文件是否存在
if (!file.exists) {
return;
}
创建数据库文件
import flash.filesystem.File;
function createDatabase():void {
var file:File =
File.applicationStorageDirectory.resolvePath("myData.db");
if (file.exists) {
trace("I already exist, ready to be used");
} else {
trace("I did not exist, now I am created");
}
}
建立数据库连接
import flash.data.SQLConnection;
import flash.events.SQLEvent;
import flash.events.SQLErrorEvent;
var connection:SQLConnection;
connection = new SQLConnection();
try {
connection.open(file);
trace("connection opened");
} catch(error:Error) {
trace(error.message);
}
数据库操作-带变量的条件匹配
var statement:SQLStatement = new SQLStatement();
statement.sqlConnection = connection;
var insert:String = "INSERT INTO geography (country, city) VALUES (:co, :ci)";
statement.text = insert;
statement.parameters[":co"] = object.country;
statement.parameters[":ci"] = object.city;
try {
statement.execute();
trace("item created");
} catch(error:SQLError) {
trace(error.message);
}
}
.execute(1)的用法
Instead of requesting the entire table, you may want to receive only one item in the table. Let’s request the country that has New York as a city.Execute(1)only returns
the item stored under table ID 1:
var statement:SQLStatement = new SQLStatement();
statement.sqlConnection = connection;
statement.text = "SELECT country FROM geography WHERE city = 'New York'";
try {
statement.execute(1);
var result:SQLResult = statement.getResult();
if (result.data != null) {
trace(result.data[0].country);
}
} catch(error:Error) {
trace("item", error.message);
}
-------------------第7章----------------------
检测设备是否支持多点触控
Testing support for multitouch is done separately:
if (Multitouch.supportsTouchEvents == true) {
trace(Multitouch.maxTouchPoints);
}
zoom手势
TransformGestureEvent.GESTURE_ZOOM
rotate手势
TransformGestureEvent.GESTURE_ROTATION
pan手势
TransformGestureEvent.GESTURE_PAN
swipe手势
TransformGestureEvent.GESTURE_SWIPE
Left to right and bottom to top returns -1. Right to left and bottom to top returns 1.
Press and Tap手势
PressAndTapGestureEvent.GESTURE_PRESS_AND_TAP
Two-Finger Tap手势
PressAndTapGestureEvent.GESTURE_TOW_FINGER_TAP
物体拖拽实例
-------------------第7章----------------------
When using these classes, you should first check at runtime that the user’s device has a motion sensor and that it is available for reading. Look at the value of the isSuppor ted boolean property:
import flash.sensors.Accelerometer;
if (Accelerometer.isSupported == false) {
return;
}
The accelerometer muted property is false if the user blocks access to the accelerometer sensor. Android devices do not provide an option to disable access at the time of this writing, so you can ignore it.
The flash.media.CameraRoll class is a subclass of the EventDispatcher class. It gives you access to the Gallery. It is not supported for AIR desktop applications.
-------------------第9章----------------------
A Media Event.COMPLETE is dispatched after a picture is taken, an Event.CANCEL if no media is selected, and anErrorEvent if there is an error in the process:
cameraUI.addEventListener(MediaEvent.COMPLETE, onComplete);
cameraUI.addEventListener(Event.CANCEL, onCancel);
cameraUI.addEventListener(ErrorEvent.ERROR, onError);
Several open source AS3 libraries are available for reading EXIF data. I chose the one by Kenichi Ishibashi. You can download his library using Subversion at http://code
.shichiseki.jp/as3/ExifInfo/.
import flash.display.Loader;
import flash.display.MovieClip;
import flash.media.CameraRoll;
import flash.media.MediaPromise;
import flash.events.MediaEvent;
import flash.events.Event;
import flash.net.URLRequest
import jp.shichiseki.exif.*;
var loader:ExifLoader;
var cameraRoll:CameraRoll;
function Exif1() {
if (CameraRoll.supportsBrowseForImage) {
init();
}
}
function init():void {
cameraRoll = new CameraRoll();
cameraRoll.addEventListener(MediaEvent.SELECT, onSelect);
cameraRoll.browseForImage();
}
function onSelect(event:MediaEvent):void {
var promise:MediaPromise = event.data as MediaPromise;
loader = new ExifLoader();
loader.addEventListener(Event.COMPLETE, imageLoaded);
loader.load(new URLRequest(promise.file.url));
}
function imageLoaded(event:Event):void {
var exif:ExifInfo = loader.exif as ExifInfo;
if (exif.thumbnailData) {
var thumbLoader:Loader = new Loader();
thumbLoader.loadBytes(exif.thumbnailData);
addChild(thumbLoader);
}
}
-------------------第10章----------------------
关于GPS(global-positoning-system)
import flash.sensors.Geolocation;
import flash.events.GeolocationEvent;
var geolocation:Geolocation;
if (Geolocation.isSupported) {
geolocation = new Geolocation();
geolocation.addEventListener(GeolocationEvent.UPDATE, onTravel);
}
function onTravel(event:GeolocationEvent):void {
trace(event.latitude);
trace(event.longitude);
}
实时方位更新
import flash.events.StatusEvent;
if (!geolocation.muted) {
geolocation.addEventListener(StatusEvent.STATUS, onStatusChange);
} else {
// inform the user to turn on the location sensor
}
function onStatusChange(event:StatusEvent):void {
trace("status:" + event.code);
if (event.code == "Geolocation.Muted") {
// inform the user to turn on the location sensor
}
}
-------------------第11章----------------------
使用麦克风
<uses-permission android:name="android.permission.RECORD_AUDIO"/>
import flash.media.Microphone;
if (Microphone.isSupported) {
// continue on
}
var microphone:Microphone = Microphone.getMicrophone();
gain works as a volume multiplier and has a value between 0 and 100. The default value is 50 or a multiplier of 1. Any value above 50 boosts the microphone and below reduces it.
activityLevel is a read-only property that returns the amount of sound detected, from 0 to 100. It can be used as a visual aid for users to monitor how loud they should be
speaking: