【问题标题】:Titanium (Android) check for running ServicesTitanium (Android) 检查正在运行的服务
【发布时间】:2013-03-08 17:46:51
【问题描述】:

我正在尝试实现一个易于使用的系统来处理 android 的状态栏通知,我正在考虑以下问题:

  • 创建一个数据库,用于存储显示时间和内容
  • 使用 API 提供的“间隔”服务创建在后台运行的服务
  • 在该服务中检查是否需要根据数据库显示任何通知,然后显示它。

唯一的问题是,我无法检测到是否需要启动服务。我尝试了这些东西,但到目前为止它们都没有奏效:

1.)如果服务已在本地存储上启动,则保存:

// Do this on application startup
var isRunning = Ti.App.Properties.getBool("service_running", false);    
if(!isRunning)
{
    var service = Titanium.Android.createService(...);

    service.addEventListener('start', function() 
    {
        Ti.App.Properties.setBool("service_running", true);
    });

    service.addEventListener('stop', function() 
    {
        Ti.App.Properties.setBool("service_running", false);
    });

    service.start();
}

这显然是行不通的,因为android系统原生的onStoponDestroy事件不会被调度,如果Service没有异常终止(比如用户强制停止应用程序),所以停止事件也不会被解雇。

2.) 尝试通过Titanium.Android.getCurrentService() 访问任何活动服务,但我收到一条错误消息,提示 Titanium.Android 没有名为 getCurrentService() 的方法。这很奇怪,因为 IDE 代码完成为我提供了这种方法。

3.) 使用 Intent 清除之前运行的 Service

var intent = Titanium.Android.createServiceIntent
(
    {
       url : 'notification/NotificationService.js' 
    }
);

intent.putExtra('interval', 1000 * 60);

//Stop if needed        
Titanium.Android.stopService(intent);

//Then start it
Titanium.Android.startService(intent);

但似乎我需要相同的 Intent 实例,启动服务以停止它,因为在应用程序启动时执行此操作,然后退出并重新启动它会导致多个服务运行。

此时我已经没有想法了,关于如何检查正在运行的服务。如果您知道任何方法可以做到这一点,请告诉我!感谢您的任何提示!

编辑

这里是给了我尝试上述方法的想法的源材料(也许只是我用错了):

  1. 本地存储:Titanium.App.Properties
  2. 访问运行服务的方法:Titanium.Android.getCurrentService
  3. 使用Intent停止服务的方法:Titanium.Android.stopService

以及我编写的 NotificationHandler“类”和 NotificationService.js 的完整源代码及其用法:link

【问题讨论】:

  • 您能解决这个问题吗?我正在努力解决一个非常相似的问题,如果不是同一个的话。 getCurrentService 对我也没有任何作用,我无法将意图/服务保存到应用程序属性等......非常烦人且令人困惑。更糟糕的是,我发现该服务可以被操作系统杀死,这是一个巨大的痛苦(而且它永远不会重新启动它)。
  • 我有关于这个问题的建议..我开始输入但它太长了所以我发布为答案..请检查

标签: android titanium titanium-mobile


【解决方案1】:

使用Bencoding AlarmManager,它将提供您安排警报通知所需的一切:https://github.com/benbahrenburg/benCoding.AlarmManager

这个模块提供你需要的东西。这真的很简单 - 只需在调度 Notification or Service 时将重复设置为 daily

完整功能代码请参考https://gist.github.com/itsamiths/6248106

我正在检查服务是否启动然后显示每日通知或启动服务然后显示每日通知

var isRunning = Ti.App.Properties.getBool("service_running", false);//get service running bool status
if (isRunning) {
    Ti.API.info('service is running');
} else {
    Ti.API.info('service is not running');
    alarmManager.addAlarmService({
        service : 'com.mkamithkumar.whatstoday.DailyEventNotificatoinService',
        hour : "08",
        repeat : 'daily'
    });
}

【讨论】:

    【解决方案2】:

    我来晚了一年,但也许这可以帮助将来的其他人。

    我们有相同的想法:永远运行服务并在每个周期进行检查(我必须检查 20 次不同的通信)。

    我遇到了同样的问题:如何检测服务正在运行,如何不再运行以不重复检查。

    为了解决这个问题,我所做的是获取每个周期的当前时间并将其保存到存储中。

    然后,在启动新服务之前,我检查最后一次执行是否及时:如果为真,则服务已停止,否则正在运行。

    不是很优雅,但我发现这是避免用户杀死应用程序(和服务)问题的唯一方法。

    这是我的服务“启动器”代码。就我而言,我测试 30 秒远:

    exports.createAndroidServiceForNotifications = function(seconds) {
        var moment = require('alloy/moment');
        var diffSeconds = moment().diff(Ti.App.Properties.getString('serviceLastRun', new Date().getTime() - 60000), 'second');
    
        if (diffSeconds > 30) {
            var now = new Date().getTime();
            var delta = new Date(now + (seconds * 1000));
            var deltaMS = delta - now;
    
            var intent = Ti.Android.createServiceIntent({
                url : 'notificationsService.js'
            });
            intent.putExtra('interval', deltaMS);
            Ti.Android.startService(intent);    
        }
    };
    

    【讨论】:

      猜你喜欢
      • 2011-05-25
      • 1970-01-01
      • 1970-01-01
      • 2011-07-28
      • 2013-07-09
      • 1970-01-01
      • 2013-04-04
      • 2021-08-15
      • 2015-06-26
      相关资源
      最近更新 更多