0

I have an android service that runs when i open my app, now I want my android service to run at boot time. I tried the bellow code, but the service is not running automatically when i reboot my device. I cannot see it running as a service on my phone! Is there something wrong in my code?

I added these permissions to the manifest:

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

Here's my receiver in the manifest:

<receiver android:name="org.qtproject.example.MyBroadcastReceiver">
  <intent-filter>
  <action android:name="android.intent.action.RECEIVE_BOOT_COMPLETED"/>
  <action android:name="android.intent.action.RECEIVE_HEADSET_PLUG"/>
  </intent-filter>
 </receiver>

And here's MyBroadcastReceiver.java:

import android.os.Bundle;
import org.qtproject.qt5.android.bindings.QtActivity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;

public class MyBroadcastReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        Intent startServiceIntent = new Intent(context, org.qtproject.example.MyCustomAppService.class);
        context.startService(startServiceIntent);

    }
}
Mena
  • 3,019
  • 1
  • 25
  • 54
  • And what is your problem?? – jpo38 May 31 '17 at 19:23
  • @jpo38 My problem is that the service doesn't start automatically when i reboot my android device. I can not see it running in my phone services. Is there something wrong in my code? – Mena Jun 01 '17 at 07:45
  • Dunno. But now your post asks a question, hopefully someone will answer it. – jpo38 Jun 01 '17 at 08:06

2 Answers2

1

replace <action android:name="android.intent.action.RECEIVE_BOOT_COMPLETED"/> with <action android:name="android.intent.action.BOOT_COMPLETED"/> in the manifest

kajay
  • 27
  • 6
0

ok so I had 2 problems in my code:

1)Thanks to @kajay, i had to change my action line as he described, to be: <action android:name="android.intent.action.BOOT_COMPLETED"/> in the manifest.

2) I was missing defining the package in the MyBroadcastReceiver.java. So, the class couldn't find the startServiceIntent. Of course qt doesn't give any errors or warnings with many java problems. So, in my case i had to add this to the MyBroadcastReceiver.java :

package org.qtproject.example;

I had to do both of the above steps to fix my problem! P.S Sometimes the service takes around 45 secs or more to start after booting!

Mena
  • 3,019
  • 1
  • 25
  • 54