6

I want create an application which contains only a service (no activity). This service must start on boot. My problem is that it seems the boot receiver don't seems call if there aren't activity. I have test with the following example. I have the different files :

MyReceiver.java :

package com.test.teststartserviceatboot;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.widget.Toast;

public class MyReceiver extends BroadcastReceiver {
    @Override
    public void onReceive( Context ctx, Intent i ) {

        Log.v( "MyReceiver", "onReceive : ");
        Intent intent = new Intent( ctx, MonService.class );
        ctx.startService(intent);
    }
}

MyService.java :

package com.test.teststartserviceatboot;

import android.app.Service;

public class MyService extends Service {
    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Log.v( "MyService","onStartCommand" );
        return super.onStartCommand(intent, flags, startId);
    }
    @Override
    public IBinder onBind( Intent arg0 ) {
        Log.v( "MyService","onBind" );
        return null;
    }
}

MainActivity.java:

package com.test.teststartserviceatboot;

import android.app.Activity;
import android.os.Bundle;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
}

I only modify the AndroidManifest on my several test.

  • Test 1 (with Activity)

    <uses-sdk android:minSdkVersion="17" 
            android:targetSdkVersion="17"/>
    
    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
    
    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
    
        <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>
        <service
            android:name=".MyService"
            android:exported="false"
            android:label="MyService" >
        </service>
    
        <receiver android:name=".MyReceiver" >
            <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED" />
            </intent-filter>
        </receiver>
    </application>
    

-> after reboot, the service is running.I see log :

AtivityManager Start proc com.test.teststartserviceatboot for broadcast com.test.teststartserviceatboot/.MyReceiver: pid=1808 uid=10156 gids={50156}

MyReceiver onReceive
MyService onStartCommand

  • Test 2 (without Activity)

    <uses-sdk android:minSdkVersion="17" 
            android:targetSdkVersion="17"/>
    
    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
    
    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <service
            android:name=".MyService"
            android:exported="false"
            android:label="MyService" >
        </service>
    
        <receiver android:name=".MyReceiver" >
            <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED" />
            </intent-filter>
        </receiver>
    </application>
    

    ->The service don't running

    • Test 3

I use the same appllication on test 1 (with activity). This time I kill the app before to restart the tablet (Parameter->Apps->TestServiceAtBoot->force stop). -> After reboot, service don't running


Is it necessary to have an activity for brodcast receiver works ? And why ?

Thank your for your lightening.

helene
  • 1,201
  • 2
  • 18
  • 30
  • I don't think so. because it's perfectly working without registering **Activity** in manifest.xml – M D Jan 09 '15 at 08:51
  • @MD he said it's NOT working when an activity is not registered in the manifest file – Leo Jan 09 '15 at 08:55
  • @helene how about making the activity fully transculent? – Leo Jan 09 '15 at 08:56
  • @Leo Read my comment carefully. – M D Jan 09 '15 at 08:56
  • You need to read http://stackoverflow.com/questions/8531926/how-to-start-a-service-when-apk-is-installed-for-the-first-time – Pankaj Kumar Jan 09 '15 at 08:57
  • @Leo Indeed I can create a translucent activity but I wanted avoid to create if it isn't necessary. – helene Jan 09 '15 at 09:27
  • @helene well, I'm afraid that's the only way your `BroadcastReceiver` will ever get hit from outside the process boundaries – Leo Jan 09 '15 at 12:49

1 Answers1

8

From Android 3.1, BroadcastReceiver will not work until the user has manually launched an activity, This is for provide security . once the user runs the app for the first time then your BroadcastReceiver will run always except it does not Force Stop it. Once activity launch at first time your broadcast receiver will run even after reboot your deice.

Therefore in your application you must have one Activity to run BroadcastReceiver.

PPD
  • 5,660
  • 12
  • 52
  • 86
  • but whatsapp gmail like apps are running check if u have above device 3.1 – wadali Jan 09 '15 at 08:56
  • 3
    @JaswinderWadali because they have activity therefore they are running. – PPD Jan 09 '15 at 08:57
  • 2
    ok thank you I understand the problem. I will have to create a activity. – helene Jan 09 '15 at 09:29
  • 1
    but what if you don't have an Activity and the app only consist of background services ? Is to impossible to receive the boot broadcast receiver ? – ralphgabb Aug 27 '19 at 09:31