1.复制粘贴技术
设置粘贴板的内容:
Titanium.UI.Clipboard.setText(source.value);
获取粘贴板内容
if (Titanium.UI.Clipboard.hasText()) {
dest.value = Titanium.UI.Clipboard.getText();
} else {
alert('No text on clipboard.');
}
2.JSON字符串的解析
JSON.parse(str)
生成JSON字符串的方法
JSON.stringify({title:'当前故障一览',type:4})
3.设置属性
Ti.App.Properties.setString('servicePort', txtPort.value);
获取属性
Ti.App.Properties.getString('servicePort');
4.base64加密
var data = "abcdefg";
var encoded = Ti.Utils.base64encode(data);
base64解密
var encoded = Ti.Utils.base64decode(encoded.toString());
SHA加密
var sha1 = Ti.Utils.sha1("abc");
md5加密
Ti.Utils.md5HexDigest(data)
5.窗体大小
var button = Ti.UI.createButton({
title : '',
top: Ti.Platform.displayCaps.platformHeight/2-10,
left:Ti.Platform.displayCaps.platformWidth/2-110,
width : 100,
height : 60
});
6.titanium相关平台的信息
var win = Titanium.UI.currentWindow;
// for battery level to work, you have to turn it
// on first otherwise it will report -1. if you
// add a battery listener, it will turn it on for you
// automagically
var needUpdate = false;
Titanium.Platform.batteryMonitoring = true;
var batteryEventListener;
win.addEventListener('close',function()
{
// turn it off, no need to waste the battery
Titanium.Platform.batteryMonitoring = false;
Titanium.Platform.removeEventListener('battery', batteryEventListener);
});
function batteryStateToString(state)
{
switch (state)
{
case Titanium.Platform.BATTERY_STATE_UNKNOWN:
return 'unknown';
case Titanium.Platform.BATTERY_STATE_UNPLUGGED:
return 'unplugged';
case Titanium.Platform.BATTERY_STATE_CHARGING:
return 'charging';
case Titanium.Platform.BATTERY_STATE_FULL:
return 'full';
}
return '???';
}
var l1 = Titanium.UI.createLabel({
text:'name/osname:' + Titanium.Platform.name+'/'+Titanium.Platform.osname,
top:10,
left:10,
width:'auto',
font:{fontSize:14},
color:'#777',
height:'auto'
});
win.add(l1);
var l2 = Titanium.UI.createLabel({
text:'model:' + Titanium.Platform.model,
top:30,
left:10,
width:300,
height:'auto',
font:{fontSize:14},
color:'#777'
});
win.add(l2);
var l3 = Titanium.UI.createLabel({
text:'version:' + Titanium.Platform.version,
top:50,
left:10,
width:300,
height:'auto',
font:{fontSize:14},
color:'#777'
});
win.add(l3);
var l4 = Titanium.UI.createLabel({
text:'architecture:' + Titanium.Platform.architecture,
top:70,
left:10,
width:300,
height:'auto',
font:{fontSize:14},
color:'#777'
});
win.add(l4);
var l5 = Titanium.UI.createLabel({
text:'macaddress:' + Titanium.Platform.macaddress,
top:90,
left:10,
width:'auto',
height:'auto',
font:{fontSize:14},
color:'#777'
});
win.add(l5);
var l6 = Titanium.UI.createLabel({
text:'processor count:' + Titanium.Platform.processorCount,
top:110,
left:10,
width:300,
height:'auto',
font:{fontSize:14},
color:'#777'
});
win.add(l6);
var l7 = Titanium.UI.createLabel({
text:'username:' + Titanium.Platform.username,
top:130,
left:10,
width:300,
height:'auto',
font:{fontSize:14},
color:'#777'
});
win.add(l7);
// NOTE: Needs to be tested on a physical device to get an accurate value;
// may select the wrong interface on non-mobile devices.
var l8 = Titanium.UI.createLabel({
text:'address:' + Titanium.Platform.address,
top:150,
left:10,
width:300,
height:'auto',
font:{fontSize:14},
color:'#777'
});
win.add(l8);
var l9 = Titanium.UI.createLabel({
text:'ostype:' + Titanium.Platform.ostype,
top:170,
left:10,
width:300,
height:'auto',
font:{fontSize:14},
color:'#777'
});
win.add(l9);
if (Titanium.Platform.batteryState == Ti.Platform.BATTERY_STATE_UNKNOWN) {
needUpdate = true;
}
var l11 = Titanium.UI.createLabel({
text:'battery state:' + batteryStateToString(Titanium.Platform.batteryState),
top:190,
left:10,
width:300,
height:'auto',
font:{fontSize:14},
color:'#777'
});
win.add(l11);
var l12 = Titanium.UI.createLabel({
text:'battery level:' + Titanium.Platform.batteryLevel,
top:210,
left:10,
width:300,
height:'auto',
font:{fontSize:14},
color:'#777'
});
win.add(l12);
var l13 = Titanium.UI.createLabel({
text:'display width-x-height:' + Titanium.Platform.displayCaps.platformWidth + 'x' + Titanium.Platform.displayCaps.platformHeight,
top:230,
left:10,
width:300,
height:'auto',
font:{fontSize:14},
color:'#777'
});
win.add(l13);
var l15 = Titanium.UI.createLabel({
text:'display density:' + Titanium.Platform.displayCaps.density,
top:250,
left:10,
width:300,
height:'auto',
font:{fontSize:14},
color:'#777'
});
win.add(l15);
var l16 = Titanium.UI.createLabel({
text:'display dpi:' + Titanium.Platform.displayCaps.dpi,
top:270,
left:10,
width:300,
height:'auto',
font:{fontSize:14},
color:'#777'
});
win.add(l16);
var l17 = Titanium.UI.createLabel({
text:'available memory:' + Titanium.Platform.availableMemory,
top:290,
left:10,
width:300,
height:'auto',
font:{fontSize:14},
color:'#777'
});
win.add(l17);
var l18 = Titanium.UI.createLabel({
text:'is24HourTimeFormat:' + Titanium.Platform.is24HourTimeFormat(),
top:310,
left:10,
width:300,
height:'auto',
font:{fontSize:14},
color:'#777'
});
win.add(l18);
var b = Titanium.UI.createButton({
title:'Open URL',
height:30,
width:200,
top:330
});
win.add(b);
var openURL=1;
b.addEventListener('click', function()
{
var url;
switch(openURL % 3)
{
case 0:
url = 'http://www.google.com';
b.title='Open URL (web)';
break;
case 1:
url = 'tel:4043332222';
b.title='Open URL (tel)';
break;
case 2:
url = 'sms:4043332222';
b.title='Open URL (sms)';
break;
}
if (Titanium.Platform.name != 'android') {
if (!Titanium.Platform.canOpenURL(url)) {
Ti.API.warn("Can't open url: "+url);
}
}
Titanium.Platform.openURL(url);
openURL++;
});
//
// BATTERY STATE CHANGE EVENT
//
batteryEventListener = function(e)
{
if (needUpdate) {
l11.text = 'battery state:' + batteryStateToString(e.state);
l12.text = 'battery level:' + e.level;
} else {
//TODO: based on various reports from the google, you only get battery state changes
//at 5% intervals.... to test this, you gotta unplug and leave your phone sitting for awhile
var message = 'Battery Notification\n\nLevel: ' + e.level + ', State: '+batteryStateToString(e.state);
Titanium.UI.createAlertDialog({title:'Platform', message:message}).show();
}
};
Titanium.Platform.addEventListener('battery', batteryEventListener);
Titanium.API.info("Current Phone Locale is "+Titanium.Platform.locale);
Titanium.API.info("OS name is " + Titanium.Platform.osname);
Titanium.API.info("Runtime: " + Titanium.Platform.runtime);
if (Titanium.Platform.osname == 'iphone' || Titanium.Platform.osname == 'ipad') {
Titanium.API.info("Data network: " + Titanium.Platform.dataAddress);
Titanium.API.info("Netmask: " + Titanium.Platform.netmask);
}
文件系统相关
var win = Titanium.UI.currentWindow;
// path variables
Titanium.API.info('Resources Directory :' + Titanium.Filesystem.resourcesDirectory);
Titanium.API.info('Temp Directory :' + Titanium.Filesystem.tempDirectory);
Titanium.API.info('Application Directory :' + Titanium.Filesystem.applicationDirectory);
Titanium.API.info('Application Data Directory :' + Titanium.Filesystem.applicationDataDirectory);
Titanium.API.info('Application Support Directory :' + Titanium.Filesystem.applicationSupportDirectory);
Titanium.API.info('External Storage Available :' + Titanium.Filesystem.isExternalStoragePresent());
Titanium.API.info('Separator :' + Titanium.Filesystem.separator);
Titanium.API.info('Line Ending :' + Titanium.Filesystem.lineEnding);
var f = Titanium.Filesystem.getFile(Titanium.Filesystem.resourcesDirectory, 'text.txt');
Ti.API.info('file = ' + f);
var contents = f.read();
Ti.API.info("contents blob object = "+contents);
Ti.API.info('contents = ' + contents.text);
Ti.API.info('mime type = ' + contents.mimeType);
Ti.API.info('Blob\'s file = ' + contents.file);
Ti.API.info('nativePath = ' + f.nativePath);
Ti.API.info('Blob\'s file nativePath= ' + contents.file.nativePath);
Ti.API.info('exists = ' + f.exists());
Ti.API.info('size = ' + f.size);
Ti.API.info('readonly = ' + f.readonly);
Ti.API.info('symbolicLink = ' + f.symbolicLink);
Ti.API.info('executable = ' + f.executable);
Ti.API.info('hidden = ' + f.hidden);
Ti.API.info('writable = ' + f.writable);
Ti.API.info('name = ' + f.name);
Ti.API.info('extension = ' + f.extension());
Ti.API.info('resolve = ' + f.resolve());
Ti.API.info('created = ' + String(new Date(f.createTimestamp()))); // #2085 test
var dir = Titanium.Filesystem.getFile(Titanium.Filesystem.resourcesDirectory);
Ti.API.info('directoryListing = ' + dir.getDirectoryListing());
Ti.API.info('getParent = ' + dir.getParent());
Ti.API.info('spaceAvailable = ' + dir.spaceAvailable());
var newDir = Titanium.Filesystem.getFile(Titanium.Filesystem.applicationDataDirectory,'mydir');
Ti.API.info("Created mydir: " + newDir.createDirectory());
Ti.API.info('newdir ' + newDir);
var newFile = Titanium.Filesystem.getFile(newDir.nativePath,'newfile.txt');
newFile.write(f.read());
Ti.API.info('directoryListing for newDir = ' + newDir.getDirectoryListing());
Ti.API.info("newfile.txt created: " + String(new Date(newFile.createTimestamp())));
Ti.API.info("newfile.txt modified: " + String(new Date(newFile.modificationTimestamp())));
Ti.API.info("newfile.txt renamed as b.txt: " + newFile.rename('b.txt'));
var renamedFile = Titanium.Filesystem.getFile(newDir.nativePath, 'b.txt');
Ti.API.info("newfile.txt deleted (expected to fail): " + newFile.deleteFile());
Ti.API.info("b.txt deleted: " + renamedFile.deleteFile());
Ti.API.info("mydir deleted: " + newDir.deleteDirectory());
Ti.API.info('directoryListing for newDir after deleteDirectory = ' + newDir.getDirectoryListing());
if (Ti.Platform.name == 'android') {
var dir = Titanium.Filesystem.getFile(Titanium.Filesystem.externalStorageDirectory);
Ti.API.info('external directoryListing = ' + dir.getParent().getDirectoryListing());
}
var l = Titanium.UI.createLabel({text:'Check Log for details', width:300, height:'auto', textAlign:'center'});
win.add(l);
// test to make sure we can still access compiled JS files
var jsfile = Titanium.Filesystem.getFile(Titanium.Filesystem.resourcesDirectory,'app.js');
Ti.API.info("app.js exists? " + jsfile.exists());
Ti.API.info("app.js size? " + jsfile.size);
// test to make sure that #3385 is resolved - we can append files, blobs or strings to a file
// this will go away once Streams are fully integrated into the Filesystem API.
var testfile = Ti.Filesystem.getFile(Ti.Filesystem.applicationDataDirectory, 'text.txt');
Ti.API.info('text.txt exists? ' + testfile.exists());
Ti.API.info('text.txt size: ' + testfile.size + ' bytes');
if(!testfile.write("text written via write()\n")) {
Ti.API.info("could not write string to file.");
}
if(!testfile.write(Ti.Filesystem.getFile(Ti.Filesystem.resourcesDirectory, 'text_two.txt'), true)) {
Ti.API.info("could not append File object to file via write method.");
}
if(!testfile.write("\nText appended via write()", true)) {
Ti.API.info("could not append string to file via write method.");
}
Ti.API.info("------------");
Ti.API.info("Test file contents:\n" + (testfile.read()).text);
//these should all fail
var bad_params = [10000, true, {}];
for(var i = 0, j = bad_params.length; i < j; i++) {
if(!testfile.write(bad_params[i])) {
Ti.API.info('Expected failure: (first parameter is "' + (typeof bad_params[i]) + '")');
}
}