kamran hatami
kamran hatami

Reputation: 122

is it possible to get clipboard manager via intent-filter and broadcastreceiver?

is it possible to get copied text via intent-filters and broadcast receiver? I want to develop a dictionary application and when user copies text wherever in his phone I want to show the meaning of text. currently I have tried using a Base Activity to register listener but i it does not meet my need.

 mClipboard = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE);

        listener = new ClipboardManager.OnPrimaryClipChangedListener() {
            public void onPrimaryClipChanged() {
                performClipboardCheck();
            }
        };
        mClipboard.addPrimaryClipChangedListener(listener);

Upvotes: 3

Views: 1343

Answers (3)

Shayan D
Shayan D

Reputation: 619

you can write a service to check clipboard and attach receiver to ClipboardManager there. so the service class should be like:

class ClipBoardService : Service(), ClipboardManager.OnPrimaryClipChangedListener {
override fun onPrimaryClipChanged() {
    val manager = getSystemService(Context.CLIPBOARD_SERVICE) as ClipboardManager
    if (manager.primaryClip.itemCount > 0) {
        val clip = manager.primaryClip.getItemAt(0).text.toString()
        toast(clip)
    }
}


override fun onBind(intent: Intent): IBinder {
    return Binder()
}

override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
    initReceiver()
    return START_STICKY
}

private fun initReceiver() {
    val manager = getSystemService(Context.CLIPBOARD_SERVICE) as ClipboardManager
    manager.removePrimaryClipChangedListener(this)
    manager.addPrimaryClipChangedListener(this)
}
}

and you can create a broadcast receiver with you own action and register it on your activity's onStart and unregister it on activity's onStop and then in your service using sendBroadcast method send intent to your activity

don't forget to declare service in your manifest file

<service
        android:name=".clipboard.ClipBoardService"
        android:enabled="true"
        android:exported="true" />

hope it was helpful.

Upvotes: 1

Hamza
Hamza

Reputation: 2045

This how i did it,just open your app first then close it but you can also write service if want to run continues

for Java

final ClipboardManager clipboard = (ClipboardManager) this.getSystemService(Context.CLIPBOARD_SERVICE);
            clipboard.addPrimaryClipChangedListener( new ClipboardManager.OnPrimaryClipChangedListener() {
                public void onPrimaryClipChanged() {
                    String a = clipboard.getText().toString();
                    Toast.makeText(getBaseContext(),"Copy:\n"+a,Toast.LENGTH_LONG).show();
                }
            }); 

for kotlin

  clipboard= this.getSystemService(Context.CLIPBOARD_SERVICE) as ClipboardManager
        clipboard.addPrimaryClipChangedListener (object : ClipboardManager.OnPrimaryClipChangedListener{
            override fun onPrimaryClipChanged() {
              Log.d(TAG,"Copy:\n= "+clipboard.primaryClip.toString())
            }

        })

Upvotes: 1

CommonsWare
CommonsWare

Reputation: 1007296

is it possible to get copied text via intent-filters and broadcast receiver?

No.

Upvotes: 0

Related Questions