eloweyn
eloweyn

Reputation: 125

Android boot service never starts

I'm new in Android programming. I have a receiver that starts a service at boot but it seems never started. Can you tell me what I'm doing wrong? I don't know how to debug it. Can you explain me how can I debug Android startup services too?

Here is my code. Thank you in advance

Recibidor.java:

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) {
      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.Servicio");
         context.startService(servicio);

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

   }
}

Servicio.java

package com.pruebas;

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

public class Servicio extends Service {
   private final String TAG = "Servicio";


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

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

   @Override
   public void onDestroy() {
      super.onDestroy();
      Log.i(TAG, "ON DESTROY");
      Toast.makeText(this, "ON DESTROY", Toast.LENGTH_LONG).show();
   }

   @Override
   public int onStartCommand(Intent intent, int flags, int startId) {
      startForeground(0, null);
      Log.i(TAG, "ON START COMMAND");
      Toast.makeText(this, "ON START COMMAND", Toast.LENGTH_LONG).show();
      return START_STICKY;
   }
}

AndroidManifest.xml

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

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

   <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
    <application>
        <service android:name=".Servicio">
            <intent-filter>
                <action android:name="com.pruebas.Servicio"/>
            </intent-filter>
        </service> 

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

    </application>

</manifest>

Upvotes: 5

Views: 1888

Answers (3)

Squonk
Squonk

Reputation: 48871

The code you've posted will never work on later versions of Android. To prevent malware, with later versions of Android, it is not possible to automatically register a BroadcastReceiver in the manifest UNTIL the user has manually started your app from the app launcher.

You will need to create an Activity with the MAIN/LAUNCHER <intent-filter> entries. Once the user has manually started the app once, the manifest registration for your BroadcastReceiver will occur and it will remain registered unless the user uses 'Force Stop` from the Manage Applications part of Settings.

Upvotes: 4

tundundun
tundundun

Reputation: 644

Try to move

<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" /> 

out of "receiver" tag.

http://developer.android.com/guide/topics/manifest/manifest-intro.html#perms

Upvotes: 0

Entreco
Entreco

Reputation: 12900

The application needs to be installed in internal storage to receive the BOOT_COMPLETED intent. See this question: My BroadcastReceiver is not receiving the BOOT_COMPLETED intent after my N1 boots. Help Please!

Upvotes: 1

Related Questions