Android uses broadcastreceiver to realize forced offline

Idea: first enter the login interface, enter the account and password, and then log in to the main interface. In the main interface, click the button to send a forced offline broadcast. After receiving the broadcast, the broadcast receiver enters the login interface again.

Create a new project broadcast03. The forced logoff function needs to close all activities first, and then return to the login interface. So first create an activitycollector class to manage all activities:

Three methods are defined in this class, which are used to add activities, remove activities and close all activities. Then create a baseactivity class as the parent of all other classes:

In the BaseActivity class, we call the addActivity () and removeActivity () methods of the ActivityCollector class, so that when other classes inherit the BaseActivity class, they will automatically rewrite these two methods.

Write the layout of the login interface login.xml:

Write a login activity. The new loginactivity inherits from baseactivity and realizes the login logic. Here, it is set that the login can be successful only when the account is admin and the password is 123456.

If you log in successfully, you will enter the main interface of mainactivity, where we_ Add a button in the main layout and click this button to send a forced offline broadcast.

Click the button on the mainactivity interface to send the broadcast logic:

A message with the value com.beijing.broadcast03.force is sent here_ Offline broadcast. Creating a broadcast receiver forceofflinereceiver inherits from broadcastreceiver. After receiving the broadcast, forceofflinereceiver will pop up a dialog box to achieve the purpose of forced logoff:

Here, build a dialog box through alertdialog. Builder. Note that you must call the setcancelable () method to set the dialog box as non cancelable (you can't exit the dialog box by pressing the return key). Then use the setpositivebutton () method to register the OK button for the dialog box. After clicking the OK button, it will call the finishall () method of the activitycollector to destroy all activities and re-enter the loginactivity interface. Since the activity is started in the broadcast receiver, be sure to add flag to intent_ ACTIVITY_ NEW_ The task flag. Finally, set the type of the dialog box to type_ SYstem_ Alert, otherwise the dialog box cannot pop up in the broadcast receiver. There are still tasks left to configure in androidmanifest.xml:

There are three points to note: first, a system level dialog box pops up in forceofflinereceiver, for which you need to declare android.permission.system_ ALERT_ Windows authority; Secondly, register loginactivity as the main activity; Finally, register forceofflinereceiver and specify it to receive com.beijing.broadcast03.force_ Offline this broadcast.

Now run the program to see the effect. Enter the login interface first, and you can enter the account and password:

After entering the account admin and password 123456, click the login button to enter the main interface:

Continue to click the send broadcast button, and a broadcast will be sent. After receiving this broadcast, the forceofflinereceiver will pop up a dialog box indicating that it has been forced offline:

At this time, the user will not be able to do any more operations. He can only click the OK button to return to the login interface. At this point, the goal of forced logoff through the broadcast receiver has been completed.

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
分享
二维码
< <上一篇
下一篇>>