Beyarkay
Beyarkay

Reputation: 35

Android - Call a function from outside an activity (through AlarmManager and Notification)

Ok, so I have a main activity called 'Main.java'. This main activity starts an AlarmManager which fires an intent leading to 'AlarmReceiver.java'. This 'AlarmReceiver.java' then creates a notification which has two buttons on it. One of the buttons is a deletion button, and so when the user clicks on that button, another intent is fired, leading it to 'DelPair.java'.

In DelPair.java, I modify a table in a Database, but then I need the UI of Main.java to reflect this change. I have created two functions in Main.java called updateArrayFromDB() and updateUIFromArray() to do this for me:

The problem is that I cannot call these two functions from DelPair.java (they don't exist in that space). I have come across Serializables in trying to find an answer but I don't know enough to know if they apply here or exactly how to implement them across the AlarmManager and the NotificationManager.

How can I access these methods from DelPair.java?

In Main.java:

public void updateArrayFromDB(){
    //... The code for this is long and irrelevant
}
public void updateUIFromArray(){
    //... The code for this is long and irrelevant
}

private void SendNotification() {
    Intent intent = new Intent(this, AlarmReceiver.class);
//... 
    PendingIntent sender = PendingIntent.getBroadcast(this, 2 , intent, PendingIntent.FLAG_UPDATE_CURRENT);
    AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
    am.setRepeating(AlarmManager.RTC_WAKEUP, 5000, notif_freq, sender);
}

In AlarmReceiver.java:

Intent delPairI = new Intent(context, DelPair.class);
PendingIntent delPairPI = PendingIntent.getService(context, 0, delPairI, PendingIntent.FLAG_UPDATE_CURRENT);

Notification noti;
noti = new Notification.Builder(context)
        //...
        .addAction(R.drawable.ic_delete_icon, "Delete the thing", delPairPI)
        .build();

NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0, noti);

and then in DelPair.java:

public class DelPair extends IntentService {
//...
    @Override
    protected void onHandleIntent(final Intent intent) {
//...
        Intent it = new Intent(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
        getApplicationContext().sendBroadcast(it);

        handler.post(new Runnable() {
            @Override
            public void run() {
    //... here is where I update the database, which works perfectly
                //now need to update the UI and array in Main.java
                updateArrayFromDB();    //these lines
                updateUIFromArray();    //obviously don't work 
            }
        });
    }
}

Upvotes: 0

Views: 423

Answers (2)

Shaishav
Shaishav

Reputation: 5312

Since you need to have an updated UI based on database changes, you can call updateArrayFromDB() and updateUIFromArray() in the onResume() method of your activity so the UI gets updated each time the user enters the activity.

Upvotes: 0

dumb_terminal
dumb_terminal

Reputation: 1825

Why not use broadcasts ? in onHandleIntent just send a broadcast

Intent i = new Intent();
i.setAction(CUSTOM_INTENT);
//put relevant data in intent
getApplicationContext().sendBroadcast(i);

The broadcast receiver:

public class IncomingReceiver extends BroadcastReceiver {
    private MainActivity act;
    public IncomingReceiver(MainActivity main){
        this.act = act;
    }
    @Override
    public void onReceive(Context context, Intent intent) {
        if (intent.getAction().equals(CUSTOM_INTENT)) {
            System.out.println("GOT THE INTENT");
            // call the method on act
        }
    }
}

In your activity onResume - register new IncomingReceiver, onPause unregister

    private IncomingReceiver receiver;
    public void onCreate(Bundle bOs){
        //other codes
        receiver = new IncomingReceiver(this);
    }
    @Override
    protected void onResume() {
        IntentFilter filter = new IntentFilter();
        filter.addAction(CUSTOM_INTENT);

        registerReceiver(receiver, filter);
        super.onResume();
    }

    @Override
    protected void onPause() {
        unregisterReceiver(receiver);
        super.onPause();
    }

Upvotes: 1

Related Questions