简体   繁体   English

Android-从活动外部调用函数(通过AlarmManager和Notification)

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

Ok, so I have a main activity called 'Main.java'. 好的,所以我有一个名为“ Main.java”的主要活动。 This main activity starts an AlarmManager which fires an intent leading to 'AlarmReceiver.java'. 此主要活动将启动AlarmManager,后者触发一个意图导致“ AlarmReceiver.java”的意图。 This 'AlarmReceiver.java' then creates a notification which has two buttons on it. 然后,此“ AlarmReceiver.java”将创建一个带有两个按钮的通知。 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'. 其中一个按钮是删除按钮,因此当用户单击该按钮时,会触发另一个意图,从而将其引导到“ 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. 在DelPair.java中,我修改了数据库中的表,但随后需要Main.java的UI来反映此更改。 I have created two functions in Main.java called updateArrayFromDB() and updateUIFromArray() to do this for me: 我已经在Main.java中创建了两个函数updateArrayFromDB()和updateUIFromArray()来为我做这件事:

  • updateArrayFromDB() will sync an ArrayList created in Main.java to a certain table in the DB. updateArrayFromDB()会将Main.java中创建的ArrayList同步到数据库中的某个表。
  • updateUIFromArray() will change the UI of Main.java to represent the ArrayList that has just been changed. updateUIFromArray()将更改Main.java的UI,以表示刚刚更改的ArrayList。

The problem is that I cannot call these two functions from DelPair.java (they don't exist in that space). 问题是我无法从DelPair.java调用这两个函数(它们在该空间中不存在)。 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. 我在尝试找到答案时遇到了Serializables,但我还不知道它们是否适用于此,或者究竟如何在AlarmManager NotificationManager中实现它们。

How can I access these methods from DelPair.java? 如何从DelPair.java访问这些方法?

In Main.java: 在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: 在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: 然后在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 
            }
        });
    }
}

Why not use broadcasts ? 为什么不使用广播? in onHandleIntent just send a broadcast 在onHandleIntent中只发送广播

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 在您的活动onResume中-注册新的IncomingReceiver,onPause取消注册

    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();
    }

由于需要基于数据库的更改来更新UI,因此可以在活动的onResume()方法中调用updateArrayFromDB()updateUIFromArray() ,以便每次用户进入活动时UI都会更新。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM