Last active
January 14, 2019 12:54
-
-
Save aminulaust/d351cbe205e0b5196cecf8130756a776 to your computer and use it in GitHub Desktop.
Foreground Service
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function monitorLocation() { | |
function start() { | |
Ti.Geolocation.accuracy = Ti.Geolocation.ACCURACY_HIGH; | |
Ti.Geolocation.addEventListener("location", function(e) { | |
Ti.Media.vibrate(); | |
Ti.API.info("@@@ Location Received: " + JSON.stringify(e.coords)); | |
}); | |
} | |
var hasPermission = Ti.Geolocation.hasLocationPermissions(Ti.Geolocation.AUTHORIZATION_WHEN_IN_USE); | |
if (!hasPermission) { | |
Ti.Geolocation.requestLocationPermissions(Ti.Geolocation.AUTHORIZATION_WHEN_IN_USE, function(e) { | |
if (e.success) { | |
start(); | |
} | |
}); | |
} else { | |
start(); | |
} | |
} | |
var SECONDS = 20; | |
// Start the service. | |
var intent = Ti.Android.createServiceIntent({ | |
url: "Service.js", | |
}); | |
intent.putExtra('interval', SECONDS * 1000); | |
Ti.Android.startService(intent); | |
// Display the main window. | |
var window = Ti.UI.createWindow(); | |
window.addEventListener("open", function(e) { | |
monitorLocation(); | |
}); | |
window.addEventListener("close", function(e) { | |
// Stop the service on app exit. | |
//Ti.Android.stopService(intent); | |
}); | |
window.open(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Ti.API.info("@@@ Service started."); | |
// Gets an intent used to resume the app if running in the background. | |
function getAppResumeIntent() { | |
var intent = Ti.App.Android.launchIntent; | |
if (!intent) { | |
intent = Ti.Android.createIntent({}); | |
} | |
return intent; | |
} | |
// Put this service into the foreground state. | |
var notificationId = null; | |
Ti.Android.currentService.foregroundNotify(notificationId, Ti.Android.createNotification({ | |
contentTitle: "Foreground Service", | |
contentText: "Content Text", | |
contentIntent: Ti.Android.createPendingIntent({ | |
// Using the launch intent will resume the app when tapped. | |
intent: getAppResumeIntent(), | |
}), | |
})); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="UTF-8"?> | |
<ti:app xmlns:ti="http://ti.appcelerator.org"> | |
<android xmlns:android="http://schemas.android.com/apk/res/android"> | |
<services> | |
<service type="interval" url="Service.js"/> | |
</services> | |
<manifest> | |
<uses-permission android:name="android.permission.FOREGROUND_SERVICE"/> | |
</manifest> | |
</android> | |
<modules/> | |
</ti:app> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment