Last active
October 24, 2018 09:47
-
-
Save soulduse/c9d08f62f8210685e87cdfee23aa7ed6 to your computer and use it in GitHub Desktop.
When I copied some text in Android, How to get copied text from listener.
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=".ClipboardService"/> | |
</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 ClipboardService: Service() { | |
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int { | |
val mCm = getSystemService(Context.CLIPBOARD_SERVICE) as ClipboardManager | |
mCm.addPrimaryClipChangedListener { | |
val newClip: String? = mCm.primaryClip?.getItemAt(0)?.text.toString() | |
toast(newClip ?: "No data") | |
} | |
return START_STICKY | |
} | |
override fun onBind(p0: Intent?): IBinder? = null | |
} |
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
inline fun Context.toast(text: String) { | |
Toast.makeText(this, text, Toast.LENGTH_SHORT).show() | |
} |
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) | |
setContentView(R.layout.activity_main) | |
startService(Intent(this, ClipboardService::class.java)) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
https://developer.android.com/guide/topics/text/copy-paste