How to resolve the Android how to do the service is closed and automatically start the implementation of the method _android

Source: Internet
Author: User

The first thing to say is that users may regard this practice as rogue software. Most of the time, programmers do not want to make software rogue software, there is no way, the leadership to decide.

When we use some Android apps, we may find that some of the services will run with them when we install an application. Also, these services start every time the phone is powered on. Some services to do even more, when the user in the running service to stop the service manually, after a period of time, the service has automatically run. Although, from the user's point of view, this way compares rogue. But how does this work from a programmer's point of view? After research, I found that there is a way to achieve. Let's share it with you.

Let's start with a brief introduction and put all the code in a moment.

How do I boot up?

This is relatively simple, enough information on the Internet, as long as the implementation of a broadcastreceiver, listening to the phone to start the completion of the event action_boot_completed can. Note that you don't seem to be using a simulator to test with your phone.

So how do you start a service and automatically start it again after the user shuts down?

In general, it will be in the above mentioned Broadcastreceiver implementation inside, listening to the phone startup completed, start a service, this is the general practice. The problem is that users can see that the service is resident in the services. If the user is very sensitive, it will stop the service, or even directly unload the relevant application. So, how can you implement a function on a regular basis without allowing users to see the service directly? Smart you must immediately think of, if not directly start the service, but to start a timmer, or alarmmanager, and then every once in a while to start the service, do things after the closure of the service can be.

Let's look at all the code below, but explain it more. These codes still have a lot of concepts, not familiar with Alarmmanager, Pendingintent, broadcastreceiver, service and so on these concepts of students can Baidu.

Copy Code code as follows:

Package com.arui.framework.android.daemonservice;

Import Android.app.AlarmManager;
Import android.app.PendingIntent;
Import Android.content.BroadcastReceiver;
Import Android.content.Context;
Import android.content.Intent;
Import Android.os.SystemClock;

public class Bootbroadcast extends Broadcastreceiver {

@Override
public void OnReceive (context context, Intent mintent) {

if (Intent.ACTION_BOOT_COMPLETED.equals (Mintent.getaction ())) {
Start complete
Intent Intent = new Intent (context, alarmreceiver.class);
Intent.setaction ("Arui.alarm.action");
Pendingintent Sender = pendingintent.getbroadcast (context, 0,
Intent, 0);
Long firstime = Systemclock.elapsedrealtime ();
Alarmmanager am = (alarmmanager) context
. Getsystemservice (Context.alarm_service);

10 seconds a cycle, Non-stop send the broadcast
Am.setrepeating (Alarmmanager.elapsed_realtime_wakeup, Firstime,
1000, sender);
}

}
}

Copy Code code as follows:

Package com.arui.framework.android.daemonservice;

Import Android.content.BroadcastReceiver;
Import Android.content.Context;
Import android.content.Intent;

public class Alarmreceiver extends Broadcastreceiver {
@Override
public void OnReceive (context context, Intent Intent) {

if (Intent.getaction (). Equals ("Arui.alarm.action")) {
Intent i = new Intent ();
I.setclass (context, daemonservice.class);
Start Service
Multiple calls to StartService do not start more than one service and call OnStart multiple times
Context.startservice (i);
}
}
}

Copy Code code as follows:

Package com.arui.framework.android.daemonservice;

Import Android.app.Service;
Import android.content.Intent;
Import Android.os.IBinder;
Import Android.util.Log;

public class Daemonservice extends Service {

@Override
Public IBinder Onbind (Intent Intent) {
return null;
}

@Override
public void OnCreate () {
Super.oncreate ();
LOG.V ("=========", "* * * daemonservice * * * * *: OnCreate");
}

@Override
public void OnStart (Intent Intent, int startid) {
LOG.V ("=========", "* * * daemonservice * * * * *: OnStart");
You can do the service here.
}
}

The following is the code for the manifest file.

Copy Code code as follows:

<receiver
Android:name= "Com.arui.framework.android.daemonservice.BootBroadcast"
android:permission= "Android.permission.RECEIVE_BOOT_COMPLETED" >
<intent-filter>
<action android:name= "Android.intent.action.BOOT_COMPLETED"/>
</intent-filter>
</receiver>
<receiver
Android:name= "Com.arui.framework.android.daemonservice.Alarmreceiver" >
<intent-filter>
<action android:name= "Arui.alarm.action"/>
</intent-filter>
</receiver>
<service
Android:name= "Com.arui.framework.android.daemonservice.DaemonService" >
</service>

continue to discuss this issue.

If the user stops the application (stops the application in the management application, or the third party software stops the entire application), the whole process is killed, all the services are killed, the Timmer, or the Alarmmanager is stopped. The service will not be started on a regular basis at this time.

Then, how can you do that, the user or Third-party software can not stop the entire application. We can then register a system-level listener (Broadcastreceiver) to listen for system-level messages, start Timmer again, or Alarmmanager. In this way, even if the application is killed, the application will start automatically at intervals. Concrete, it is not here to unfold.

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.