views:

837

answers:

3

I've been trying to start a service when a device boots up on android, but I cannot get it to work.

I've looked a number of links online but none of the code is working. Am I forgetting something? This is my code.

Manifest

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

<receiver   android:name=".StartServiceAtBootReceiver"
  android:enabled="true" 
  android:exported="false"
  android:label="StartServiceAtBootReceiver">
  <intent-filter>
    <action android:name="android.intent.action._BOOT_COMPLETED"/>
   </intent-filter>
</receiver>
<service android:enabled="true" android:name="com.test.RunService"/>

Receiver OnReceive

public void onReceive(Context context, Intent intent)
{
  if("android.intent.action.BOOT_COMPLETED".equals(intent.getAction()))
  {
     Intent serviceLauncher = new Intent(context, RunService.class);
     context.startService(serviceLauncher);
     Log.v("TEST", "Service loaded at start");
  }
}

Thanks,

A: 

Hi,

I have an additional <category>-tag, don't know if that makes any difference.

<receiver android:name="BootIntentReceiver">  
        <intent-filter>  
            <action android:name="android.intent.action.BOOT_COMPLETED" />  
            <category android:name="android.intent.category.HOME" />  
        </intent-filter>  
</receiver>

Have you tried ommiting the if-clause "android.intent.action.BOOT_COMPLETED".equals(intent.getAction(), as the receiver probably only receives that intent anyway?

Nick
tried this and it didn't work btw i forgot to mention i also have the<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
Alex
+1  A: 

I think your manifest needs to add:

<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
RickNotFred
i forgot to mention i have that
Alex
Move it to receiver tag - look at example i've posted below.
Alex Volovoy
+1  A: 
<receiver
            android:name="BootCompleteReceiver"
            android:permission="android.permission.RECEIVE_BOOT_COMPLETED">
            <intent-filter>
                <action
                    android:name="android.intent.action.BOOT_COMPLETED"></action>
            </intent-filter>
        </receiver>

i'd also change if to intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED) shouldn't make a difference, but i'd avoid using just a string

Alex Volovoy
tried this and it's still not working
Alex
What does it say in the logs ?
Alex Volovoy
Also - you're trying it on device right ?
Alex Volovoy