Android BroadcastReceiver, auto run service after reboot of device

65,846

Solution 1

First do this

1) In your <manifest> element:

<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />

Solution 2

You forgot about the permissions

<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />

Solution 3

Though all the above answers were correct, however from Android Oreo they put some limitation on Background Services.

Check Background Execution Limits to know more about background limits in Android O.

You can't start a Service directly from BroadCastReceiver when the application is in the background.

However, you can start a foreground service from the receiver by calling startForegroundService() or use JobIntentService as there is no such limitation with JobIntentService.

Solution 4

Use this code on your Broadcast Receiver class:

if ("android.intent.action.BOOT_COMPLETED".equals(intent.getAction())) {
   Intent service = new Intent(context, MsgPushService.class);  
    context.startService(service);  
  }
Share:
65,846
BangBoat
Author by

BangBoat

Updated on May 21, 2020

Comments

  • BangBoat
    BangBoat almost 4 years

    Hello i am writing an application, which is when the phone reboot, the service will auto start instead of click on the application.

    Here is my code for

    BootCompleteReceiver.java

    package com.example.newbootservice;
    
    import android.content.BroadcastReceiver;  
    import android.content.Context;  
    import android.content.Intent;    
    
    public class BootCompleteReceiver extends BroadcastReceiver {   
    
        @Override  
        public void onReceive(Context context, Intent intent) {  
    
            Intent service = new Intent(context, MsgPushService.class);  
            context.startService(service);   
    
        }  
    
    }
    

    MsgPushService.java

    package com.example.newbootservice;
    
    import android.app.Service;  
    import android.content.Intent;  
    import android.os.IBinder;   
    import android.widget.Toast;
    
    public class MsgPushService extends Service {  
    
    
        @Override  
        public void onCreate() {  
            super.onCreate();    
        }  
    
        @Override  
        public int onStartCommand(Intent intent, int flags, int startId) {  
    
            Toast.makeText(this, "Service Started", Toast.LENGTH_LONG).show();
            return Service.START_STICKY;  
        }  
    
        @Override
        public void onDestroy() {
    
            super.onDestroy();
            Toast.makeText(this, "Service Destroy", Toast.LENGTH_LONG).show();
        }
    
        @Override  
        public IBinder onBind(Intent arg0) {  
            return null;  
        }  
    } 
    

    MainActivity.java (not sure whether i need this)

    package com.example.newbootservice;
    
    import android.os.Bundle;
    import android.app.Activity;
    import android.content.Intent;
    import android.view.Menu;
    
    public class MainActivity extends Activity {
    
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
    
            startService(new Intent(getBaseContext(), MsgPushService.class));
        }
    
        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            getMenuInflater().inflate(R.menu.activity_main, menu);
            return true;
        }
    }
    

    Manifest

    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="com.example.newbootservice"
        android:versionCode="1"
        android:versionName="1.0" >
    
        <uses-sdk
            android:minSdkVersion="8"
            android:targetSdkVersion="15" />
    
        <application
            android:icon="@drawable/ic_launcher"
            android:label="@string/app_name"
            android:theme="@style/AppTheme" >
    
            <service android:name=".MsgPushService"/>
    
            <receiver android:name=".BootCompleteReceiver">  
                <intent-filter>     
                    <action android:name="android.intent.action.BOOT_COMPLETED"/>  
                    <category android:name="android.intent.category.DEFAULT" />  
                </intent-filter>  
            </receiver>
    
            <activity
                android:name=".MainActivity"
                android:label="@string/title_activity_main" >
                <intent-filter>
                    <action android:name="android.intent.action.MAIN" />
    
                    <category android:name="android.intent.category.LAUNCHER" />
                </intent-filter>
            </activity>
        </application>
    </manifest>
    

    I want the service to be started automatically after reboot instead of starting it manually.

  • BangBoat
    BangBoat over 11 years
    Dixit, it works ! thank you. Do you have any idea if i want to avoid user kill the service, and it will start service again by using alarm or some sort of trigger to activate it ?
  • Dixit Patel
    Dixit Patel over 11 years
    @BangBoat service is automatically close when it work is complete & the service should stop itself when its job is done by calling stopSelf(), or another component can stop it by calling stopService().
  • BangBoat
    BangBoat over 11 years
    What i want to do with my application is. When phone get stolen, sim card is changed, and the the service will compare sim serial, if different the service/application will email the owner. I am just so lost what's the next step to do, right now my service will start after every reboot of device.
  • BangBoat
    BangBoat over 11 years
  • Dixit Patel
    Dixit Patel over 11 years
    i cant understand actually what you do in your application
  • BangBoat
    BangBoat over 11 years
    Actually. My application is to prevent phone stolen. The application will be running all the time, on every reboot it will check the default sim by sim serial. If sim serial is changed (stolen), then my application will email the owner of device.
  • Dixit Patel
    Dixit Patel over 11 years
    this service will start after every reboot of device & check the sim is changed or not for that just put sim compare code & send email code in Your service.
  • Tasneem
    Tasneem about 7 years
    Its not working for me on Nougat , I am trying to wake up FirebaseMessagingService which is being closed on force stop.
  • Nouman Ch
    Nouman Ch almost 6 years
    link is broken.
  • Ravi Kilnake
    Ravi Kilnake over 5 years
    i am unable to implement with foreground service, do you mind explaining with example
  • Prajwal Waingankar
    Prajwal Waingankar about 4 years
    That means as that of Oreo, we cannot start the broadcastreceiver service when the app is restarted when it is in background right? is there any work around?
  • Some random IT boy
    Some random IT boy about 4 years
    Link is not broken, it has definitely been hacked though xD