How to start an application on startup

Source: Internet
Author: User

I have recently studied how to enable auto-startup. I have found many examples on the Internet. The experiment is successful. I 'd like to share it with you today!

1. manifest. XML is as follows.

<? XML version = "1.0" encoding = "UTF-8"?>
<Manifest xmlns: Android = "http://schemas.android.com/apk/res/android"
Android: versioncode = "1" Android: versionname = "1.0" package = "cn. etzmico. Autorun">

<Application Android: icon = "@ drawable/icon" Android: Label = "@ string/app_name">
<Cycler Android: Name = "cn. etzmico. 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>
<Activity Android: Name = "cn. etzmico. Autorun" Android: Label = "@ string/app_name">
<Intent-filter>
<Action Android: Name = "android. Intent. Action. Main"/>
<Category Android: Name = "android. Intent. Category. launcher"/>
</Intent-filter>
</Activity>

</Application>

<Uses-Permission Android: Name = "android. Permission. receive_boot_completed"> </uses-Permission> sets the permission for auto-start.

</Manifest>

2. bootreceiver. Java

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, 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); // sets the startup parameter for the activity to be started. This parameter indicates that a new stack is created for the activity at startup.
/* If you want to start a service, follow below Method */
Context. startactivity (newintent );

}
}
}

3. Autorun. Java

Package CN. etzmico;

Import Android. App. activity;
Import Android. OS. Bundle;
Import CN. etzmico. Autorun. R;

Public class Autorun extends activity {
/** Called when the activity is first created .*/
@ Override
Public void oncreate (bundle savedinstancestate ){
Super. oncreate (savedinstancestate );
Setcontentview (R. layout. Main );
System. Out. println ("successful ");
}
}

It's that simple.

 

 

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.