3

Basically I've got a boot receiver set up like so:

<receiver
        android:name="com.xxx.xxx.XXX.BootUpReciever"
        android:enabled="true"
        android:exported="true"
        android:permission="android.permission.RECEIVE_BOOT_COMPLETED" >
        <intent-filter android:priority="999" >
            <action android:name="android.intent.action.BOOT_COMPLETED" />
            <action android:name="android.intent.action.QUICKBOOT_POWERON" />

            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </receiver> code here

Now this seems to work fine most of the time. It will boot fire on boot fine and continue doing what it needs to do. However, if I combine this with another application I develop, the receiver never gets called despite still being registered.

I always make sure to run the application first so it is registered, so it's not that. If I uninstall the other application, it works. They do have a shared user, but I don't think that has much to do with it as I use that across a number of applications which work fine in combination. It's just this one particular application it does not get along with.

EDIT:

I do have <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />, otherwise it would not work at all.

And the boot receiver itself:

public class BootUpReciever extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent args) {
    incBootCounter(context);
    Intent i = new Intent(context, HomeScreenActivity.class);  
    i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    context.startActivity(i);
}

private void incBootCounter(Context ctx) {
    SharedPreferences s = PreferenceManager.getDefaultSharedPreferences(ctx);
    if (s != null) {
        Editor edit = s.edit();
        if (edit != null) {
            edit.putInt("how_many_times_have_we_booted", s.getInt("how_many_times_have_we_booted", 0) + 1);
            edit.commit();
        }
    }
}

}

Just to point out, this isn't an application that goes on the Google Play store or anything. It's a very specific application that is only deployed to devices we own as a company.

EDIT 2:

I've found some help in the logs. With the other application installed I get this message:

W/BroadcastQueue(  986): Permission Denial: receiving Intent { act=android.intent.action.BOOT_COMPLETED flg=0x8000010 (has extras) } to com.xxx.xxx.xxxxxxxxx/.BootUpReciever requires android.permission.RECEIVE_BOOT_COMPLETED due to sender null (uid 1000)

Without the other application I get this message:

I/ActivityManager(  980): Start proc com.xxx.xxx.xxxxxx for broadcast com.sps.smc.SMCKiosk/.BootUpReciever: pid=1656 uid=10109 gids={50109, 3003, 3002, 3001, 1028, 1015}
deadwards
  • 2,109
  • 1
  • 16
  • 27
  • Remove `android:enabled="true"` and `android:exported="true"`, you don't need these directives. Ensure you have `android.permission.RECEIVE_BOOT_COMPLETED` permission in your AndroidManifest.xml file. Also, post your codes for the **BootUpReciever** class, maybe there's something wrong with this class. – ChuongPham Mar 20 '14 at 16:01
  • @ChuongPham I added those in to try and fix the problem, without them it's the same issue. I do have the permission so it's not that, but I'll add the code for the receiver in now – deadwards Mar 20 '14 at 16:04
  • and the other application does nothing related to boot_receiver and boradcastreceiver? – njzk2 Mar 20 '14 at 16:04
  • @njzk2 It's the only boot_completed receiver, nothing else is set to receive them. – deadwards Mar 20 '14 at 16:05
  • Is the path to `com.xxx.xxx.XXX.BootUpReciever` correct? Note: Depending how you setup your packages in your Android project, you can also have a relative path like `.BootUpReciever`. – ChuongPham Mar 20 '14 at 16:06
  • I would have this in the `onReceive` method: `if (intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED)) { // your codes here }. You may also want to check with `if (context != null) {...}`, as the Context may return null. FYI: The `android.intent.action.BOOT_COMPLETED` is not affected by Google Play store. – ChuongPham Mar 20 '14 at 16:08
  • @ChuongPham Yes, the path is correct, otherwise it would never receive it regardless of what other applications are installed. It's only when another specific application is installed that it does not get received. I'll try some of your other suggestions and get back to you. – deadwards Mar 20 '14 at 16:11
  • OK, so I added some logging in and that isn't even getting called, so it just never even gets to the method. It's not that it's crashing during the method. Not seeing anything in the logs to suggest that either. – deadwards Mar 20 '14 at 16:15
  • Remove `android.permission.RECEIVE_BOOT_COMPLETED` from your receiver declaration and add it as `` – ChuongPham Mar 20 '14 at 16:21
  • Have done the changes, still not receiving with other application installed but works without the other application. – deadwards Mar 20 '14 at 16:35

2 Answers2

0

Try something like:

public void onReceive(Context context, Intent intent) {
   if (intent.getAction().equalsIgnoreCase(Intent.ACTION_BOOT_COMPLETED)) {
      if (context != null) {
        // your codes here
      }
   }
}

The first check ensures that only action equals Intent.ACTION_BOOT_COMPLETED will be processed. The second check ensures that only a valid context is used to execute the codes.

Also, the AndroidManifest.xml should have the following declarations:

<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
<receiver
   android:name="com.xxx.xxx.XXX.BootUpReciever">
   <intent-filter android:priority="999" >
      <action android:name="android.intent.action.BOOT_COMPLETED" />
      <action android:name="android.intent.action.QUICKBOOT_POWERON" />    
      <category android:name="android.intent.category.DEFAULT" />
   </intent-filter>
</receiver>
ChuongPham
  • 4,761
  • 8
  • 43
  • 53
  • This answer plainly makes no sense. The problem is that Android wouldn't even pass the intent to the application because of some permission violation, the contents of the receiver method are irrelevant, and the OP already provided proper permission and intent filters. – Pawel Veselov Jan 20 '23 at 16:01
0

Managed to solve it.

My problem was that because it was a shared user, I had to put the permission in the other manifest (or at least I think that's what the issue was).

For some reason it was revoking it because it was not in both manifests. Why it worked with the same shared user in other applications without the same permissions mirroring across is unknown to me at this point. Might be the order in which it loads the packages at a guess.

deadwards
  • 2,109
  • 1
  • 16
  • 27
  • 1
    For each Android app, you'll need both the `android.permission.RECEIVE_BOOT_COMPLETED` and the receiver declaration `android.intent.action.BOOT_COMPLETED` for it to work properly. Without the proper permission and receiver declaration, Android will not allow an app to be executed. That's why you can't get both of your apps to work properly. – ChuongPham Mar 20 '14 at 17:27