Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import android.app.Activity
- import android.app.AlarmManager
- import android.app.PendingIntent
- import android.content.BroadcastReceiver
- import android.content.Context
- import android.content.Intent
- import android.content.IntentFilter
- import android.os.Bundle
- import android.support.v4.content.LocalBroadcastManager
- import br.com.grupocriar.launchercriar.R
- import kotlinx.android.synthetic.main.activity_block.*
- class BlockActivity : Activity() {
- companion object {
- const val IT_STRING_BLOCK = "IT_STRING_BLOCK"
- const val ACTION_RELEASE = "ACTION_RELEASE"
- fun block(context: Context, mensage: String) {
- context.startActivity(getIntent(context, mensage))
- }
- private fun getIntent(context: Context, mensage: String): Intent {
- val intent = Intent(context, BlockActivity::class.java)
- intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK or
- Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS or
- Intent.FLAG_ACTIVITY_SINGLE_TOP or
- Intent.FLAG_ACTIVITY_REORDER_TO_FRONT
- intent.putExtra(IT_STRING_BLOCK, mensage)
- return intent
- }
- fun release(context: Context) {
- LocalBroadcastManager.getInstance(context).sendBroadcast(Intent(ACTION_RELEASE))
- }
- }
- private var release = false
- override fun onCreate(savedInstanceState: Bundle?) {
- super.onCreate(savedInstanceState)
- setContentView(R.layout.activity_block)
- if (null == savedInstanceState) {
- if (null != intent) {
- val stringExtra = intent.getStringExtra(IT_STRING_BLOCK)
- if (stringExtra != null && stringExtra.isNotEmpty()) {
- text.text = stringExtra
- }
- }
- } else {
- text.text = savedInstanceState.getString(IT_STRING_BLOCK)
- }
- }
- override fun onSaveInstanceState(outState: Bundle) {
- super.onSaveInstanceState(outState)
- outState.putString(IT_STRING_BLOCK, text.text.toString())
- }
- private var localBroadcast: LocalBroadcast? = null
- override fun onResume() {
- super.onResume()
- if (null == localBroadcast) {
- localBroadcast = LocalBroadcast()
- }
- LocalBroadcastManager.getInstance(this).registerReceiver(localBroadcast!!, IntentFilter(ACTION_RELEASE))
- }
- override fun onPause() {
- super.onPause()
- if (null != localBroadcast)
- LocalBroadcastManager.getInstance(this).unregisterReceiver(localBroadcast!!)
- }
- fun onRelease() {
- release = true
- finish()
- }
- override fun onStop() {
- if (release) {
- super.onStop()
- return
- }
- val mPendingIntent = PendingIntent.getActivity(this, 100,
- getIntent(this, text.text.toString()),
- PendingIntent.FLAG_UPDATE_CURRENT)
- val mgr = getSystemService(Context.ALARM_SERVICE) as AlarmManager
- mgr.set(AlarmManager.RTC, System.currentTimeMillis() + 200, mPendingIntent)
- super.onStop()
- finish()
- }
- override fun onBackPressed() {
- }
- inner class LocalBroadcast : BroadcastReceiver() {
- override fun onReceive(context: Context?, intent: Intent) {
- if (ACTION_RELEASE == intent.action) {
- onRelease()
- }
- }
- }
- }
Add Comment
Please, Sign In to add comment