Android auto-start

Source: Internet
Author: User

To enable boot in the Android system, it takes only a few steps.

1. Define Broadcasting

2. register the broadcast class in Manifest. xml

3. Add Permissions

 

 

The following is the specific operation.

 

First, let's define the broadcast class.

Create a class BootReceiver to inherit BroadcastReceiver.

Rewrite some necessary Java Functions

[Html] package cn. etzmico;
 
Import android. content. BroadcastReceiver;
Import android. content. Context;
Import android. content. Intent;
Import android. util. Log;
 
Public class BootReceiver extends BroadcastReceiver {
Public void onReceive (Context context, Intent intent ){
 
If (intent. getAction (). equals ("android. intent. action. BOOT_COMPLETED ")){
Log. d ("BootReceiver", "system boot completed ");
 
// Context, AutoRun. class
Intent newnewIntent = new Intent (context, AutoRun. class );
 
/* MyActivity action defined in AndroidManifest. xml */
NewIntent. setAction ("android. intent. action. MAIN ");
 
/* MyActivity category defined in AndroidManifest. xml */
NewIntent. addCategory ("android. intent. category. LAUNCHER ");
 
/*
* If activity is not launched in Activity environment, this flag is
* Mandatory to set
*/
NewIntent. setFlags (Intent. FLAG_ACTIVITY_NEW_TASK );
 
/* If you want to start a service, follow below method */
Context. startActivity (newIntent );
 
}
}
}
Package cn. etzmico;

Import android. content. BroadcastReceiver;
Import android. content. Context;
Import android. content. Intent;
Import android. util. Log;

Public class BootReceiver extends BroadcastReceiver {
Public void onReceive (Context context, Intent intent ){

If (intent. getAction (). equals ("android. intent. action. BOOT_COMPLETED ")){
Log. d ("BootReceiver", "system boot completed ");

// Context, AutoRun. class
Intent newIntent = new Intent (context, AutoRun. class );

/* MyActivity action defined in AndroidManifest. xml */
NewIntent. setAction ("android. intent. action. MAIN ");

/* MyActivity category defined in AndroidManifest. xml */
NewIntent. addCategory ("android. intent. category. LAUNCHER ");

/*
* If activity is not launched in Activity environment, this flag is
* Mandatory to set
*/
NewIntent. setFlags (Intent. FLAG_ACTIVITY_NEW_TASK );

/* If you want to start a service, follow below method */
Context. startActivity (newIntent );

}
}
}
 

AutoRun. class is the Activity where the program runs.


Next, register the broadcast class in Manifest. xml.

[Html] <er android: name = ". BootReceiver" android: label = "@ string/app_name">
<Intent-filter>
<Action android: name = "android. intent. action. BOOT_COMPLETED"/>
<Category android: name = "android. intent. category. LAUNCHER"/>
</Intent-filter>
</Cycler>
<Cycler android: name = ". BootReceiver" android: label = "@ string/app_name">
<Intent-filter>
<Action android: name = "android. intent. action. BOOT_COMPLETED"/>
<Category android: name = "android. intent. category. LAUNCHER"/>
</Intent-filter>
</Cycler>

Finally, add the permission.

[Html] <uses-permission android: name = "android. permission. RECEIVE_BOOT_COMPLETED"> </uses-permission>
<Uses-permission android: name = "android. permission. RECEIVE_BOOT_COMPLETED"> </uses-permission>

 

In this way, the Android system is automatically started upon startup. Do not forget the operations in Manifest. xml!

 

 

There are two resources. They are essentially the same. After the program runs, shut down the virtual machine or mobile devices, and restart the system. When the system program loads foreign currency, our program runs.

If you want to hide it, you only need to add finish. I have added a System. out in the program, so you don't have to worry about the success of the finish operation. You just need to check whether Successful exists in LogCat.

Yes.




 

Remember to check whether the time is consistent to avoid unnecessary trouble!

 

Demo resource 1: http://www.bkjia.com/uploadfile/2012/0308/20120308012933938.zip
 

Demo Resource 2: http://www.bkjia.com/uploadfile/2012/0308/20120308013159656.zip
 

 

From Etzmico
 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.