In Android, how does one enable the service automatically after it is disabled (zz)

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 the user stops the server, the service runs automatically for 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 you can disable the service. So how can we enable it again after it is disabled? If you don't start the service directly, you must start a Timmer or alarmmanager, and then start the service every other time.

 

 

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.

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, 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 );
}

}
}
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, 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 );
}
}
}
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 ){
Return NULL;
}

@ Override
Public void oncreate (){
Super. oncreate ();
Log. V ("==========", "****** daemonservice *****: oncreate ");
}

@ Override
Public void onstart (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.

<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>

 

Reprinted from: How to enable a service automatically after it is disabled in Android

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.