Android Judgment Switch Background judge press the home button

Source: Internet
Author: User

Write this blog is not to achieve the monitoring home button, but to determine the switch backstage, said not to write some boring blog, listen to Home button or the method of shielding home button, please Baidu.


I just want to write a two-state judgment on this blog:

1. The application switches to the background of the listener or is judged.

2. Application switch to the background and reopen the listening judgment.


So, the question is, if your company has a need: after each switch to the background of the app, determine whether the server that is currently connected to a socket is disconnected.

Lying trough, your city needs a lot of people, so annoying, this is difficult to achieve.



There are probably two solutions to the solution, the application of the background after the re-opening of the time to take those methods, this look at the activity of the life cycle, it seems that can not be judged. Some things are more complicated, every time go to resume, but not every time to go onreate onstart some column method.


Then this routine does not walk, we try to hide the activity in the place to start, activity a jump to activity B when pause, stop, then, in their own application activity jumps will also trigger this thing.

But where does he differ from pressing the home button or opening other apps in our app?

Solution: The difference between switching to other applications, the current application in the foreground is not our own package. Then we can use the current app's foreground app's package name to determine whether it is our own app, can be achieved.

But the question comes again, if we finish the last page, isn't it?

Can this switch background and finish distinguish apart?

Solution: Yes.

So, all the problems in this way are resolved by switching to the background. Switch to the background of the time to do a record on the line, record whether the current is the background, the next time to determine whether to open the background before the backstage, if the backstage, to determine whether sokect in the connection, but also to the background record to reset.



In theory, the process is this, stop, Judge Activitymanage to get the most foreground of the application is the application, not the words are backstage, there is the price judgment if the finish, do not record.



Okay, on the code:

    Private Boolean isfinished = false;    Private final String  run_sp_key= "Isrunningbackground";    @Override public    void Finish () {        super.finish ();        Isfinished = true;    }    If isfinished equals false, the current activity only switches to the background and does not exit.    @Override    protected void OnStop () {        super.onstop ();        if (!isfinished) {            String packagename= "com. Xxx.mypackagename ";//the package name of our own application            String topactivityclassname= packageutils.gettopactivityname (this);            if (Packagename!=null&&topactivityclassname!=null &&!topactivityclassname.startswith (packageName )            {                //app has background                sputils.put (this, Run_sp_key, true);    }}}
Above this is the processing at stop, the next is the time to start processing.


    protected void Onresume () {        super.onresume ();        Boolean ISREOEPN = (Boolean) sputils.get (this, Run_sp_key, false);        if (ISREOEPN) {has been disconnected            if (Isconnect ()) {                Sputils.put (this, Run_sp_key, false);                            } else {                Showconnectpopupwindow ();//Popup disconnect prompt Poupwindow            }}        }    









Android Judgment Switch Background judge press the home button

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.