Sometimes, the application needs to run automatically when it is started, for example, a background service that automatically updates content from the Internet. How can I enable applications that run automatically upon startup? At the time of writing this article, I think that Mr. Gao hentang uses "Don't call me, I'll call you back !" To sum up the Android framework, let's talk about the idea. Understanding the meaning of this sentence can solve many problems related to the implementation of certain functions on the Android platform.
Application Scenario: After the mobile phone is turned on, the program automatically runs and "Hello. I started!" is displayed on the screen! .
Background: when Android is started, a system broadcast is sent with the content ACTION_BOOT_COMPLETED, and its String constant is represented as android. intent. action. BOOT_COMPLETED. You only need to "capture" the 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 to prepare for receiving this message, and the Implementation means is to implement a BroadcastReceiver.
Code parsing:
1. Interface Activity: SayHello. java
Package com. ghstudio. BootStartDemo;
Import android. app. Activity;
Import android. OS. Bundle;
Import android. widget. TextView;
Public class SayHello extends Activity
{
@ Override
Public void onCreate (Bundle savedInstanceState)
{
Super. onCreate (savedInstanceState );
TextView TV = new TextView (this );
TV. setText ("Hello. I started! ");
SetContentView (TV );
}
}
This code is very simple. When the Activity starts, create a TextView and use it to display "Hello. I started! .
2. Receive broadcast messages: BootBroadcastReceiver. java
Package com. ghstudio. BootStartDemo;
Import android. content. BroadcastReceiver;
Import android. content. Context;
Import android. content. Intent;
Public class BootBroadcastReceiver extends BroadcastReceiver
{
Static final String ACTION = "android. intent. action. BOOT_COMPLETED ";
@ Override
Public void onReceive (Context context, Intent intent)
{
If (intent. getAction (). equals (ACTION ))
{
Intent sayHelloIntent = new Intent (context, SayHello. class );
SayHelloIntent. addFlags (Intent. FLAG_ACTIVITY_NEW_TASK );
Context. startActivity (sayHelloIntent );
}
}
}
This class is derived from BroadcastReceiver. In the overwriting method onReceive, it detects whether the received Intent meets BOOT_COMPLETED. If yes, it starts the Activity SayHello.
3. configuration file: AndroidManifest. xml
<? Xml version = "1.0" encoding = "UTF-8"?>
<Manifest xmlns: android = "http://schemas.android.com/apk/res/android"
Package = "com. ghstudio. BootStartDemo"
Android: versionCode = "1"
Android: versionName = "1.0" type = "codeph" text = "/codeph">
<Application android: icon = "@ drawable/icon" android: label = "@ string/app_name">
<Activity android: name = ". SayHello" android: label = "@ string/app_name">
<Intent-filter>
<Action android: name = "android. intent. action. MAIN"/>
<Category android: name = "android. intent. category. LAUNCHER"/>
</Intent-filter>
</Activity>
<Cycler android: name = ". BootBroadcastReceiver">
<Intent-filter>
<Action android: name = "android. intent. action. BOOT_COMPLETED"/>
</Intent-filter>
</Cycler>
</Application>
<Uses-sdk android: minSdkVersion = "3"/>
<Uses-permission android: name = "android. permission. RECEIVE_BOOT_COMPLETED"> </uses-permission>
</Manifest>
Note that the node registers a receiver with the system. The intent-filter sub-node receives the android. intent. action. BOOT_COMPLETED message. Do not forget to configure the permission for android. permission. RECEIVE_BOOT_COMPLETED.
Compile the apk package and install it to the simulator or mobile phone. Shut down and reboot.