-2

Suppose i have an interface class:

public interface interfaceClass{
    void message();
}

The interface is implemented by two Activities A and B:

public class A extends AppCompatActivity implements interfaceClass{

      @Override
      public void message() {
           // message: this is class A implementing interface
      }
}

public class B extends AppCompatActivity implements interfaceClass{

      @Override
      public void message() {
           // message: this is class B implementing interface
      }
}

Now I have service where i want to call the method of the interface class implemented by both activities A and B by creating the object of the interface class but i don`t know how to initialize the interface class object in order to call the method implementation from class A or B

public class serviceClass extends Service {

      interfaceClass object = (interfaceClass)context;
      object.message();

}

Any suggestions how to get the context of the activity A or B to initialize the object???

Waqas Ahmed
  • 153
  • 11
  • `new A()` if you need `A` instance, `new B()` otherwise. No need to cast them to `interfaceClass` as both `A` and `B` are subtypes of this type. – Roman Puchkovskiy Dec 12 '17 at 18:56
  • I don`t want to initialize it by explicitly calling new A() or new B(), any way we can initialize it using the context of the activity? – Waqas Ahmed Dec 12 '17 at 18:58
  • Well, the question was 'how to initialize the interface class object'. When you get an instance of `A` or `B` in whatever way you choose, it is automatically an instance of `interfaceClass`. If your question is 'how to obtain an instance of A or B in my `serviceClass`', you probably need to ask that :) – Roman Puchkovskiy Dec 12 '17 at 19:01
  • yes i have updated the question, check it now. how to get the context of the activity A or B? – Waqas Ahmed Dec 12 '17 at 19:04
  • @RomanPuchkovskiy it's never correct to use `new` in order to create an instance of an activity – Daniel Nugent Dec 12 '17 at 19:05
  • Yep, I've overlooked that that was an activity, sorry for that. – Roman Puchkovskiy Dec 12 '17 at 19:06

2 Answers2

1

I believe you can solve this by using a Bound Service: https://developer.android.com/guide/components/bound-services.html

Then you could set up your activity hierarchy like this:

public abstract class ServiceActivity extends AppCompatActivity implements MyInterface {

    protected MyService service;

    ...

    @Override
    public void onServiceConnected(ComponentName name, IBinder binder) {
        MyBinder myBinder = (MyBinder) binder;
        service = myBinder.getService();
        ...
    }
}

And

public class A extends ServiceActivity {...}

public class B extends ServiceActivity {...}

Now, in your service, you might have a method like this:

public void useInterface(MyInterface i) {
    i.doSomething();
}

Which you could call from your activity like this:

service.useInterface(this);
Ben P.
  • 52,661
  • 6
  • 95
  • 123
0

For your question: No way to get activity object in service.

In service, you can't obtain the Context of an activity. You can only get Context of service and ApplicationContext.

If you want to communicate with your activity from service you can use BroadcastReceiver.

More info about Broadcast: https://developer.android.com/guide/components/broadcasts.html

Bhuvanesh BS
  • 13,474
  • 12
  • 40
  • 66