1

I'm trying to have an intentservice working like I did with a location service saving within a file perfectly.

The service pops up a notification when a file occurs in a directory. this works nicely inside an activity but doesn't when I close.

Here is my code :

public class NotifyService extends IntentService {



public NotifyService() {
    super("NotifyService");
}

@Override
protected void onHandleIntent(Intent intent) {
          File alerte = new File(Environment.getExternalStorageDirectory() +
            File.separator +"SmartCollecte"+File.separator+ "ALERTE"+File.separator+ "IN"+File.separator+"alerte");
    if(alerte.exists()){
        createNotification();
    }
    else{}

}




@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
public void createNotification() {

    Intent intent = new Intent(this, NotificationReceiverActivity.class);
    PendingIntent pIntent = PendingIntent.getActivity(this, (int) System.currentTimeMillis(), intent, 0);


    Notification noti = new Notification.Builder(this)
            .setContentTitle("nouvelle collecte " + "test@gmail.com")
            .setContentText("une nouvelle collecte est disponible").setSmallIcon(R.drawable.notification)
            .setContentIntent(pIntent)
            .setSmallIcon(R.drawable.notification)
            .build();
    NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

    noti.flags |= Notification.FLAG_AUTO_CANCEL;

    notificationManager.notify(0, noti);

}

}

I can't just find what I'm missing... Thanks for your help

filoman
  • 17
  • 6

1 Answers1

0

This solution works only for preor 26 (Android O) SDK:

You have to launch your intent service using WakefulBroadcastReceiver Using this way you can manage your service using alarmmanager, you prevent from being killed by system (wake lock) and remove Activity dependecies.

Futhermore, I'd suggest to use this same in order to implement you idea.

https://developer.android.com/reference/android/support/v4/content/WakefulBroadcastReceiver.html

Vyacheslav
  • 26,359
  • 19
  • 112
  • 194
  • this seems to be the right way to do what I want! Thanks Vyacheslav I found a good example to use the alarmmanager : https://www.mindstick.com/Articles/1627/alarmmanager-with-broadcastreceiver-in-android – filoman Aug 31 '17 at 09:40