1

I programmed a boot up receiver to start a service, but the service is not starting. I don't even think the broad cast receiver is receiving any intents.

I set the permission

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

I declared the receiver in the manifest

<receiver android:name=".ServiceStarter">
    <intent-filter>
        <action android:name="android.intent.action.BOOT_COMPLETED"/>
    </intent-filter>
</receiver>

Here is my broad cast receiver class. I'm not logging any output in this class after boot up.

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

public class ServiceStarter extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        Intent i = new Intent("com.prac.test.MyPersistingService");
        i.setClass(context, MyPersistingService.class);
        context.startService(i);

        Log.v("TAG","Broadcast received"); //Doesn't print anything
    }
}

Here is my service class. None of the toasts are displayed after boot up.

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

public class MyPersistingService extends Service {

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

    @Override
    public void onCreate() {
        Toast.makeText(this, "Service created!", Toast.LENGTH_LONG).show();         //Doesn't show up
    }

    @Override
    public void onDestroy() {
        Toast.makeText(this, "Service stopped", Toast.LENGTH_LONG).show();          //Doesn't show up
    }

    @Override
    public void onStart(Intent intent, int startid) {
        Toast.makeText(this, "Service started by user.", Toast.LENGTH_LONG).show(); //Doesn't show up
        Log.v("TAG", "Service started");
    }
}
the_prole
  • 8,275
  • 16
  • 78
  • 163

3 Answers3

0

Try this:

<receiver android:name=".ServiceStarter" android:enabled="true" >
    <intent-filter>
       <action android:name="android.intent.action.BOOT_COMPLETED" />
    </intent-filter>
</receiver>
John Slegers
  • 45,213
  • 22
  • 199
  • 169
joanafcoelho
  • 1
  • 1
  • 1
0

if still dosn't work please try this

 <receiver
    android:name=".ServiceStarter"
    android:enabled="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>

Good luck

Netero
  • 3,761
  • 2
  • 29
  • 29
  • use the adb command - am broadcast -a android.intent.action.BOOT_COMPLETED instead of reboot phone each time – Netero Jan 11 '16 at 22:07
  • hi, i am curious about your question did the answer provided worked for you ? – Netero Jan 14 '16 at 07:49
0

It's very likely that your service is starting up, but is being killed off before it can do anything meaningful. Try acquiring a partial wake lock - Mark Murphy has written a handy set of classes just for that purpose.

Melllvar
  • 2,056
  • 4
  • 24
  • 47