读书人

在Titanium中运用Android的Service

发布时间: 2012-07-27 11:03:00 作者: rapoo

在Titanium中使用Android的Service
在使用Titanium做MP3播放器的时候,对于Android平台,我们可以不用特别的考虑,使用Titanium.Media.AudioPlayer、Titanium.Media.Sound等函数即可在播放声音,当前Activity终了新的Activity生成的时候,正在播放的身音也不会被中止。

在我们使用手机时,往往是多线程的,而且是会突然间断的。比如我们会一边浏览网页,一边听MP3,或者正在发短信的时候,突然打进来电话。这样我们必定会离开当前的用户界面。比如你会关闭MP3歌曲列表,去发短信,所以往往就需要有些程序在后台完成,暂时脱离屏幕。

那么对于Android平台来说,是如何实现的呢?答案很简单,这时就可以用到Android中的Service!

我们查看Titanium的例子KitchenSink可以发现,Titanium也支持Service。

<ti:app xmlns:ti="http://ti.appcelerator.org"> <android xmlns:android="http://schemas.android.com/apk/res/android"> <tool-api-level>7</tool-api-level> <manifest> <uses-sdk android:minSdkVersion="7"/> </manifest> <services> <service type="interval" url="playSoundService.js"/> </services> <activities/> </android></ti:app>

Titanium会为你自动生成以下代码:

import ti.modules.titanium.android.TiJSIntervalService;public final class PlaySoundServiceService extends TiJSIntervalService {public PlaySoundServiceService() {super("playSoundService.js");}}


并且在AndroidManifest.xml注册:
<service android:name="《tiapp_id》.PlaySoundServiceService"/>


通过以下的尝试,我们发现可以实现我们的需求。

(1)首先通过Titanium.Android.createServiceIntent() 得到Intent,然后可以利用putExtra('interval', 1000);方法设置参数,其他任何参数也可以这样设置。
var soundIntent = Ti.Android.createServiceIntent( { url:  'playSoundService.js' } );soundIntent.putExtra('interval', 1000);


(2)把上边设置好的Intent传递给Titanium.Android.createService(),然后调用返回实例的start()方法从而启动Servie。
sound_service = Ti.Android.createService( soundIntent );sound_service.start();


但是需要注意的是,Service在每次启动的时候都会从代码的开始执行,所以我们需要判断当前Service是否已经的状态从而作出不同的处理,在Titanium中可以通过Titanium.Android.currentService获取当前Service的实例,这样一来我们就可以判断出是否是第一启动还是Service的循环处理了。

↓ Service例( playSoundService.js )

var service = Ti.Android.currentService;var intent = service.intent;if( !service.initialized ){service.addEventListener( 'playSound' , function( e ){if( service.sound == undefined || service.sound.url != service.fileToPlay ){if( service.sound ){service.sound.stop();}service.sound = Ti.Media.createSound({url:service.fileToPlay});}service.sound.play();});service.addEventListener( 'pauseSound' , function( e ){service.sound.pause();});service.addEventListener( 'resumeSound' , function( e ){service.sound.play();});service.addEventListener( 'stopSound' , function( e ){service.sound.stop();});service.initialized = true;}

给Service赋予了sound相关的处理,在Activity中也可以使用。

在Activity中,判断你想要启动的Service是否已经启动了,可以使用Titanium.Android.isServiceRunning,如果没有启动的话,把它启动起来!

↓ JavaScript(Activity)代码:

var sound_service;var soundIntent = Ti.Android.createServiceIntent( { url:  'playSoundService.js' } );if( Ti.Android.isServiceRunning( soundIntent ) == false ){Ti.API.info( 'service is not Running' );soundIntent.putExtra('interval', 1000);sound_service = Ti.Android.createService( soundIntent );sound_service.addEventListener('resume', function(e) {if( e.source.sound ){Ti.API.info( 'sound.getTime() ' + e.source.sound.getTime() );}});sound_service.addEventListener('pause', function(e) {});sound_service.addEventListener('stop', function(e) {Ti.API.info('Service is stopped');});sound_service.start();}else{Ti.API.info( 'service is Running' );sound_service = Ti.Android.currentService;}

这样一来,Service和Activity就能偶衔接起来。给Service实例设置好参数后,通过fireEvent 方法来调用事件。

例如上边的代码,如果想播放声音的话,就需要在JavaScript(Activity)中:

sound_service.fileToPlay = 'music.mp3';sound_service.fireEvent( 'startSound' );


这样就会在Service中播放声音了!!!

===========================================================
===========================================================
需要注意的几个地方:

a) Ti.Android.currentService返回的实例一定是我们自己预想的Service吗?
???

b) Service什么时候停止比较合适?
???

c) 播放音乐的话,一般都不是定期处理,所以是否应该采用interval以外的方法?
???

读书人网 >Android

热点推荐