How to start an Android Application

Source: Internet
Author: User


Sometimes, Applications
It needs to run automatically at startup.
,

For example, a background that automatically updates content from the Internet
Service
. How can I enable applications that run automatically upon startup? When writing this article, I think of Mr. Gao hentang
"Don't call me, I'll call you
Back !"
Summary
Android
Framework. Understanding the meaning of this sentence involves many
Android
Platform

To implement certain functions.
Can solve the problem.


Use Case: Mobile Phone
Automatically runs the program after it is started.
, On the screen
Display on
"Hello. I started! "
.


Background: When
Android
At startup, a system will be issued
Broadcast, content is
Action_boot_completed
Its String constant is represented
Android

. Intent. Action. boot_completed
.

As long
"
Capture
"
Then start the message. Remember,
Android
Framework:
Don't call me, I'll call you
Back
. What we need to do is to prepare for receiving this message, and the Implementation means is to implement
Broadcastreceiver
.


Code

Resolution:

1
Interface

Activity
:
Sayhello. Java

Package com. ghstudio. bootstartdemo;

Import Android. app
. Activity;
Import Android. OS. Bundle;
Import Android. widget. textview
;

Public class sayhello extends activity {

@ Override
Public void oncreate (bundle savedinstancestate ){
Super. oncreate (savedinstancestate );

Textview
TV = new textview (this );
TV. settext ("Hello. I started! ");

Setcontentview (TV );
}
}


This code is very simple.
Activity
Create
Textview
, Use it to display
"Hello. I started! "
.

2
Receive broadcast messages:
Bootbroadcastreceiver. Java

Package com. ghstudio. bootstartdemo;

Import Android. content. broadcastreceiver;
Import Android. content. context;
Import Android. content. Intent
;

Public class bootbroadcastreceiver extends broadcastreceiver
{

Static final string action =
"Android. Intent. Action. boot_completed ";

@ Override
Public void onreceive (context, intent ){

If (intent. getaction (). Equals (Action )){
Intent sayhellointent = new intent (context, sayhello. Class );
Sayhellointent. addflags (intent. flag_activity_new_task );

Context. startactivity (sayhellointent );
}
}

}


This class is derived from
Broadcastreceiver
, Overwriting Method
Onreceive
, Detected
Intent
Yes
Boot_completed
If yes, start
Sayhello
That
Activity
.

3
, Configuration file
:
Androidmanifest. xml

<? XML version = "1.0"
Encoding = "UTF-8"?>
<Manifest
Xmlns: Android = "http://schemas.android.com/apk/res/android"
Package = "com. ghstudio. bootstartdemo"
Android: versioncode = "1"
Android: versionname = "1.0" type = "codeph" text = "/codeph">
<Application Android: icon = "@ drawable/icon"
Android: Label = "@ string/app_name">
<Activity
Android: Name = ". sayhello"
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 = ". bootbroadcastreceiver">
<Intent-filter>
<Action
Android: Name = "android. Intent. Action. boot_completed"
/>
</Intent-filter>
</Cycler>
</Application>
<Uses-SDK
Android: minsdkversion = "3"
/>

<Uses-Permission
Android: Name = "android. Permission. receive_boot_completed"> </uses-Permission>

</Manifest>



Note that the node registers
Cycler
, Subnode
Intent-Filter
Receive
Android. Intent. Action. boot_completed
Message. Do not forget the configuration
Android. Permission. receive_boot_completed
Permission.


After compilation
Output
APK
Package, installed to the simulation
Or mobile phone. Shut down and reboot.

 


This is a broadcast.
Cycler
Accept
Receive_boot_completed
This broadcast event starts when it is started.
App
.


If it is a system process, you need to add
Init
Configuration file to start,
Linux
Start.

 

Not a master
Linux Kernel
There is
Init
Folder is not? After compilation
Init
Generate, which is
Kernel
The first user process started, right?


If you have your own processes to start, such
Service
You need
Service
Add as this
Init
In this way, a process is started when the system is started?

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.