Last active
October 22, 2018 05:49
-
-
Save soulduse/5f9cc0ec1e288426594b8fbe3ace1c4d to your computer and use it in GitHub Desktop.
Auto launch apps randomly.
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
<application .. > | |
... | |
<service android:name=".StartAppService"/> | |
</application> |
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
class MainActivity : AppCompatActivity() { | |
override fun onCreate(savedInstanceState: Bundle?) { | |
super.onCreate(savedInstanceState) | |
startService(Intent(this, StartAppService::class.java)) | |
} | |
} |
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
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" ... ) | |
} | |
} |
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
/** | |
* 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