Currently making a URL shortener, I wanted to add functionality to show a notification when a URL was copied even when the app is in the background or closed.
Using a service class can be a good idea but it has some disadvantages:
Some devices like MI and OnePlus don't allow running services for more than 30 seconds.
We need to always show a notification otherwise the service doesn't run for long
etc...
I currently used this in my app and added some copyable content on the app and it worked:
class ClipboardListener implements ClipboardManager.OnPrimaryClipChangedListener
{
public void onPrimaryClipChanged()
{
Log.i("currenltyCopiedText - ", getPrimaryClip() + "");
}
}
ClipboardManager clipBoard = (ClipboardManager)getSystemService(CLIPBOARD_SERVICE);
clipBoard.addPrimaryClipChangedListener( new ClipboardListener() );
But, this works only IN the app. Not when it is closed. How can I achieve that?