Method for realizing automatic operation after startup by Android programming

This paper describes the method of Android programming to realize automatic operation after startup. Share with you for your reference, as follows:

Sometimes, applications need to run at runtime, such as a background service that automatically updates content from the Internet. How to realize the application of automatic startup? When writing this article, I associate Mr. Gao Huantang with "don ''t call me, I''ll call you back!" The summary framework is really on the point. Understanding the meaning of this sentence, many problems related to the implementation of a certain on the Android platform can be solved.

Usage scenario: after startup, it runs automatically and displays "hello. I started!" on the screen word.

Background: when Android starts, a broadcast will be sent with the content of action_ BOOT_ Completed, whose string constant is expressed as Android intent. action. BOOT_ COMPLETED。 Just "catch" this message in the program and start it again. Remember, the Android framework says: don ''t call me, I''ll call you back. What we need to do is get ready to receive this message, and the implementation means is to implement a broadcastreceiver.

Code parsing:

1、Activity:SayHello. java

This code is very simple. When the activity starts, create a textview and use it to display "hello. I started!" word.

2. Receive broadcast message: bootbroadcastreceiver java

This class is derived from broadcastreceiver. In the overlay method onReceive, it detects whether the received intent conforms to the boot_ Completed. If yes, start the sayhello activity.

3. Configuration: androidmanifest xml

Note the bold part. The node registers a receiver with the system, and the child node intent filter indicates receiving Android intent. action. BOOT_ Completed message. Don't forget to configure Android permission. RECEIVE_ BOOT_ COMPLETED。

After completion, compile the APK package and install it into the simulator or mobile phone. Shut down and restart.

More readers interested in Android related content can view the special topics of this site: introduction and advanced tutorial of Android development, summary of Android view skills, summary of Android layout skills, summary of Android debugging skills and solutions to common problems, summary of Android multimedia operation skills (audio, video, recording, etc.) Usage Summary of Android basic components and Android controls

I hope this article will help you in Android programming.

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