Hi,
I need a Background Service in Android. This Service read a url and then creates a notification depending on the response. When app is in foreground runs ok, but when app is in background not run. I read the "Q & A" of Titanium and then I try to use bencoding.AlarmManager module. But the problem is the same. The app run some minutes in background and then stop.
Any suggestions will be appreciated.
My code is:
index.js:
//*********************************** // SERVICIOS BACKGROUND var SECONDS = 30; // every 30 seconds var lointento = Titanium.Android.createServiceIntent({ url: 'MiBackground.js' }); lointento.putExtra('interval', SECONDS * 1000); // Needs to be milliseconds if (!Ti.Android.isServiceRunning(lointento)) { Ti.Android.startService(lointento); Ti.API.info('Service lo intento ***********.'); } else { Ti.API.info('Service is already running.************'); } Ti.API.info('BACKGROUND termino lo intento ***********.' + Ti.Android.isServiceRunning(lointento)); // FIN SERVICIOS BACKGROUND //*********************************** var alarmManager = require('bencoding.alarmmanager').createAlarmManager(); var Now = new Date(); alarmManager.addAlarmService({ //The full name for the service to be called. Find this in your AndroidManifest.xml Titanium creates service:'com.miapp.thermostatmulti.MiBackground', interval: 10000, //You can use the words hourly,daily,weekly,monthly,yearly or you can provide milliseconds. second: Now.getSeconds() });
Tiapp.xml:
<manifest android:versionCode="2" android:versionName="1.01">> <uses-permission android:name="android.permission.WAKE_LOCK" /> <application android:debuggable="false" android:icon="@drawable/appicon" android:label="Thermostat Multi" android:name="ThermostatMultiApplication" android:theme="@style/Theme.NoActionBar"> <activity android:configChanges="keyboardHidden|orientation|screenSize" android:label="@string/app_name" android:name=".ThermostatMultiActivity" android:screenOrientation="portrait" android:theme="@style/Theme.Titanium"> <intent-filter> <action android:name="android.intent.action.MAIN"/> <category android:name="android.intent.category.LAUNCHER"/> </intent-filter> </activity> <activity android:configChanges="keyboardHidden|orientation|screenSize" android:name="org.appcelerator.titanium.TiActivity" android:screenOrientation="portrait"/> <activity android:configChanges="keyboardHidden|orientation|screenSize" android:name="org.appcelerator.titanium.TiTranslucentActivity" android:screenOrientation="portrait" android:theme="@style/Theme.AppCompat.Translucent"/> <activity android:configChanges="orientation|screenSize" android:name="ti.modules.titanium.ui.android.TiPreferencesActivity" android:screenOrientation="portrait"/> <service android:exported="false" android:name="com.appcelerator.analytics.APSAnalyticsService"/> <receiver android:name="bencoding.alarmmanager.AlarmNotificationListener"/> <receiver android:name="bencoding.alarmmanager.AlarmServiceListener"/> </application> </manifest> <services> <service type="interval" url="MiBackground.js"/> </services>
MiBackground.js:
var Recibido=""; var ArrayRecibido=""; var url = "http://www.miserver.com/thermic/"; var clientelee = Ti.Network.createHTTPClient({ // function called when the response data is available onload : function(e) { Recibido=this.responseText; ArrayRecibido=Recibido.split("/"); if (ArrayRecibido[0]=="NOSERIE"){ Ti.API.info("**** no existe ****"); } else{ if (ArrayRecibido[3]>0){ var NotiIntrusion = Titanium.Android.createNotification({ contentTitle: 'MY HOME', contentText : 'INTRUSION: ' + NombreLeido, contentIntent: Ti.Android.createPendingIntent({intent: Ti.Android.createIntent({})}), icon: Ti.App.Android.R.drawable.ladron, when: new Date() }); Ti.Android.NotificationManager.notify(1, NotiIntrusion); } } }, // function called when an error occurs, including a timeout onerror : function(e) { Ti.API.debug(e.error); }, timeout : 5000 // in milliseconds }); var TablaSerieTermostatos=[]; var TablaPinTermostatos=[]; function CargoTermostatos(){ var db = Ti.Database.open('MiBD'); var rows = db.execute('SELECT * FROM Termostatos ORDER BY Termostatos.Nombre'); NRegistros=ComprueboRegBD(); var RowsTabla=[]; var i; for (i=0;i<NRegistros;i++){ TablaSerieTermostatos[i]=rows.field(0); TablaPinTermostatos[i]= rows.field(1); TablaNombreTermostatos[i]= rows.field(2); } db.close(); } CargoTermostatos(); var NombreLeido; for (i=0;i<NRegistros;i++){ TablaSerieTermostatos[i]=rows.field(0); TablaPinTermostatos[i]= rows.field(1); url = "http://www.miserver.com/thermic/read/"+TablaSerieTermostatos[i]+"/?pin="+TablaPinTermostatos[i]; // Prepare the connection. clientelee.open("GET", url); // Send the request. clientelee.send(); }