how Android listens for pasteboard content
Recent project to do the monitoring of the Paste Plate window function small remember. In fact, the idea is very simple, write a service running in the background, through the Clipboardmanager service to monitor the paste board. Nonsense Shao said code:
~~~java
public class Pastecopyservice extends Service {
Clipboardmanager Clipboardmanager;
Private String Tag = "Pastecopy";
Private WindowManager WM;
Private Window Mwindow;
@Override public void OnCreate () {super.oncreate ();
BABYTREELOG.E (Tag, "onCreate");
Clipboardmanager = (Clipboardmanager) getsystemservice (Context.clipboard_service);
WM = (WindowManager) getsystemservice (Context.window_service);
Final Windowmanager.layoutparams params = new Windowmanager.layoutparams ();
Params.type = WindowManager.LayoutParams.TYPE_TOAST;
Params.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
Params.width = Timeapplication.screenwidth;
params.gravity = Gravity.top;
Params.height = 200;
params.x = 0;
Params.y = 0;
Final Layoutinflater Minflater = Layoutinflater.from (this);
Final View view = Minflater.inflate (R.layout.window_paste_copy,null); View.setonclicklistener (New View.onclicklistener () {@Override public void OnClick (View v) {i
F (Avoidloginutil.islogin (Pastecopyservice.this)) { Intent Intent = new Intent (pastecopyservice.this, Mainactivity.class);
Intent.setflags (Intent.flag_activity_clear_top);
StartActivity (Intent);
} wm.removeview (view);
}
});
Clipboardmanager.addprimaryclipchangedlistener (New Clipboardmanager.onprimaryclipchangedlistener () {@Override
public void onprimaryclipchanged () {Wm.addview (view,params); View.post (New Runnable () {@Override public void Run () {}})
;
Toast.maketext (pastecopyservice.this, "test", Toast.length_short). Show ();
}
});
@Override public void OnDestroy () {BABYTREELOG.E (Tag, "OnDestroy");
Super.ondestroy ();
@Override public int Onstartcommand (Intent Intent, int flags, int startid) {babytreelog.e (Tag, "Onstartcommand");
Return Super.onstartcommand (Intent, flags, Startid); @Nullable @Override Public IbindeR Onbind (Intent Intent) {return null;}
}
~~~
To play the window, you have to add permissions to the manifest.
~xml
~
These are not code implementations in real projects, lots of UI, animations, and business-related code.
In fact, there is no difficulty, just a thought. The Clipboard action can be heard through the Clipboardmanager Onprimaryclipchangedlistener. If the Clipboard has new paste content, you can perform the contents of the onprimaryclipchanged.
Here are some details, WindowManager.LayoutParams.TYPE_TOAST this can avoid some phone pop-up reminders.
after testing if the service is not killed in the background can successfully bounce the window in any interface.