Code example of Android forced offline function

There are many application scenarios of forced offline. For example, many of our accounts have been robbed and will be pushed offline

Detailed implementation:

In fact, the idea of realizing the forced offline function is also relatively simple. You only need to pop up a dialog box in the interface, so that users can't do other operations. You must click the OK button in the dialog box and then return to the login interface

But there is another problem. We inform the user to force offline. The user may be in any interface. Do you need to write the logic of a pop-up dialog box on each interface? Of course not! We can easily realize this function with the help of the broadcasting knowledge learned in this chapter

Create baseactivity class as the parent class of all activities. The code is as follows:

First write the layout file activity_ Login file:

Code in loginactivity.java:

At this time, we can understand that after logging in to the interface, enter the activity_ Main.xml is the main interface. The main interface has no other functions, only a textview for display and a button for offline function

Code in mainactivity.java:

A broadcast is sent in the click event of our button, and the broadcast value is com.junzaivip.broadcastbestpractice.force_ Offline, this broadcast is used to inform the program to force users to offline

In other words, the logic of forcing users to log off is not written in mainactivity, but should be written in the receiver receiving this broadcast. The advantage is that the function of forcing users to log off will not be attached to any interface. No matter where the program is, it only needs to issue such a broadcast to complete the operation of forcing users to log off

Then, you need to create a broadcast receiver to receive this forced offline broadcast. The only problem is, where should you create it? A dialog box needs to pop up in the broadcast receiver to block the normal operation of users. However, if a statically registered broadcast receiver is created, there is no way to pop up UI controls such as dialog box in onreceive() method, and obviously we can't and can't register a dynamic broadcast receiver in each activity

So what should we do? In fact, obviously, you only need to dynamically register a broadcast receiver in baseactivity, because all activities inherit from baseactivity

Code in baseactivity:

We need to set the main activity as loginactivity instead of mainactivity. Simulate accessing a program first on the login page

Modify the androidmanifest.xml file:

The operation effect is as follows:

Source code: broadcastbestpractice_ jb51.rar

The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support programming tips.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>