Appcelerator Android服务后台停止

时间:2013-08-05 10:27:01

标签: android service background appcelerator

我有appcelerator的问题,关于android后台服务。 服务启动,当我按下主页按钮时,服务仍在运行,当我单击后退按钮时。 但是当我从最近的应用程序列表中删除我的应用程序(按下长按主题按钮)时,服务停止。 这是我的呼叫服务代码:

var SECONDS = 6;
    // every 10 minutes
    var intent = Titanium.Android.createServiceIntent({
        url : 'myservice.js'
    });
    intent.putExtra('interval', SECONDS * 1000);
    intent.putExtra('message_to_echo', num);

    //in millimeter
    var service = Titanium.Android.createService(intent);

    service.addEventListener('resume', function(e) {
    //  num++;
    //  alert(num);
    });

    service.start();    

这是文件服务代码:

var service = Titanium.Android.currentService;
var intent = service.intent;
var message = intent.getStringExtra("message_to_echo");

var intent = Ti.Android.createIntent({
    flags : Ti.Android.FLAG_ACTIVITY_CLEAR_TOP | Ti.Android.FLAG_ACTIVITY_SINGLE_TOP,
    // Substitute the correct classname for your application
    className : 'com.mappa.angeli.MappaActivity',
});
intent.addCategory(Ti.Android.CATEGORY_LAUNCHER);

// Create a PendingIntent to tie together the Activity and Intent
var pending = Titanium.Android.createPendingIntent({
    intent: intent,
    flags: Titanium.Android.FLAG_UPDATE_CURRENT
});

// Create the notification
var notification = Titanium.Android.createNotification({
    // icon is passed as an Android resource ID -- see Ti.App.Android.R.
   // icon: Ti.App.Android.R.drawable.my_icon,
    contentTitle: 'Something Happened',
    contentText : 'Click to return to the application.',
   // tickerText: 'Our app made a notification!',
    defaults:Titanium.Android.NotificationManager.DEFAULT_SOUND,
    contentIntent: pending
});
// Send the notification.
Titanium.Android.NotificationManager.notify(0, notification);

如何连续运行服务,例如什么应用程序或类似的东西? 请帮助我,这非常重要。 在此先感谢!!!

1 个答案:

答案 0 :(得分:0)

首先让我给你一些关于你的问题的观点,以便你能够理解背后的逻辑。

  

我对您的问题和结论的理解

即使在原生应用中,您所要求的也是不可能的,特别是如果您使用的是后台服务。

  

为什么会这样做?

     

因为该应用程序已不再使用。该应用程序是   强行从背景中删除,因此操作系统会停止所有操作   在后台运行的服务可以清除内存。

大多数默认播放器播放音乐的方法是使用前台服务并显示应用正在运行的通知,并在后台处理某些内容(在这种情况下播放歌曲)。

  

您可以查找的选项

我建议您将“推送通知服务”添加到您的应用中,而不是进入“后台服务”。即使您的应用不在后台,您也会收到通知(与WhatsApp中的相同)。

您可以按照以下链接为Android实施 GCM推送服务http://www.tidev.io/2013/12/20/using-gcm-for-android-push-notifications-with-acs/

希望这有帮助。

祝你好运,干杯