How to enable the Android app to automatically start when it is started

Source: Internet
Author: User

If you need to automatically start the application when Android starts up, you can respond to android. intent. action. BOOT_COMPLETED broadcasts messages. Android is triggered when the android system is started. intent. action. BOOT_COMPLETED message. The procedure is as follows:

1. Define a Broadcast Receiver, such as BootupReceiver.

 

[Java]
Public class BootupReceiver extends BroadcastReceiver {

@ Override
Public void onReceive (Context context, Intent intent ){

// Better delay some time.
Try {
Thread. sleep (2000 );
} Catch (InterruptedException e ){
// TODO Auto-generated catch block
E. printStackTrace ();
}
Intent I = new Intent (context, BootupDemoActivity. class );
I. addFlags (Intent. FLAG_ACTIVITY_NEW_TASK );
Context. startActivity (I );

}

}
Public class BootupReceiver extends BroadcastReceiver {
 
@ Override
Public void onReceive (Context context, Intent intent ){
 
// Better delay some time.
Try {
Thread. sleep (2000 );
} Catch (InterruptedException e ){
// TODO Auto-generated catch block
E. printStackTrace ();
}
Intent I = new Intent (context, BootupDemoActivity. class );
I. addFlags (Intent. FLAG_ACTIVITY_NEW_TASK );
Context. startActivity (I );
 
}
 
}

 

It is used to respond to android. intent. action. BOOT_COMPLETED.


2. Define the Broadcast Receiver in the Manifest File


<Cycler android: name = ". BootupReceiver" android: enabled = "true"
Android: permission = "android. permission. RECEIVE_BOOT_COMPLETED">
<Intent-filter>
<Action android: name = "android. intent. action. BOOT_COMPLETED"> </action>
<Category android: name = "android. intent. category. DEFAULT"/>
</Intent-filter>


</Cycler>


3. Add the required permissions.


<Uses-permission android: name = "android. permission. RECEIVE_BOOT_COMPLETED"/>


However, the app will automatically run when the mobile phone carries out its re-revelation. However, your application may be locked by the screen. After you unlock the screen, you can see the application you are running.


If you want to see your application on the machine, you can use the code to remove the screen lock. The method is as follows:


1. Add the following code to the onCreate event processing of the Activity.

[Java]
KeyguardManager keyguardManager
= (KeyguardManager) getSystemService (KEYGUARD_SERVICE );
KeyguardLock lock = keyguardManager. newKeyguardLock (KEYGUARD_SERVICE );

Lock. disableKeyguard ();
KeyguardManager keyguardManager
= (KeyguardManager) getSystemService (KEYGUARD_SERVICE );
KeyguardLock lock = keyguardManager. newKeyguardLock (KEYGUARD_SERVICE );
 
Lock. disableKeyguard ();
2. Add the required permissions to the Manifest file.


<Uses-permission android: name = "android. permission. DISABLE_KEYGUARD"/>

 



Download this sample code: http://www.bkjia.com/uploadfile/2012/0518/20120518113514147.zip






Excerpted from the mobile app
 

Related Article

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.