How to enable the Service automatically after it is disabled in Android

Source: Internet
Author: User

First, users may regard this practice as a rogue software. Most of the time, programmers do not want to turn the software into rogue software. No way, the leaders have the say.

When using some Android applications, we may find that some services will run after an application is installed. In addition, these services are started every time when the mobile phone starts up. Some services are even more powerful. After a user stops the service manually in the running service, the service runs automatically after a while. Although, from the user's point of view, this method is relatively rogue. But from the programmer's point of view, how can this be done? After research, I found that there is a way to achieve it. We will share the following with you.

First, I will briefly introduce it and paste all the code later.

How can I start the instance at startup?

This is relatively simple. There is enough information on the Internet. You only need to implement a BroadcastReceiver and listen to the ACTION_BOOT_COMPLETED event completed by the mobile phone. It seems that the simulator cannot be used. You need to test it on a mobile phone.

So how can we start a Service automatically after the user closes it?

Generally, in the implementation of BroadcastReceiver mentioned above, a Service is started after the listening mobile phone is started. This is a general practice. The problem is that the user can see that the Service is resident in the Service. If the user is sensitive, the Service will be stopped, or even the relevant applications will be directly uninstalled. So how can we implement a function on a regular basis without allowing users to directly view the Service? If you are smart, you must immediately think about it. If you do not start the Service directly, you can start a timmer or alarmManager, and then start the Service at intervals, after you finish the task, close the Service.

Let's take a look at all the code below, but I have explained it more. There are still many concepts in these codes. If you are not familiar with AlarmManager, PendingIntent, BroadcastReceiver, and Service, you can Baidu.

Copy codeThe Code is 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 ())){
// Startup 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 );

// Send broadcast continuously for a period of 10 seconds
Am. setRepeating (AlarmManager. ELAPSED_REALTIME_WAKEUP, firstime,
10*1000, sender );
}

}
}

Copy codeThe Code is 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 the service
// Multiple call of startService does not start multiple services, but calls onStart multiple times.
Context. startService (I );
}
}
}

Copy codeThe Code is 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 ");
// Here we can do what the Service should do
}
}

The following is the code of the manifest file.

Copy codeThe Code is as follows: <Cycler
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>
</Cycler>
<Cycler
Android: name = "com. arui. framework. android. daemonservice. Alarmreceiver">
<Intent-filter>
<Action android: name = "arui. alarm. action"/>
</Intent-filter>
</Cycler>
<Service
Android: name = "com. arui. framework. android. daemonservice. DaemonService">
</Service>

Continue to discuss this issue.

If the user stops the entire application (stops the application in the management application, or the third-party software stops the entire application), the entire process is killed, and all services are naturally killed. timmer, or alarmManager stops. In this case, the service will not be started on a regular basis.

Then, how can we achieve this? users or third-party software cannot stop the entire application. We can register another system-level listener (BroadcastReceiver) to listen to system-level messages, and start timmer or alarmManager again. In this way, even if the application is killed, the application will automatically start after a period of time. The details are not shown here.

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.