Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android BOOT_COMPLETED event not starting service

I am writing a small android application which starts my service upon device boot. Application is working fine on all the mobiles with Android versions 4.4.2 (Mi Note 4G, have to enable permissions in their security app), 5.0 (Lenovo K3 Note), 5.1 (One Plus One), 6 (Nexus 5) except on one mobile running Android 5.0 (Lenovo A1000, Launcher is similar to MiUI). Application installs on interal storage only. Used/Launched the app once before the device reboot. Here are the excerpts from the manifest and broadcast receiver files

manifest.xml:

 <receiver android:name="com.company.Broadcast"
     android:permission="android.permission.RECEIVE_BOOT_COMPLETED">
       
        <intent-filter>
    
            <action android:name="android.intent.action.BOOT_COMPLETED" />
    
            <category android:name="android.intent.category.LAUNCHER" />
    
        </intent-filter>
    
    </receiver>
    
    <service android:enabled="true" android:exported="false" android:name="com.company.service" />

broadcast receiver:

    public class Broadcast extends BroadcastReceiver {
    
        private static final String TAG = “BroadcastReceiver";
    
        @Override
    
        public void onReceive(Context context, Intent intent) {
    
            Log.d(TAG, "onReceive");
    
            Intent service = new Intent(context, Service.class);
    
            service.setFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES);
    
            context.startService(service);
        
    }
    
    
    }

Some the pre-installed apps start fine on boot and some are skipped. My app happened to be on the skipped apps list.

Here are some lines from logcat when the device boots,

01-18 17:02:58.933 504-560/? I/PackageManager: Package com.android.deskclock checking android.permission.RECEIVE_BOOT_COMPLETED: BasePermission{183dc63c android.permission.RECEIVE_BOOT_COMPLETED}
01-18 17:02:58.933 504-560/? I/PackageManager: Package com.android.deskclock granting android.permission.RECEIVE_BOOT_COMPLETED
01-18 17:02:58.933 504-560/? I/PackageManager: Package com.android.mms.service checking android.permission.RECEIVE_BOOT_COMPLETED: BasePermission{183dc63c android.permission.RECEIVE_BOOT_COMPLETED}
01-18 17:02:58.933 504-560/? I/PackageManager: Package com.android.mms.service granting android.permission.RECEIVE_BOOT_COMPLETED

Skipped package comments:

01-18 17:03:06.235 504-529/? D/ActivityManager: send broadcast: android.intent.action.BOOT_COMPLETED, skip package: com.google.android.gm
01-18 17:03:06.235 504-529/? D/ActivityManager: send broadcast: 

> android.intent.action.BOOT_COMPLETED, skip package: com.company

01-18 17:03:06.235 504-529/? D/ActivityManager: send broadcast: android.intent.action.BOOT_COMPLETED, skip package: com.frozendevs.cache.cleaner

I might have missed out some bit of code to make it work. Any Comments/suggestions would help a lot for me to proceed furhter.

like image 557
Naresh Avatar asked Oct 18 '22 17:10

Naresh


1 Answers

The problem is with the device. some devices only allow internal apps to receive this action.

you can add this to your intent filter as work around

<action android:name="android.intent.action.USER_PRESENT" />

This is triggered after the user unlocks the device.

like image 128
Krishna Chaitanya Kornepati Avatar answered Oct 21 '22 08:10

Krishna Chaitanya Kornepati