Preface
Before that, no matter whether we do Activity, Service, or BroadcastReceiver, they all run after the boot. In fact, during the boot event, an event called Android will also be sent. intent. action. for broadcast information of BOOT_COMPLETED, as long as you can receive this ACTION name, you can run your own program in our custom BroadcastReceiver onReceive () method.
The program will run as soon as it is started, knowing that the program has been deleted.
Ideas
In order to capture the system boot event broadcast, you must go to AndroidMainfesy. set the <receiver ER/> label in the <application/> node in xml, and set <actionandroid: name = "android. intent. action. BOOT_COMPLETED "/>. In this way, the Android System Broadcasts BOOT_COMPLETED every time the server is started, the broadcast receiver that is interested in this broadcast is the custom MyBroadcastReceiver class inherited from BroadcastReceiver. Therefore, the onReceive () method of this class will be called, in this method, we can run our program.
Steps
I. layout file writing
1.1 main. xml
<? Xml version = "1.0" encoding = "UTF-8"?>
<LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android"
Android: orientation = "vertical"
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent"
>
<TextView
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: text = "@ string/hello"
Android: id = "@ + id/mytv"
/>
</LinearLayout>
Ii. Code File writing
2.1 MainActivity. java
Package com. menglin. openequipmentrunapp;
Import android. app. Activity;
Import android. OS. Bundle;
Import android. widget. TextView;
Public class MainActivity extends Activity
{
Private TextView TV;
@ Override
Public void onCreate (Bundle savedInstanceState)
{
Super. onCreate (savedInstanceState );
// Load the main. xml layout File
SetContentView (R. layout. main );
// Obtain the TextView object through the findViewById () method
TV = (TextView) findViewById (R. id. mytv );
// Set the text of the TextView object
TV. setText (R. string. hello );
}
}
2.2 MyBroadcastreceiver. java
Package com. menglin. openequipmentrunapp;
Import android. content. BroadcastReceiver;
Import android. content. Context;
Import android. content. Intent;
Public class MyBroadReceiver extends BroadcastReceiver
{
Static final String ACTION = "android. intent. action. BOOT_COMPLETED ";
@ Override
Public void onReceive (Context context, Intent intent)
{
If (intent. getAction (). equals (ACTION ))
{
// Create an Intent object and specify the class to be started
Intent myintent = new Intent (context, MainActivity. class );
// Call another Activity and hand over the main control permission to MainActivity
Context. startActivity (myintent );
}
}
}
Iii. project configuration file
<? Xml version = "1.0" encoding = "UTF-8"?>
<Manifest xmlns: android = "http://schemas.android.com/apk/res/android"
Package = "com. menglin. openequipmentrunapp"
Android: versionCode = "1"
Android: versionName = "1.0" type = "codeph" text = "/codeph">
<Uses-sdk android: minSdkVersion = "8"/>
<Application android: icon = "@ drawable/icon" android: label = "@ string/app_name">
<Activity android: name = ". MainActivity"
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 = ". MyBroadcastReceiver">
<Intent-filter>
<Action android: name = "android. intent. action. BOOT_COMPLETED"/>
</Intent-filter>
</Cycler>
</Application>
<Uses-sdk android: minSdkVersion = "8"/>
<Uses-permission android: name = "android. permission. RECEIVE_BOOT_COMPLETED"/>
</Manifest>
The running effect is as follows:
As long as the server is started, our MainActivity. java program will appear.