Guest User

Untitled

a guest
Feb 21st, 2018
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.34 KB | None | 0 0
  1. import android.app.Activity
  2. import android.app.AlarmManager
  3. import android.app.PendingIntent
  4. import android.content.BroadcastReceiver
  5. import android.content.Context
  6. import android.content.Intent
  7. import android.content.IntentFilter
  8. import android.os.Bundle
  9. import android.support.v4.content.LocalBroadcastManager
  10. import br.com.grupocriar.launchercriar.R
  11. import kotlinx.android.synthetic.main.activity_block.*
  12.  
  13. class BlockActivity : Activity() {
  14.  
  15. companion object {
  16. const val IT_STRING_BLOCK = "IT_STRING_BLOCK"
  17. const val ACTION_RELEASE = "ACTION_RELEASE"
  18.  
  19. fun block(context: Context, mensage: String) {
  20. context.startActivity(getIntent(context, mensage))
  21. }
  22.  
  23. private fun getIntent(context: Context, mensage: String): Intent {
  24. val intent = Intent(context, BlockActivity::class.java)
  25. intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK or
  26. Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS or
  27. Intent.FLAG_ACTIVITY_SINGLE_TOP or
  28. Intent.FLAG_ACTIVITY_REORDER_TO_FRONT
  29. intent.putExtra(IT_STRING_BLOCK, mensage)
  30. return intent
  31. }
  32.  
  33. fun release(context: Context) {
  34. LocalBroadcastManager.getInstance(context).sendBroadcast(Intent(ACTION_RELEASE))
  35. }
  36. }
  37.  
  38. private var release = false
  39.  
  40. override fun onCreate(savedInstanceState: Bundle?) {
  41. super.onCreate(savedInstanceState)
  42. setContentView(R.layout.activity_block)
  43. if (null == savedInstanceState) {
  44. if (null != intent) {
  45. val stringExtra = intent.getStringExtra(IT_STRING_BLOCK)
  46. if (stringExtra != null && stringExtra.isNotEmpty()) {
  47. text.text = stringExtra
  48. }
  49. }
  50. } else {
  51. text.text = savedInstanceState.getString(IT_STRING_BLOCK)
  52. }
  53. }
  54.  
  55. override fun onSaveInstanceState(outState: Bundle) {
  56. super.onSaveInstanceState(outState)
  57. outState.putString(IT_STRING_BLOCK, text.text.toString())
  58. }
  59.  
  60.  
  61. private var localBroadcast: LocalBroadcast? = null
  62.  
  63. override fun onResume() {
  64. super.onResume()
  65. if (null == localBroadcast) {
  66. localBroadcast = LocalBroadcast()
  67. }
  68. LocalBroadcastManager.getInstance(this).registerReceiver(localBroadcast!!, IntentFilter(ACTION_RELEASE))
  69. }
  70.  
  71. override fun onPause() {
  72. super.onPause()
  73. if (null != localBroadcast)
  74. LocalBroadcastManager.getInstance(this).unregisterReceiver(localBroadcast!!)
  75. }
  76.  
  77. fun onRelease() {
  78. release = true
  79. finish()
  80. }
  81.  
  82. override fun onStop() {
  83. if (release) {
  84. super.onStop()
  85. return
  86. }
  87.  
  88. val mPendingIntent = PendingIntent.getActivity(this, 100,
  89. getIntent(this, text.text.toString()),
  90. PendingIntent.FLAG_UPDATE_CURRENT)
  91. val mgr = getSystemService(Context.ALARM_SERVICE) as AlarmManager
  92. mgr.set(AlarmManager.RTC, System.currentTimeMillis() + 200, mPendingIntent)
  93. super.onStop()
  94. finish()
  95. }
  96.  
  97. override fun onBackPressed() {
  98. }
  99.  
  100. inner class LocalBroadcast : BroadcastReceiver() {
  101. override fun onReceive(context: Context?, intent: Intent) {
  102. if (ACTION_RELEASE == intent.action) {
  103. onRelease()
  104. }
  105. }
  106. }
  107.  
  108. }
Add Comment
Please, Sign In to add comment