4

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

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);

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

4

1 回答 1

0

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

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

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

为什么这样做?

因为应用程序不再服务了。该应用程序被强制从后台删除,因此操作系统停止在后台运行的所有服务以清除内存。

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

您可以寻找的选项

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

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

希望这可以帮助。

祝你好运,干杯

于 2017-03-21T12:59:12.960 回答