Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Start Service at boot without activity

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.

like image 648
helene Avatar asked Jan 09 '15 08:01

helene


People also ask

How do I start a service without an activity?

In your Activity's onCreate() , start your Service with startService() . Exit the Activity with finish() once you've started the Service.

Can we create application without activity?

You don't have to have the main activity in your application, or any activities at all. Check the manifest and remove the launcher-specific intent filter from your main activity if you don't want it to show up in the application list.


1 Answers

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.

like image 192
PPD Avatar answered Sep 18 '22 10:09

PPD