Search code examples
androidbroadcastreceiverandroid-serviceandroid-broadcastbootcompleted

Android boot start app on boot from BroadcastReceiver with multiple services


I need to start 2 services on bootcompleted. The first service starts correctly, but second service seems is not starting. I don't know if I have to create two BroadcastReceiver or it's enough with one. Here is my code. I've put the two services in one BroadcastReceiver. Please, can you tell me what I'm doing wrong?

Thank you in advance

AndroidManifest.xml:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.pruebas.appservicelocator"
    android:versionCode="1"
    android:versionName="1.0"
    android:installLocation="internalOnly"  >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="16" />

    <!-- Startup service -->
    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
    <!-- GPS -->
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <!-- UUID -->
    <uses-permission android:name="android.permission.READ_PHONE_STATE"/>
    <!-- Acceso a web service -->
    <uses-permission android:name="android.permission.INTERNET"></uses-permission>

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.pruebas.appservicelocator.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <service android:name=".Servicio">
            <intent-filter>
                <action android:name="com.pruebas.appservicelocator.Servicio"/>
            </intent-filter>
        </service> 

        <service android:name=".ServicioBD">
            <intent-filter>
                <action android:name="com.pruebas.appservicelocator.ServicioBD"/>
            </intent-filter>
        </service> 

        <receiver android:name=".Recibidor" android:enabled="true" android:exported="true">
            <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED"/>
                <category android:name="android.intent.category.DEFAULT"/>
            </intent-filter>
        </receiver>
    </application>

</manifest>

Recibidor.java:

package com.pruebas.appservicelocator;

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

public class Recibidor extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        android.os.Debug.waitForDebugger(); 
        Toast.makeText(context, "Iniciando Recibidor", Toast.LENGTH_LONG).show();
        final String TAG = "Recibidor";
        Log.i(TAG, "Iniciando Recibidor");

        if (intent.getAction().equalsIgnoreCase("android.intent.action.BOOT_COMPLETED")) {
            Toast.makeText(context, "Iniciando Intent", Toast.LENGTH_LONG).show();
            Log.i(TAG, "Iniciando Intent");

            Intent servicio = new Intent();
            servicio.setAction("com.pruebas.appservicelocator.Servicio");
            context.startService(servicio);

            Intent servicioBD = new Intent();
            servicio.setAction("com.pruebas.appservicelocator.ServicioBD");
            context.startService(servicioBD);

            Log.i(TAG, "Iniciando Servicios");
            Toast.makeText(context, "Iniciando Servicio", Toast.LENGTH_LONG).show();
         } 
    }

}

"Servicio" service works fine, so I don't write the code. If you need, please, tell-me and I will write it.

ServicioBD.java:

package com.pruebas.appservicelocator;

import com.pruebas.utils.UsersLocationsDBHelper;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.widget.Toast;

public class ServicioBD extends Service{

    private UsersLocationsDBHelper locDBHelper = null;
    private static String TAG = "ServicioBD";

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        Toast.makeText(this, "SERVICIOBD ON CREATE", Toast.LENGTH_LONG).show();

    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        startForeground(0, null);

        Toast.makeText(this, "SERVICIOBD ON START COMMAND", Toast.LENGTH_LONG).show();


        return START_STICKY;
    }
}

Solution

  • I've found the error.

      Intent servicio = new Intent();
      servicio.setAction("com.pruebas.appservicelocator.Servicio");
      context.startService(servicio);
    
      Intent servicioBD = new Intent();
      servicio.setAction("com.pruebas.appservicelocator.ServicioBD");
      context.startService(servicioBD);
    

    Has to be

      Intent servicio = new Intent();
      servicio.setAction("com.pruebas.appservicelocator.Servicio");
      context.startService(servicio);
    
      Intent servicioBD = new Intent();
      servicioBD.setAction("com.pruebas.appservicelocator.ServicioBD");
      context.startService(servicioBD);