Android activity/service automatically runs after startup

Source: Internet
Author: User

I have read several examples on the Internet and started a system to run it directly.ActivitySmallProgram

CodePaste it below:

FirstBroadcastreceiverA new class is derived to listen to broadcast messages sent after the system is started.Android. Intent. Action. boot_completed.

Bootreceiver. Java:

Import Android. content. broadcastreceiver;
Import Android. content. context;
Import Android. content. intent;
Import Android. util. log;

Public ClassBootreceiverExtendsBroadcastreceiver {

Public VoidOnreceive (context, intent ){

If (Intent. getaction (). Equals ( " Android. Intent. Action. boot_completed " ))
{
Log. D ( " Bootreceiver " , " System Boot completed " );
Intent newintent =   New Intent (context, firstrun. Class );
Newintent. setaction ( " Android. Intent. Action. Main " ); // Myactivity action defined in androidmanifest. xml

Newintent. addcategory ( " Android. Intent. Category. Launcher " ); // Myactivity category defined in androidmanifest. xml

Newintent. setflags (intent. flag_activity_new_task ); // If activity is not launched in activity environment, this flag is mandatory to set

Context. startactivity (newintent );

//If you want to start a service, follow below method:

/****************************************** *************

Intent service = new intent (yourservice. action_start );
Service. setclass (context, yourservice. Class );
Context. startservice (service );

**************************************** **************/
}
}
}

 

Next, this class listens to the activity that we want to run after the system is started.
Firstrun. Java

 

Import Android. App. activity;
Import Android. OS. Bundle;

Public ClassFirstrunExtendsActivity {

Public VoidOncreate (bundle savedinstancestate ){
Super. Oncreate (savedinstancestate );
Setcontentview (R. layout. Main );
}
}

 

Of course, we also need to modify the configuration file. Note that you need to add
<Uses-Permission Android: Name="Android. Permission. receive_boot_completed"></Uses-Permission>

Manifest. xml

<? XML version = "1.0" encoding = "UTF-8" ?>
< Manifest Xmlns: Android = "Http://schemas.android.com/apk/res/android"
Package = "Com. Service. prac"
Android: versioncode = "1"
Android: versionname = "1.0" >

< Application Android: icon = "@ Drawable/icon" Android: Label = "@ String/app_name" >
< 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 >
< Activity Android: Name = ". Firstrun" >
< Intent-Filter >
< Action Android: Name = "Android. Intent. Action. Main"   />
< Category Android: Name = "Android. Intent. Category. launcher"   />
</ Intent-Filter >
</ Activity >
</ Application >
< Uses-SDK Android: minsdkversion = "3"   />
< Uses-Permission Android: Name = "Android. Permission. receive_boot_completed" > </ Uses-Permission >
</ Manifest >

 


 

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.