- Implement your BlockService.kt (Sample: MyBlockService.kt)
- Implement when to use block (Sample AirPlaneIntentReceiver.kt: When device into in air plane mode, call service.)
Last active
April 11, 2018 11:53
-
-
Save gbzarelli/60d5f3480833eefb125cdb6e30740421 to your computer and use it in GitHub Desktop.
Esta é uma tela de bloqueio de dispositivo, essa tela vai sempre ficar visivel e no topo após chamar o 'block', ela só vai parar de ser visivel apos o metodo 'release' ser chamado!
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
<?xml version="1.0" encoding="utf-8"?> | |
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" | |
xmlns:app="http://schemas.android.com/apk/res-auto" | |
xmlns:tools="http://schemas.android.com/tools" | |
android:layout_width="match_parent" | |
android:layout_height="match_parent" | |
android:background="@android:color/black" | |
android:padding="15dp"> | |
<TextView | |
android:id="@+id/text" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:drawableTop="@drawable/ninja" | |
android:gravity="center" | |
android:text="@string/block_default_msg" | |
android:textColor="@android:color/holo_red_dark" | |
android:textSize="30sp" | |
android:textStyle="bold" | |
app:layout_constraintBottom_toBottomOf="parent" | |
app:layout_constraintEnd_toEndOf="parent" | |
app:layout_constraintStart_toStartOf="parent" | |
app:layout_constraintTop_toTopOf="parent" /> | |
</android.support.constraint.ConstraintLayout> |
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
import android.content.BroadcastReceiver | |
import android.content.Context | |
import android.content.Intent | |
/** | |
* Created by gbzarelli on 2/1/18. | |
*/ | |
class AirPlaneIntentReceiver : BroadcastReceiver() { | |
override fun onReceive(context: Context, intent: Intent) { | |
val isAirplaneModeOn = intent.getBooleanExtra("state", false) | |
val itAirPlaneBlockService = Intent(context, MyBlockService::class.java) | |
itAirPlaneBlockService.putExtra(MyBlockService.IT_MESSAGE_BLOCK, context.getString(R.string.block_airplane)) | |
if (isAirplaneModeOn) { | |
context.startService(itAirPlaneBlockService) | |
} else { | |
context.stopService(itAirPlaneBlockService) | |
} | |
} | |
} |
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
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 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, message: String) { | |
context.startActivity(getIntent(context, message)) | |
} | |
private fun getIntent(context: Context, message: 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, message) | |
return intent | |
} | |
fun release(context: Context) { | |
LocalBroadcastManager.getInstance(context).sendBroadcast(Intent(ACTION_RELEASE)) | |
} | |
} | |
private var release = false | |
private var localBroadcast: LocalBroadcast? = null | |
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()) | |
} | |
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() + 1_000, mPendingIntent) | |
super.onStop() | |
} | |
override fun onBackPressed() { | |
} | |
inner class LocalBroadcast : BroadcastReceiver() { | |
override fun onReceive(context: Context?, intent: Intent) { | |
if (ACTION_RELEASE == intent.action) { | |
onRelease() | |
} | |
} | |
} | |
} |
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
import android.app.Service | |
import android.content.Intent | |
import android.os.IBinder | |
/** | |
* Created by gbzarelli on 2/19/18. | |
*/ | |
abstract class BlockService : Service(), Runnable { | |
override fun run() { | |
do { | |
BlockActivity.block(this, getMessageBlock()) | |
Thread.sleep(getTimeMillisUpdate()) | |
} while (checkLock()) | |
stopSelf() | |
} | |
abstract fun getMessageBlock(): String | |
abstract fun getTimeMillisUpdate(): Long | |
abstract fun checkLock(): Boolean | |
override fun onBind(intent: Intent): IBinder? { | |
return null | |
} | |
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int { | |
if (1 == startId) { | |
Thread(this).start() | |
} | |
return super.onStartCommand(intent, flags, startId) | |
} | |
override fun onDestroy() { | |
super.onDestroy() | |
BlockActivity.release(this) | |
} |
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
import android.content.Intent | |
import android.provider.Settings | |
/** | |
* Created by gbzarelli on 2/19/18. | |
*/ | |
class MyBlockService : BlockService() { | |
companion object { | |
const val IT_MESSAGE_BLOCK = "IT_MESSAGE_BLOCK" | |
} | |
private var message: String? = null | |
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int { | |
if (null != intent) { | |
message = intent.getStringExtra(IT_MESSAGE_BLOCK) | |
} | |
return super.onStartCommand(intent, flags, startId) | |
} | |
override fun getMessageBlock(): String = message ?: "" | |
override fun getTimeMillisUpdate(): Long = 15_000 | |
override fun checkLock(): Boolean = Settings.Global.getInt(contentResolver, Settings.Global.AIRPLANE_MODE_ON, 0) != 0 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment