`
longgangbai
  • 浏览: 7331172 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

Titanium调用小技巧

 
阅读更多

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]) + '")');
	}
}
 
分享到:
评论

相关推荐

    Titanium plugin开发初探

    这些插件可以包括访问硬件设备(如摄像头、蓝牙),调用系统服务(如地图、推送通知),或者集成第三方库。 **2. 开发环境准备** 在开始 Titanium 插件开发之前,确保已安装以下组件: - Titanium SDK:安装最新的 ...

    TitaniumBackup_6.0.5.1

    《TitaniumBackup_6.0.5.1:专业版的安卓备份与恢复解决方案》 在安卓设备的管理和维护中,数据备份与恢复是至关重要的环节。TitaniumBackup_6.0.5.1,这款专业版应用,以其强大的功能和高效的操作,为用户提供了...

    Titanium中文版开发手册

    **Titanium中文版开发手册** Titanium中文版开发手册是一份专门为中文用户编译的开发者指南,旨在帮助熟悉中文的开发者充分利用Titanium框架进行移动应用的开发。Titanium是一个开源的JavaScript平台,允许开发者...

    Titanium Mobile API

    - **最佳实践**:分享了编写高质量应用程序的经验和技巧。 #### 四、版权与许可说明 根据文档中的版权声明,Titanium Mobile API 的文档和源码受 Appcelerator, Inc. 的版权保护。个人可以存储文档于单一计算机中...

    前端开源库-node-titanium-sdk

    在实际开发过程中,使用`node-titanium-sdk`时,开发者需要熟悉Titanium的API,理解如何通过JavaScript代码调用这些API来实现原生功能。同时,还需要掌握基本的移动应用设计原则,以确保应用在不同平台上拥有良好的...

    [Titanium] Appcelerator Titanium 移动应用开发教程 (英文版)

    [Packt Publishing] Appcelerator Titanium 移动应用开发教程 (英文版) [Packt Publishing] Creating Mobile Apps with Appcelerator Titanium (E-Book) ☆ 图书概要:☆ Develop fully-featured mobile ...

    Titanium Backup_3.7.4捐赠完全版

    Titanium Backup_3.7.4捐赠完全版

    titanium 打开本地网络

    Titanium 是一个强大的开源JavaScript框架,专为开发原生移动应用而设计。它允许开发者使用JavaScript编写代码,同时能够利用iOS、Android等平台的原生功能。在涉及到“titanium 打开本地网络”的话题时,我们主要...

    titanium-d1-kickstart.6.1.1

    titanium-d1-kickstart.6.1.1.gbin 强大的思科模拟器

    Titanium开发者平台介绍

    这类项目涉及到了API调用、UI设计、事件处理等多个方面,虽然会遇到一些挑战(如调试问题),但是官方提供的资源(如KitchenSink示例项目)能极大帮助开发者解决问题。 综上所述,Titanium开发者平台以其独特的优点...

    Titanium中支持IOS设备的拖拽

    在iOS开发中,Titanium是一个流行的跨平台框架,它允许开发者使用JavaScript编写代码,同时能够构建原生的iOS和Android应用程序。"Titanium中支持iOS设备的拖拽"这一主题聚焦于如何在Titanium框架下实现iOS应用的...

    Titanium Mobile SDK 3.1.0 Apidoc 离线版

    Titanium Mobile SDK 3.1.0 是一个用于构建原生移动应用的开发工具,尤其针对iOS和Android平台。这个版本的Apidoc是开发者的重要参考资料,它包含了完整的API文档,帮助开发者理解并使用Titanium框架的各种功能。...

    Titanium[1].Backup.Pro.v3.7.2

    Titanium[1].Backup.Pro.v3.7.2_cnFixed_newkey.apk Titanium[1].Backup.Pro.v3.7.2_cnFixed_newkey.apk

    Titanium学习教程

    - **本地功能调用**:通过Titanium API可以直接访问设备的各种硬件特性(如摄像头、GPS等),实现与原生应用相同的体验。 - **Web技术**:前端开发者可以利用HTML、CSS和JavaScript等熟悉的Web技术来构建界面,降低...

    Google-Cloud-Messaging--Titanium-, 在 Titanium 中,Google云消息传递.zip

    Google-Cloud-Messaging--Titanium-, 在 Titanium 中,Google云消息传递 Google-Cloud-Messaging--Titanium -注册带有GCM和处理发送到设备的通知的Titanium MODULE 。Android平台使用c2dm进行推送,但是因为c2dm停止...

    atom-titanium, 用于 Titanium 合金的Atom 封装.zip

    atom-titanium, 用于 Titanium 合金的Atom 封装 用于 Titanium 合金的 All-in-One封装这是一个用于 Titanium 合金的Atom 封装。$ apm install titanium-alloy冲突&需要通知Alloy 1.8. x

    Titanium 2.1API

    这是截至到目前最新的版本(Titanium 2.1 API) 方便没网的情况下,童鞋们翻阅文档。

    【mac】dvd光盘刻录 Roxio Toast Titanium 17.4.dmg

    【mac】dvd光盘刻录 Roxio Toast Titanium 17.4.dmg,安装即用

    Titanium Backup Pro 8.0.0.apk

    钛备份 Titanium Backup,备份你的程序及程序数据,并支持定时备份等功能。 功能很强大,有此软件,不管你的格机还是重刷ROM,在重装软件时都是无痛的,非实适用经常捣鼓手机有机友! 解锁器使用说明: 1.安装钛...

Global site tag (gtag.js) - Google Analytics