嗨,我尝试按照他们的教程使用 Titanium 开发一个简单的 Android 服务应用程序。当应用程序处于前台时它运行良好,但是当我按下后退按钮时它会杀死应用程序并且我的服务没有运行。
下面是我使用的代码和应用程序进入后台时得到的日志。请让我知道我哪里出错了。(我正在使用带有 Android Runtime v8/Rhino 的 Titanium SDK 2.0.1.GA2 和 Android SDK 2.2]。
我的 app.js 文件:
var intent = Titanium.Android.createServiceIntent( { url: 'logservice.js' } );
// Service should run its code every 10 seconds.
intent.putExtra('interval', 10000);
// A message that the service should 'echo'
intent.putExtra('message_to_echo', 'Titanium rocks!');
var service = Titanium.Android.createService(intent);
service.addEventListener('resume', function(e) {
Titanium.API.info('Service code resumes, iteration ' + e.iteration);
});
service.addEventListener('pause', function(e) {
Titanium.API.info('Service code pauses, iteration ' + e.iteration);
/* if (e.iteration === 3) {
Titanium.API.info('Service code has run 3 times, will now stop it.');
service.stop();
} */
});
service.start();
我的 logservice.js 文件是:
var service = Titanium.Android.currentService;
var intent = service.intent;
var message = intent.getStringExtra("message_to_echo");
Titanium.API.info("Hello World! I am a Service. I have this to say: " + message);
我在 tiapp.xml 中包含了该服务标签
这是我按下后退按钮时得到的日志:
W/KeyCharacterMap( 322): No keyboard for id 0
W/KeyCharacterMap( 322): Using default keymap: /system/usr/keychars/qwerty.kcm.bin
E/TiBaseActivity( 322): (main) [62040,92891] Layout cleanup.
D/dalvikvm( 322): GC_FOR_MALLOC freed 9526 objects / 480272 bytes in 115ms
D/dalvikvm( 322): GC_FOR_MALLOC freed 12770 objects / 693152 bytes in 80ms
D/dalvikvm( 322): GC_FOR_MALLOC freed 8846 objects / 508872 bytes in 77ms
D/dalvikvm( 322): GC_FOR_MALLOC freed 8976 objects / 512112 bytes in 78ms
D/EventEmitter( 322): callHandler function not available for resume
W/TiAnalyticsSvc( 322): (Thread-15) [29966,122857] Analytics Service Started
I/TiAnalyticsSvc( 322): (Thread-15) [69,122926] Sending 1 analytics events.
D/dalvikvm( 322): GC_FOR_MALLOC freed 7386 objects / 436936 bytes in 87ms
D/NativeCrypto( 322): Freeing OpenSSL session
D/dalvikvm( 322): GC_FOR_MALLOC freed 1581 objects / 89648 bytes in 71ms
I/dalvikvm-heap( 322): Grow heap (frag case) to 3.588MB for 87396-byte allocation
D/dalvikvm( 322): GC_FOR_MALLOC freed 12 objects / 712 bytes in 76ms
D/dalvikvm( 322): GC_FOR_MALLOC freed 956 objects / 49400 bytes in 88ms
I/dalvikvm-heap( 322): Grow heap (frag case) to 3.624MB for 87396-byte allocation
D/dalvikvm( 322): GC_FOR_MALLOC freed 0 objects / 0 bytes in 75ms
W/TiAnalyticsSvc( 322): (Thread-15) [3772,126698] Stopping Analytics Service
D/SntpClient( 60): request time failed: java.net.SocketException: Address family not supported by protocol