a_dzik
a_dzik

Reputation: 977

Android - get App from dependency

I have two projects, let's call them App and Dependency. Dependency is a some kind of GCM parser project that does ... things. It's hard to tell, because it's poorly written, company project that I am forced to use.

Anyways, I'm currently trying to change the part, where I show the notification. I wan't to filter that because sometimes I get notifications that were already showed. My plan is co compare ID's with GreenDAO database... which is implemented inside App. I have singleton class-manager for my database, that manages all database queries.

How should I get to that class from dependency project? I know, that I should use interfaces, one way or another, but I'm not sure how.

Upvotes: 0

Views: 63

Answers (1)

vinitius
vinitius

Reputation: 3274

I don't know how your classes are, but in general, you could do something like this:

In Dependency project:

Create a listener for receiving the list you want from the database, let's say onDBRequest:

public interface onDBRequest {
   public List<Integer> onIDRequest();
}

Then, in your class where you show he notifications you can do:

 public class ClassThatShowsNotifications {
     private OnDBRequest mDbListener;
     public onDBRequest getDblistener();
     public void setDblistener();
     ...

} When you need the list of IDs to compare , you can just call:

getDbListener().onIDrequest(); //will return the list of IDs you need to compare

In App project:

Make your DAO implement onDBRequest and implement onIDRequest to return the list you need to compare later:

public class YourDAO implements onDBRequest{
  ...

public List<Integer> onIDRequest(){
 List<Integer> IDs;
  //do your stuff to return the IDs
 return IDs;

} }

Now, the only thing you need to do is set the listener you want on the class that shows notifications. Example:

In the YourDAO class

ClassThatShowsNotification notifications = new ClassThatShowsNotifications();
notifications.setDBListener(this);

EDIT

If your class is a based service class, you can do pretty much the same. When you start the service you can can pass your singleton as the listener. You can work with bindService() or make a singleton from your service.

public class YourService extends Service{

  private static YourService sInstance;


  public static YourService getInstance(){
    return sInstance;
  }
  onCreate(){
    sInstance = this;

    }

}

Then, when you start the service

YourService.getInstance().setDBListener((onDBRequest)YourDAOSingleton.getInstance());

This would work fine since you have a local service, but you can also take a look at Broadcast Receivers

Upvotes: 1

Related Questions