35

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.

CopsOnRoad
  • 109,635
  • 30
  • 367
  • 257
BangBoat
  • 415
  • 2
  • 5
  • 10

5 Answers5

52

First do this

1) In your <manifest> element:

<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
Dixit Patel
  • 6,051
  • 1
  • 30
  • 42
  • 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 ? – BangBoat Jan 17 '13 at 18:34
  • @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(). – Dixit Patel Jan 17 '13 at 18:37
  • 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 Jan 17 '13 at 18:40
  • http://stackoverflow.com/questions/14051023/get-current-simcard-number-in-android – BangBoat Jan 17 '13 at 18:40
  • i cant understand actually what you do in your application – Dixit Patel Jan 17 '13 at 18:45
  • 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. – BangBoat Jan 17 '13 at 18:49
  • 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. – Dixit Patel Jan 17 '13 at 18:52
  • Its not working for me on Nougat , I am trying to wake up FirebaseMessagingService which is being closed on force stop. – Tas Apr 05 '17 at 05:46
  • 3
    link is broken. – Nouman Ch Jun 06 '18 at 08:08
  • Link is not broken, it has definitely been hacked though xD – Some random IT boy Apr 14 '20 at 15:37
9

You forgot about the permissions

<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
agamov
  • 4,367
  • 1
  • 25
  • 30
7

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.

Kunu
  • 3,825
  • 3
  • 27
  • 57
2

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);  
  }
Juboraj Sarker
  • 848
  • 1
  • 8
  • 13
0

also try these permissions,it may help you .ifyou are using htc phones then these permissions are required

<action android:name="android.intent.action.BOOT_COMPLETED" />
<category android:name="android.intent.category.DEFAULT" />
<action android:name="android.intent.action.QUICKBOOT_POWERON"/>
bcesars
  • 1,048
  • 1
  • 17
  • 36
saksham agarwal
  • 240
  • 3
  • 14