Drag the icon in the launcher of Android to stop and display the unmount analysis.

Source: Internet
Author: User

People who use the launcher of ADW should be somewhat impressed with the function, that is, to drag the application or widget icon to the delete icon and stay for more than 1 second,

The "drop to uninstall" prompt is displayed. If you open it, open the interface for uninstalling the program. Now, you need to analyze and simulate this function.

 

There are two main things used: unload and stay tips.

The main usage of uninstallation is:

Intent uninstallintent = new intent (intent. action_delete, <br/> Uri. parse ("package:" + muninstallpkg); <br/> deletezone. this. getcontext (). startactivity (uninstallintent ); 

From this point of view, to uninstall an application, you need to know its packagename.

 

When you drag the icon to the delete column and wait for a certain period of time, it is switched to the unmount mode.

 

Postdelayed (runnable R, long delaymillis)

.

 

Now let's analyze the response events generated by drag and drop,

When you start to drag, the first operation is:

Ondragstart-> ondragover-> ondragexit-> ondragend.

When you drag the icon to the deleted area, ondragenter is executed and ondragexit is executed.

If the icon is dragged to the delete area and then released, it will be ondragenter-> ondragdrop-> ondragexit-> ondragend.

The two events that will be executed when the drag ends are ondragexit-> ondragend. ondragexit occurs before ondragend.

 

Therefore, when ondragenter is used, read packagename and postdelayed and send a delayed message to runnable (set the uninstall flag)

And then in ondragexit

 

Removecallbacks (runnable R) removes messages from the queue,

And postdelayed a message to clear the unmount mark. Of course, the delay here is much smaller than the time in the previous ondragenter,

The result is as follows:

1. If it is dragged to the deleted area, the unmount flag is cleared when it is dragged out;

2. if you drag the image into the deleted area for a long time before releasing it (you should open the unmount interface and set the unmount flag), make sure that ondragend has been executed during the delay because postdelayed indicates clearing the unmount flag, therefore, if you want to retain the unmount flag, the delay time is longer than the ondragend execution time.

 

At the end of ondragend, determine whether the uninstall flag and program packagename are unmounted if the conditions are met.

The source code is as follows:

// Yao. guet add <br/> Private Static final string tag = "deletezone"; <br/> Private Static final int stay_over_time = 1500; <br/> Private Static Boolean muninstall; <br/> private Boolean menterdelzone; <br/> private string muninstallpkg; <br/> private final handler mhandler = new handler (); <br/> private final runnable mshowunstaller = new runnable () {<br/> Public void run () {<br/> // todo Auto-generated method stub <br/> log. E (TAG, "runnable showunstaller is called! "); <Br/> muninstall = menterdelzone; <br/> If (muninstall = true) {<br/> toast. maketext (mcontext," drop to unstall the app! ", 500). Show (); <br/>}</P> <p> }; 

Ondragenter:

// Yao. guet <br/> log. E (TAG, "dragenter"); <br/> If (item instanceof launcherappwidgetinfo | <br/> item instanceof applicationinfo | <br/> item instanceof shortcutinfo) {<br/> mhandler. removecallbacks (mshowunstaller); <br/> log. E (TAG, "postdelayed"); <br/> menterdelzone = true; <br/> mhandler. postdelayed (mshowunstaller, stay_over_time); </P> <p> // get the package name <br/> If (item instanceof AP Plicationinfo) {<br/> final applicationinfo AI = (applicationinfo) item; <br/> muninstallpkg = AI. componentname. getpackagename (); <br/> If (this. getclass (). getpackage (). getname (). equals (muninstallpkg) <br/> muninstallpkg = NULL; <br/>}else if (item instanceof launcherappwidgetinfo) {<br/> final launcherappwidgetinfo appwidget = (launcherappwidgetinfo) item; <br/> final appwidgetproviderinfo AP = Appwidgetmanager. getinstance (mlauncher) <br/>. getappwidgetinfo (appwidget. appwidgetid); <br/> If (AP! = NULL) <br/> muninstallpkg = ap. provider. getpackagename (); <br/>} else if (item instanceof shortcutinfo) {<br/> final shortcutinfo scinfo = (shortcutinfo) item; <br/> If (scinfo. iconresource! = NULL) {<br/> muninstallpkg = scinfo. iconresource. packagename; <br/>} else if (scinfo. intent! = NULL) {<br/> muninstallpkg = scinfo. Intent. getcomponent (). getpackagename (); <br/>}< br/>} 

Ondragexit:

// Yao. guet add to remove the call back function <br/> log. E (TAG, "dragexit"); <br/> mhandler. removecallbacks (mshowunstaller); <br/> dragview. setpaint (null); <br/> If (muninstall = true) {<br/> menterdelzone = false; <br/> mhandler. postdelayed (mshowunstaller, 100); <br/>} 

Ondragend:

// Yao. guet deal the uninstall flag when drag is end <br/> log. E (TAG, "flag:" + muninstall + ", PKG:" + muninstallpkg); <br/> If (muninstall & (muninstallpkg! = NULL) {<br/> intent uninstallintent = new intent (intent. action_delete, <br/> Uri. parse ("package:" + muninstallpkg); <br/> deletezone. this. getcontext (). startactivity (uninstallintent); <br/>} 

Run:

 

 

 

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.