androidqtqmlqtembeddedqtandroidextras

Qt android Can't listen to os intents ex.RECEIVE_BOOT_COMPLETED


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);

    }
}

Solution

  • 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!