Android BroadcastReceiver, auto run service after reboot of device
Asked Answered
D

4

37

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.

Doughboy answered 17/1, 2013 at 17:58 Comment(0)
J
10

You forgot about the permissions

<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
Jacobinism answered 17/1, 2013 at 18:12 Comment(0)
C
8

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.

Caterwaul answered 8/3, 2018 at 10:22 Comment(2)
i am unable to implement with foreground service, do you mind explaining with exampleRiver
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?Vicentevicepresident
D
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);  
  }
Dictate answered 24/12, 2018 at 5:7 Comment(0)
R
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"/>
Rist answered 24/3, 2015 at 13:49 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.