Quantcast
Channel: Appcelerator Developer Center Q&A Unanswered Questions 20
Viewing all articles
Browse latest Browse all 8068

Android Background Service not working properly on device

$
0
0

I want to create a notification system based on different time period, I was using Android background service. It works fine in Emulator (Currently using Genymotion). But when I want to set a duration for 2 hours, it is not giving me the notification. Probably the service stopped executing. what do I suppose to do, Is there any other alternative solution.

function setAlarmForAndroid(){
 
        var seconds = 7200;
        var alarmTimeIntent = Ti.Android.createIntent({
            className: 'org.appcelerator.titanium.TiActivity',
            packageName: 'package name here',
            flags: Titanium.Android.FLAG_ACTIVITY_CLEAR_TOP | Titanium.Android.FLAG_ACTIVITY_SINGLE_TOP 
        });
 
        var alarmTimePendingIntent = Ti.Android.createPendingIntent({
            activity: Ti.Android.currentActivity,
            intent: alarmTimeIntent,
            type: Ti.Android.PENDING_INTENT_FOR_ACTIVITY,
            flags: Titanium.Android.FLAG_CANCEL_CURRENT
        });
 
        var alarmComplete = Titanium.Android.createNotification({
            contentIntent: alarmTimePendingIntent,
            contentTitle: 'Hey Times up.',
            tickerText: 'Your task is completed',
            when: new Date()
        });
        var alarmBefor15Min = Titanium.Android.createNotification({
            contentIntent: alarmTimePendingIntent,
            contentTitle: 'You have only 15 minute left',
            tickerText: 'Only 15 Minutes left',
            when: new Date()
        });
 
        Alloy.Globals.service.start();
        Alloy.Globals.service.addEventListener('resume', function(e) {
        Titanium.API.info('Service code resumes, iteration ' + e.iteration);
         if (parseInt(e.iteration) == (seconds-900) ) {  //change the value to second   (seconds-900)
                Alloy.Globals.ap15min.play(); 
                Ti.Android.NotificationManager.notify(1, alarmBefor15Min);          
         }
         if (parseInt(e.iteration) == seconds) {  //change the value to second
                Alloy.Globals.apComplete.play(); 
                Ti.Android.NotificationManager.notify(1, alarmComplete);
                Alloy.Globals.service.stop();
 
                if(Ti.Android.isServiceRunning(Alloy.Globals.intent)){
 
                }
          }
        });
 
}

Viewing all articles
Browse latest Browse all 8068

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>