Skip to content

Instantly share code, notes, and snippets.

@soulduse
Last active October 22, 2018 05:49
Show Gist options
  • Save soulduse/5f9cc0ec1e288426594b8fbe3ace1c4d to your computer and use it in GitHub Desktop.
Save soulduse/5f9cc0ec1e288426594b8fbe3ace1c4d to your computer and use it in GitHub Desktop.
Auto launch apps randomly.
<application .. >
...
<service android:name=".StartAppService"/>
</application>
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
startService(Intent(this, StartAppService::class.java))
}
}
class StartAppService: Service() {
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
val timer = Timer()
timer.scheduleAtFixedRate(ScheduleJob{
startApp()
}, 1000, 1000 * 60)// SEC or MINUTES
return Service.START_STICKY
}
override fun onBind(intent: Intent?): IBinder? = null
private fun startApp() {
val intent = packageManager.getLaunchIntentForPackage(APPS.random())
startActivity(intent)
}
class ScheduleJob(val runApp: () -> Unit): TimerTask() {
override fun run() {
runApp()
}
}
companion object {
val APPS = listOf("A app package name", "B app package name", "C app package name" ... )
}
}
/**
* Usage
*
* val APPS = listOf("A", "B", "C")
*
* APPS.random()
*/
fun <T> List<T>.random() : T {
val random = Random().nextInt(size)
return get(random)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment