-3

I want to create service which will check new notifications every some time, even if main app is closed.

Add to manifest

    <service
        android:name=".service.NotificationService"
        android:enabled="true"
        android:exported="true"
        android:stopWithTask="false"
        android:process=":notification"/>

    <!-- Declaring broadcast receiver for BOOT_COMPLETED event. -->
    <receiver android:name=".BootReceiver"
        android:enabled="true"
        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>

Second is boot receiver, it also will start service after reboot.

NotificationService.class

import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.os.IBinder;
import android.util.Log;

import com.tagwishes.fc.Notification;
import com.tagwishes.fc.app.AppConfig;
import com.tagwishes.fc.helper.SessionManager;

import java.util.Timer;
import java.util.TimerTask;

public class NotificationService extends Service {


    static int WIFI_NETWORK_ID = 1;
    static int MOBILE_NETWORK_ID = 2;
    static int NO_NETWORK_ID = 3;
    private SessionManager sm;


    final String LOG_TAG = "fc";

    public void onCreate() {
        super.onCreate();
        Log.d(LOG_TAG, "MyService onCreate");
    }

    public void onDestroy() {
        super.onDestroy();
        Log.d(LOG_TAG, "MyService onDestroy");
    }

    public int onStartCommand(Intent intent, int flags, int startId) {
        Log.d(LOG_TAG, "MyService onStartCommand");


        readFlags(flags);
        MyRun mr = new MyRun(startId);
        new Thread(mr).start();


        Log.d("fc", "START_STICKY");

        return START_STICKY;
    }

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

    int chkStatus() {
        final ConnectivityManager connMgr = (ConnectivityManager)
                this.getSystemService(Context.CONNECTIVITY_SERVICE);
        final android.net.NetworkInfo wifi = connMgr.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
        final android.net.NetworkInfo mobile = connMgr.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
        if (wifi.isConnectedOrConnecting()) {
            return WIFI_NETWORK_ID;
        } else if (mobile.isConnectedOrConnecting()) {
            return MOBILE_NETWORK_ID;
        } else {
            return NO_NETWORK_ID;
        }
    }

    void readFlags(int flags) {
        if ((flags&START_FLAG_REDELIVERY) == START_FLAG_REDELIVERY)
            Log.d(LOG_TAG, "START_FLAG_REDELIVERY");
        if ((flags&START_FLAG_RETRY) == START_FLAG_RETRY)
            Log.d(LOG_TAG, "START_FLAG_RETRY");
    }

    class MyRun implements Runnable {

        int startId;

        public MyRun(int startId) {
            this.startId = startId;
            Log.d(LOG_TAG, "MyRun#" + startId + " create");
        }

        public void run() {
            Log.d(LOG_TAG, "MyRun#" + startId + " start");

            final int networkID = chkStatus();

            Log.d("fc", "networkID " + networkID);

            if (sm == null) {
                sm = new SessionManager(getApplication());
            }

            int reCheckTime = AppConfig.NOTIFICATION_RECHECK_DEFAULT;

            if (networkID == WIFI_NETWORK_ID) {
                reCheckTime = AppConfig.NOTIFICATION_RECHECK_WIFI;
            } else if (networkID == MOBILE_NETWORK_ID) {
                reCheckTime = AppConfig.NOTIFICATION_RECHECK_MOBILE;
            }


            final Notification nt = new Notification(getApplicationContext());//Its my own class


            Log.d("fc", "Timer check noti started " + reCheckTime);
            new Timer().scheduleAtFixedRate(new TimerTask(){
                @Override
                public void run(){

                    if (networkID != NO_NETWORK_ID || !sm.isLoggedIn()) {//IF HAVE NETWORK CONNECTION
                        Log.d("fc", "checkNotifications ");

                        nt.checkNotifications();
                    }

                }
            }, 0, reCheckTime*1000);


        }

    }

}

Boot Receiver

public class BootReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {

    Log.d("fc", "BootReceiver BroadcastReceiver");

    Intent myIntent = new Intent(context, NotificationService.class);
    context.startService(myIntent);
}

}

And this how i call service on MainActivity

        Intent bindIntent = new Intent(getBaseContext(), NotificationService.class);
        startService(bindIntent);

When i close app from Apps History, it close also my :notification process, and service not started again. I use START_STICKY for this.

Hitesh Sahu
  • 41,955
  • 17
  • 205
  • 154
  • if you are developing on API23 and above, you have to handle doze mode. This could be the reason why your service is stopped... – Opiatefuchs Jun 16 '16 at 12:40
  • it´s important which target you set and which version is installed on your device....not the min api. Especially which version is installed. An app with lower API is not excluded from doze mode. It just has no possibillity to stay alive because of not available methods/android packages...... – Opiatefuchs Jun 16 '16 at 13:08
  • https://developer.android.com/training/monitoring-device-state/doze-standby.html – Opiatefuchs Jun 16 '16 at 14:05
  • my phone android version 4.4.2 . Why START_STICKY dont restart service after killing? – Fəqan Çələbizadə Jun 16 '16 at 15:00
  • ah, now we´re coming near ;). It seems to be a bug on KitKat: http://stackoverflow.com/questions/20636330/start-sticky-does-not-work-on-android-kitkat-edit-and-jelly-bean – Opiatefuchs Jun 16 '16 at 18:32
  • Yeah) I found this already and solve problem) Thanks very much – Fəqan Çələbizadə Jun 17 '16 at 08:17

2 Answers2

0

start your service inside onDestroy function

@Override
protected void onDestroy() {
super.onDestroy();
//startService()
}
Musakkhir Sayyed
  • 7,012
  • 13
  • 42
  • 65
Maninder Taggar
  • 306
  • 3
  • 8
  • Not working. onDestroy called when back button pressed. When i close app from App History it closed with notification service, and without calling onDestroy. – Fəqan Çələbizadə Jun 16 '16 at 12:44
0

For nadroid O+ you have to call startForground service. so this would be your receiver:

 public void onReceive(Context context, Intent intent) {

   
    Intent service = new Intent(context, NotificationService.class);
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        context.startForegroundService(service);
    } else {
        context.startService(service);
    }

and of course declare the foreground service in your manifest.

Keivan.k
  • 548
  • 6
  • 21