Android Basics Tutorial Boot Example _android

Source: Internet
Author: User

Manifest.xml file:

Copy Code code as follows:

<service
Android:name= ". Daemonservice "
Android:enabled= "true"
Android:process= ". Daemonservice ">
<intent-filter android:priority= "1000" >
<action android:name= "Cn.test.DaemonService"/>

<category android:name= "Android.intent.category.DEFAULT"/>
</intent-filter>
</service>

Note: Because we have to run the service in the background, we do not adopt the Bindservice way, but directly adopt the StartService way.

This will not be the end of our program, but also the end of our service.

Copy Code code as follows:

Package cn.start.test;

Import java.util.List;
Import Android.annotation.SuppressLint;
Import Android.app.ActivityManager;
Import Android.app.Service;
Import Android.app.ActivityManager.RunningAppProcessInfo;
Import android.content.Intent;
Import Android.os.Handler;
Import Android.os.IBinder;
Import Android.util.Log;

public class Daemonservice extends Service {

private static final String TAG = "Alarmreceiver";
Handler hd1 = new Handler ();
int delay = 5000;

Public IBinder Onbind (Intent Intent) {
TODO auto-generated Method Stub
return null;
}

@SuppressLint ("Newapi")
public void OnCreate () {

SYSTEM.OUT.PRINTLN ("Service started successfully .....") ");
Hd1.postdelayed (mtasks, delay);

}

Private Runnable mtasks = new Runnable () {
@SuppressLint ("Newapi")
public void Run () {
Android.os.Process.setThreadPriority (Android.os.Process.THREAD_PRIORITY_FOREGROUND);
if (checkmainappisactive ()) {
LOG.D (TAG, "Service detection main application is still running");

} else {
LOG.D (TAG, "Service detection main application has been closed");
Intent Intent = Getpackagemanager (). Getlaunchintentforpackage (
"Cn.start.test");
if (intent!= null) {
DaemonService.this.startActivity (Intent);
LOG.D (TAG, "service launches the main application.") ");
} else {
LOG.D (TAG, "the service detects that the main application is not installed and exits automatically." ");
Return
}
}
Hd1.postdelayed (mtasks, delay);

}
};

/**
* Detect if the main program is still running 
* @return
*/
@SuppressLint ({"Newapi", "Newapi"})
public Boolean checkmainappisactive () {
Activitymanager am = (activitymanager) getsystemservice (Activity_service);
Getting a running application
List<runningappprocessinfo> run = am.getrunningappprocesses ();
for (Runningappprocessinfo Ra:run) {
if (Ra.processName.equals ("Cn.start.test")) {
return true;
}
}
return false;
}
}

Boot automatically start the program, the nature of the boot started broadcast.

Manifest.xml file:

Copy Code code as follows:

<receiver android:name= ". Startupreceiver ">
<intent-filter android:priority= "1000" >
<action android:name= "Android.intent.action.BOOT_COMPLETED"/>
</intent-filter>
</receiver>

Copy Code code as follows:

public class Startupreceiver extends Broadcastreceiver {

@Override
public void OnReceive (context context, Intent Intent) {
TODO auto-generated Method Stub
Intent i = new Intent (context,loginactivity.class);
I.setflags (Intent.flag_activity_new_task);
Context.startactivity (i);


Intent serviceintent = new Intent (context,daemonservice.class);
Context.startservice (serviceintent);
}
}

Summary: Startup broadcast must be registered in the manifest file. Timer should pay attention to timing delay.

by list<runningappprocessinfo> run = am.getrunningappprocesses (); Determine if your process is still running

Through Getpackagemanager (). Getlaunchintentforpackage ("Cn.start.test"); Getpackagemanager (). Getlaunchintentforpackage ("Cn.start.test");

To start your own program.

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.