5

I am using the service for sending notifications every 30 sec. When my application is in background mode it is working fine. Every notification receives properly. But when I kill/close my application the service stops and I am not receiving any notification.

Anybody have an idea on how to run the service when application is closed or killed?

here is my manifest file`
``

<uses-sdk
    android:minSdkVersion="11"
    android:targetSdkVersion="21" />

<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
<uses-permission android:name="android.permission.GET_TASKS" />
<uses-permission android:name="android.permission.KILL_BACKGROUND_PROCESSES" />

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

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

    <service
        android:name=".MainService"
        android:enabled="true"
        android:exported="false"
         >
    </service>
    <service android:name=".NewService" >
    </service>

    <activity
        android:name=".MainActivity2"
        android:label="@string/title_activity_main_activity2" >
    </activity>
</application>

`

dev_mg99
  • 563
  • 6
  • 17

1 Answers1

2

If you want to continue your service even after killing application from Recent app's list, set stopWithTask as false in manifest:

<service
    android:enabled="true"
    android:name=".MyService"
    android:exported="false"
    android:stopWithTask="false" />

And your Service should be sticky.

@Override
public final int onStartCommand(Intent intent, int flags, int startId) {
    Log.i(TAG, "onStarCommand(): Received id " + startId + ": " + intent);
    return START_STICKY; // run until explicitly stopped.
}

Refer this question for all details.

And if you want to perform some additional action when application gets removed from recent list, you can override method onTaskRemoved

Hope this helps.

Community
  • 1
  • 1
MysticMagicϡ
  • 28,305
  • 16
  • 71
  • 119
  • its working fine but when i click on the recent application button my application directly open i do not want to open my application like this – dev_mg99 Feb 12 '15 at 10:22
  • You click on recent app or remove it? What do you want to do if user opens app from recent app list? Put your code in onResume of that activity – MysticMagicϡ Feb 12 '15 at 10:23
  • now my recent button just open my application not show the other application list – dev_mg99 Feb 12 '15 at 10:26
  • What you are saying is not very clear. Please explain properly. – MysticMagicϡ Feb 12 '15 at 10:27
  • when i click on recent apps button(right side of home button) then instead of the recent apps list, my app is getting open up. otherwise the functionality is working fine with your given code. is it clear now? – dev_mg99 Feb 12 '15 at 10:34
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/70783/discussion-between-user3459071-and-flameprincess). – dev_mg99 Feb 12 '15 at 11:40
  • have you find the solution – M.Yogeshwaran Oct 20 '16 at 12:08