Click here to Skip to main content
15,881,715 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi
I plan to have a service that can be enabled and disabled whenever the user wants.
But the phone is switched off if the service is running does not run unless the user activates it again.
I want the service to run automatically after power on the phone again.
Please help me grow.
Thank you very much
Posted

1 solution

You need to create a BroadcastReceiver and listen for the even RECEIVE_BOOT_COMPLETED.
That will require your application to have permission android.permission.RECEIVE_BOOT_COMPLETE.
Java
public class MyBootListener extends BroadcastReceiver {
    @Override
    public void onReceive(final Context context, final Intent intent) {
        final Intent startServiceIntent = new Intent(context, MyService.class);
        context.startService(startServiceIntent);
    }
}

Here's an example showing how to auto start an Activity[^] that you should be able to change to start your service.

Hope this helps,
Fredrik
 
Share this answer
 
Comments
Member 10496892 30-Oct-14 13:43pm    
thanks a lot
Fredrik Bornander 30-Oct-14 13:46pm    
Glad I could help.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900