How Android listens for pasteboard content

Source: Internet
Author: User
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.
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.