Last active
January 17, 2019 12:42
-
-
Save MotiurRahman/a2100bf126f72381e088505e7b3f303a to your computer and use it in GitHub Desktop.
Android Background 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
// https://docs.appcelerator.com/platform/latest/#!/guide/Android_Notifications | |
var win = Ti.UI.createWindow({ | |
backgroundColor: 'black', | |
layout: 'vertical' | |
}); | |
var label = Ti.UI.createLabel({ | |
text: 'A service is now running: you will see a notification every 10 seconds\nEven after closing your apps', | |
width: Ti.UI.SIZE, | |
top: 10, | |
color: 'white' | |
}); | |
win.add(label); | |
// create the background service | |
var SECONDS = 10; | |
// every 10 seconds | |
var intent = Titanium.Android.createServiceIntent({ | |
url: 'pushService.js' | |
}); | |
intent.putExtra('interval', SECONDS * 1000); | |
// Needs to be milliseconds | |
if (!Ti.Android.isServiceRunning(intent)) { | |
Titanium.Android.startService(intent); | |
} | |
var btnStop = Ti.UI.createButton({ | |
title: "Stop service", | |
top: 50 | |
}); | |
win.add(btnStop); | |
btnStop.addEventListener("click", function() { | |
Ti.Android.stopService(intent); | |
}); | |
var btnStart = Ti.UI.createButton({ | |
title: "Start service", | |
top: 10 | |
}); | |
btnStart.addEventListener("click", function() { | |
Titanium.Android.startService(intent); | |
}); | |
win.addEventListener("androidback", function(e) { | |
// If the back key was press, cancel it and go to the home-screen instead. | |
var intent = Ti.Android.createIntent({ | |
action: Ti.Android.ACTION_MAIN, | |
}); | |
intent.addCategory(Ti.Android.CATEGORY_HOME); | |
intent.setFlags(Ti.Android.FLAG_ACTIVITY_NEW_TASK); | |
Ti.Android.currentActivity.startActivity(intent); | |
}); | |
win.add(btnStop); | |
win.add(btnStart); | |
win.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
function getAppResumeIntent() { | |
var intent = Ti.App.Android.launchIntent; | |
if (!intent) { | |
intent = Ti.Android.createIntent({}); | |
} | |
return intent; | |
} | |
var pending = Titanium.Android.createPendingIntent({ | |
intent: getAppResumeIntent(), | |
flags: Titanium.Android.FLAG_UPDATE_CURRENT | |
}); | |
// create a random id | |
var nId = parseInt(10000 * Math.random()); | |
// Create the notification | |
var notification = Titanium.Android.createNotification({ | |
contentTitle: 'Notification #' + nId, | |
contentText : 'Click to return to the application.', | |
icon: Ti.App.Android.R.drawable.appicon, | |
contentIntent: pending, | |
// sound: Ti.Filesystem.getResRawDirectory() + 'notify.mp3', | |
}); | |
// Send the notification. | |
Titanium.Android.NotificationManager.notify(nId, notification); |
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
<android xmlns:android="http://schemas.android.com/apk/res/android"> | |
<manifest android:versionCode="1" xmlns:android="http://schemas.android.com/apk/res/android"> | |
</manifest> | |
<services> | |
<service type="interval" url="pushService.js" /> | |
</services> | |
</android> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment