Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Foreground service doesn't start at boot time

I am trying to start a foreground service at boot time, but it never starts, however If i try to start a normal background service. It starts perfectly fine.

Can you please let me what is wrong with my code ?

My code is: Manifest file:

<application
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name" >
    <receiver android:name="com.test.andsrvfg.AndSrvFgService">
        <intent-filter>
            <action android:name="android.intent.action.BOOT_COMPLETED" />
        </intent-filter>
    </receiver>
    <service android:name="AndSrvFgService">
        <intent-filter>
            <action android:name="com.test.andsrvfg.AndSrvFgService"></action>
        </intent-filter>
    </service>
</application>

BroadcastReceiver to handle ACTION_BOOT_COMPLETED:

public class AndSrvFgStarter extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) 
    {
        if (intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED)) {
            Intent i = new Intent();
            i.setAction("com.test.andsrvfg.AndSrvFgService");
            context.startService(i);
        }
    }
}

The actual service is like:

public class AndSrvFgService extends Service {

    private boolean bForeground = false;
    public AndSrvFgService() {
    }

    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public void onCreate() {
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        if(!bForeground) {
            bForeground = true;
            Notification note = new Notification( 0, null, System.currentTimeMillis() );
            note.flags |= Notification.FLAG_NO_CLEAR;
            startForeground( 1242, note );      
                }


        return START_STICKY;
    }

    @Override
    public void onDestroy() {
        if(bForeground) {
            stopForeground(true);
        }
    }
like image 242
Androidme Avatar asked Dec 04 '25 17:12

Androidme


1 Answers

This code works for me:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.dfsdf"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="15" />
    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
            android:label="@string/title_activity_main" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <activity android:name="com.google.ads.AdActivity"
              android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"/>

        <service android:name=".MyService"/>

        <receiver android:enabled="true" 
            android:name=".BootUpReceiver"
            android:permission="android.permission.RECEIVE_BOOT_COMPLETED">
            <intent-filter >
                <action android:name="android.intent.action.BOOT_COMPLETED"/>
                <category android:name="android.intent.category.DEFAULT"/>
            </intent-filter>
        </receiver>

    </application>

</manifest>
  • Put this in the receiver:

    android:permission="android.permission.RECEIVE_BOOT_COMPLETED"

  • Set enable to true:

    android:enabled="true"

like image 180
moskis Avatar answered Dec 06 '25 06:12

moskis



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!