-2

I have

public class Backgroundservice implements AlarmReceiverCallback {
private Context context;

public Backgroundservice(Context context) {
    this.context = context;
}

@Override
public void onAlarmReceived() {
   //interface trigger
}}

now i would like to extends Backgroundservice in my MainActivity class that extends AppCompatActivity .

so i can use my method inside the mainactivity once the interface in Backgroundservice get trigger

11
  • 1
    Why do you want to "extend" MainActivity?
    – Zun
    Oct 17, 2018 at 12:08
  • 2
    no i want to extend backgroundservice class Inside the mainactivity Oct 17, 2018 at 12:09
  • 3
    well i have method that runs in the mainactivity class, i want to run it once the onAlarmReceived implements in Backgroundservice get triggered Oct 17, 2018 at 12:10
  • 3
    okay so i have alarmservice that trigger lets say every 1hour, once i get call back from the alarmManager i call AlarmReceiverCallback trigger = new Backgroundservice(context); trigger.onAlarmReceived(); and then i send send API to get new Token. now the problem is i need to do my refresh token api in mainactivity so im trying to get trigger in mainactivity Oct 17, 2018 at 12:13
  • 2
    because i would like to re-use the same code in multi apps so i need it to be more dynamic, i could have just set my method inside the Backgroundservice class but im trying to find way to reuse the code Oct 17, 2018 at 12:16

2 Answers 2

1

You can use BroadCastReceiver

in you serveice class

public class Backgroundservice implements AlarmReceiverCallback {
    private Context context;

    public Backgroundservice(Context context) {
        this.context = context;
    }

    @Override
    public void onAlarmReceived() {

       // send broadcast from here
       Intent intent  = new Intent("alaram_received");
       context.sendBroadcast(intent);
    }

and in your MainActivity receive this broadcast.

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // Register Receiver
        IntentFilter intentFilter = new IntentFilter("alaram_received");
        registerReceiver(alarm_receiver,intentFilter); 
    }

    BroadcastReceiver alarm_receiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            // refresh Token
        }
    };
}

Happy Coding.

1
  • 2
    Thanks this works, and i will have to find way to receive data when app is closed too Oct 17, 2018 at 14:45
0

I manged to get it working by creating new interface

public interface ReceiverCallback {
public void onReceived();}

and implements in my mainactivity, and than creating new object of it inside

    @Override
public void onReceive(Context context, Intent intent)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.