在stackoverflow上有人问到Titanium Mobile是如何运行的,AppceleratorCEO Jeff Haynie 给出了他的回答,这里给大家转过来看看!
How Does Appcelerator Titanium Mobile Work?
Question:
Haynie's Answer:
So, a simple way to think about it is that your JS code is compiled almost one to one into the representative symbols in nativeland. There's still an interpreter running in interpreted mode otherwise things like dynamic code wouldn't work. However, its much faster, much more compact and it's about as close to pure native mapping as you can get.
We're obviously still got plenty of room to improve this and working on that. So far in our latest 1.0 testing, it's almost indistinguishable from the same objective-c direct code (since in most cases it's exactly mapped to that). From a CompSci standpoint, we can now however start to optimize things that a human really couldn't easily do that - much like the GCC compiler already does today.
http://stackoverflow.com/questions/4217551/what-happens-to-javascript-code-after-app-is-compiled-using-titanium-mobile
Kevin Whinnery's Answer:
It's true that Titanium Mobile used the WebView (in both Android and iOS) in the pre-1.0 days. However, this is no longer true and hasn't been since our 1.0 release is March 2010.
Since 1.0, we've shipped two separate Javascript runtimes with our apps, and we are running the Javascript code directly without a WebView. Your entire app from start to finish is now controlled by JS, and we provide a comprehensive set of Native APIs that enable this. Everything from UI widgets (yes, including WebView), Core APIs like Networking, Filesystem, Database, all the way to OS-specific things like JS Activities in Android. On the JS runtime front, we're shipping a forked version of WebKit's JavaScriptCore in iOS and a snapshot of Rhino 1.7 R3 CVS for Android. What we actually do with your javascript source is dependent on the platform, but generally it breaks up like this:
Source is statically analyzed to find references to Titanium modules
Localization strings (strings.xml), App metadata (tiapp.xml), and density specific images all generate platform specific analogs.
In iOS:
An XCode project / configuration is generated
JS Source is base64'd and inlined as a variable into a generated C file
xcodebuild is used to generate the final binaries
provisioning profiles, signing keys etc are applied
iTunes and some other glue are used to send the IPA to your iOS device
In Android:
An Android / Eclipse project is generated
In "Development" mode, JS source is packaged as APK assets
In "Distribution" (production) mode, when you're ready to ship the app, we compile the JS to Java bytecode using the Rhino JSC compiler. You can also enable this during development mode by setting "ti.android.compilejs" to "true" in tiapp.xml, see: http://developer.appcelerator.com/question/100201/enable-android-byte-code-compile
dex, aapt, and other Android SDK tools are used to build and generate the final APK
adb and keytool are used for pushing the APK out to the emulator and/or device
There are many more details that I could dive into specifically on each of these points, but the point I wanted to drive home is that we no longer use the WebView as our Javascript engine. You can however still embed WebViews, and we provide some simple integration that allows you to call Titanium APIs from an embedded WebView.
-Kevin Dev Relations Manager
pendingIntent字面意义:等待的,未决定的Intent。
要得到一个pendingIntent对象,使用方法类的静态方法
getActivity(Context, int, Intent, int)
, getBroadcast(Context, int, Intent, int)
,
getService(Context, int, Intent, int) 分别对应着Intent的3个行为,跳转到一个activity组件、打开一个广播组件和打开一个服务组件。
参数有4个,比较重要的事第三个和第一个,其次是第四个和第二个。可以看到,要得到这个对象,必须传入一个Intent作为参数,必须有context作为参数。
pendingIntent是一种特殊的Intent。主要的区别在于Intent的执行立刻的,而
pendingIntent的执行不是立刻的。
pendingIntent执行的操作实质上是参数传进来的Intent的操作,但是使用
pendingIntent的目的在于它所包含的Intent的操作的执行是需要满足某些条件的。
主要的使用的地方和例子:通知
Notificatio的发送,短消息
SmsManager
的发送和警报器AlarmManager的执行等等。
Android 的状态栏通知 (Notification)
如果需要查看消息,可以拖动状态栏到屏幕下方即可查看消息。
步骤:
1 获取通知管理器 NotificationManager ,它也是一个系统服务
2 建立通知 Notification notification = new Notification(icon, null, when);
3 为新通知设置参数 ( 比如声音,震动,灯光闪烁 )
4 把新通知添加到通知管理器
发送消息的代码如下:
// 获取通知管理器
NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE)
int icon = android.R.drawable.stat_notify_chat;
long when = System.currentTimeMillis();// 通知发生的时间为系统当前时间
// 新建一个通知,指定其图标和标题
Notification notification = new Notification(icon, null, when);// 第一个参数为图标 , 第二个参数为短暂提示标题 , 第三个为通知时间
notification.defaults = Notification.DEFAULT_SOUND;// 发出默认声音
notification.flags |= Notification.FLAG_AUTO_CANCEL;// 点击通知后自动清除通知
Intent openintent = new Intent(this, OtherActivity.class);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, openintent, 0);/ / 当点击消息时就会向系统发送 openintent 意图
notification.setLatestEventInfo(this, “ 标题 ”, “ 我是内容 ", contentIntent);
mNotificationManager.notify(0, notification);// 第一个参数为自定义的通知唯一标识
重点是 setLatestEventInfo( ) 方法的最后一个参数!!!!它是一个 PendingIntent!!!!!!!!!
这里使用到了 PendingIntent(pend 本意是待定,不确定的意思 )
PendingIntent 可以看作是对 Intent 的包装。 PendingIntent 主要持有的信息是它所包装的 Intent 和当前 Application 的 Context 。正由于 PendingIntent 中保存有当前 Application 的 Context ,使它赋予带他程序一种执行的 Intent 的能力,就算在执行时当前 Application 已经不存在了,也能通过存在 PendingIntent 里的 Context 照样执行 Intent 。
PendingIntent 的一个很好的例子:
SmsManager 的用于发送短信的方法:
sendTextMessage(destinationAddress, scAddress, text, sentIntent, deliveryIntent);
第一个参数: destinationAddress 对方手机号码
第二个参数: scAddress 短信中心号码 一般设置为空
第三个参数: text 短信内容
第四个参数: sentIntent 判断短信是否发送成功,如果你没有 SIM 卡,或者网络中断,则可以通过这个 itent 来判断。注意强调的是“发送”的动作是否成功。那么至于对于对方是否收到,另当别论
intent
英文意思是意图,
pending
表示即将发生或来临的事情。
PendingIntent
这个类用于处理即将发生的事情。比如在通知
Notification
中用于跳转页面,但不是马上跳转。
Intent
是及时启动,
intent
随所在的
activity
消失而消失。
PendingIntent
可以看作是对
intent
的包装,通常通过
getActivity,getBroadcast ,getService
来得到
pendingintent
的实例,当前
activity
并不能马上启动它所包含的
intent,
而是在外部执行
pendingintent
时,调用
intent
的。正由于
pendingintent
中
保存有当前
App
的
Context
,使它赋予外部
App
一种能力,使得外部
App
可以如同当前
App
一样的执行
pendingintent
里的
Intent
,
就算在执行时当前
App
已经不存在了,也能通过存在
pendingintent
里的
Context
照样执行
Intent
。另外还可以处理
intent
执行后的操作。常和
alermanger
和
notificationmanager
一起使用。
Intent
一般是用作
Activity
、
Sercvice
、
BroadcastReceiver
之间传递数据,而
Pendingintent
,一般用在
Notification
上,
可以理解为延迟执行的
intent
,
PendingIntent
是对
Intent
一个包装。
Java 代码
1. private void showNotify(){
2. Notification notice= new Notification();
3. notice.icon=R.drawable.icon;
4. notice.tickerText= " 您有一条新的信息 " ;
5. notice.defaults=Notification.DEFAULT_SOUND;
6. notice.when=10L;
7. // 100 毫秒延迟后,震动 250 毫秒,暂停 100 毫秒后,再震动 500 毫秒
8. //notice.vibrate = new long[] { 100, 250, 100, 500 }; 出错?
9. //notice.setLatestEventInfo(this, " 通知 ", " 开会啦 ", PendingIntent.getActivity(this, 0, null, 0));
10. notice.setLatestEventInfo( this , " 通知 " , " 开会啦 " , PendingIntent.getActivity( this , 0 , new Intent( this ,Activity2. class ), 0 )); // 即将跳转页面,还没跳转
11. NotificationManager manager=(NotificationManager)getSystemService( this .NOTIFICATION_SERVICE);
12. manager.notify( 0 ,notice);
13. }
1. GSM
网络中
android
发送短信示例
Java 代码
1. String msg = " 你好,美女 " ;
2. String number = "135****6784" ;
3. SmsManager sms = SmsManager.getDefault();
4.
5. PendingIntent pi = PendingIntent.getBroadcast(SmsActivity. this , 0 , new Intent(...), 0 );
6. sms.sendTextMessage(number, null , msg, pi, null );
7. Toast.makeText(SmsActivity. this , " 发送成功 " ,Toast.LENGHT_LONG).show();
代码解释
PendingIntent
就是一个
Intent
的描述,我们可以把这个描述交给别的程序,别的程序根据这个描述在后面的别的时间做你安排做的事情
(
By
giving a PendingIntent to another application, you are granting it the
right to perform the operation you have specified as if the other
application was yourself
,就相当于
PendingIntent
代表了
Intent
)。本例中别的程序就是发送短信的程序,短信发送成功后要把
intent
广播出去
。
函数
SmsManager.sendTextMessage(String
destinationAddress, String scAddress, String text, PendingIntent
sentIntent, PendingIntent deliveryIntent)
中参数解释:
1
)
PendingIntent sentIntent
:当短信发出时,成功的话
sendIntent
会把其内部的描述的
intent
广播出去,否则产生错误代码并通过
android.app.PendingIntent.OnFinished
进行回调,这个参数最好不为空,否则会存在资源浪费的潜在问题;
2
)
PendingIntent deliveryIntent
:是当消息已经传递给收信人后所进行的
PendingIntent
广播。
查看
PendingIntent
类可以看到许多的
Send
函数,就是
PendingIntent
在进行被赋予的相关的操作。
转自:http://www.cnblogs.com/lhxin/archive/2011/10/20/2218522.html