简体   繁体   中英

Titanium (Android) check for running Services

I'm trying to implement a convenient-to-use system for handling status bar notifications for android, and i was thinking about the following:

  • Create a database, where i store when and what to show
  • Create a service what runs in the background using the 'interval' Service, what the API provides
  • In that service check if any notification needs to be shown according to the database, then show it.

The only problem is, that, i cannot detect, if i need to start the service or not. I tried these things, but none of them worked well so far:

1.) Save if the service was already started on the local storage:

// 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();
}

This obviously won't work, because the android systems native onStop and onDestroy events will not be dispatched, if the Service doesn't terminates unusually (like the user force stops the app), so the stop event also won't be fired.

2.) Try to access any active service via Titanium.Android.getCurrentService() , but i got an error saying Titanium.Android has no method called getCurrentService(). This is pretty strange, because the IDEs code completion offered me this method.

3.) Use an Intent to clear the previously running 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);

But it seems like i need to have the same instance of Intent, that started the service to stop it, because doing this on application startup, then exiting and restaring it results in multiple Services to run.

At this point i ran out of ideas, on how to check for running services. Please if you know about any way to do this, let me know! Thanks for any hints!

EDIT

Here are the source materials which gave me the idea to try the above methods (maybe only i use them incorrectly):

  1. The local storage: Titanium.App.Properties
  2. The method for accessing running services: Titanium.Android.getCurrentService
  3. The method for stoping a service with an Intent: Titanium.Android.stopService

And the full source for the NotificationHandler "class" and NotificationService.js that I wrote, and their usage: link

Use Bencoding AlarmManager and it will provide all you need to schedule an alarm notification : https://github.com/benbahrenburg/benCoding.AlarmManager

This module provides what you need. It's really easy - just set repeat to daily when sheduling a Notification or Service .

Refer https://gist.github.com/itsamiths/6248106 for fully functional code

I am checking if the service is started then show daily notification or else start service and then show daily notification

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

I come one year late, but maybe this can help others in the future.

We had the same idea: run the service forever and do the checks on every cycle (I must check 20 different communications).

And I had the same problem: how to detect that the service is running, to don't run again to don't duplicate the checks.

To solve that problem, what I did is get the current time on every cycle and save it to store.

Then, before launch a new service, I check if the last execution was to far in time: if true, then the service was stopped, else is running.

Not very elegant, but was the only way I found to avoid the problem of the user killing the app (and the service).

This is my code for the "launcher" of the service. In my case, I test 30 seconds far away:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM