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

时间:2016-08-01 11:46:05

标签: java android android-intent

好的,所以我有一个名为' Main.java'的主要活动。这个主要活动启动一个AlarmManager,它触发一个导致“AlarmReceiver.java”的意图。 这个' AlarmReceiver.java'然后创建一个通知,上面有两个按钮。其中一个按钮是删除按钮,因此当用户点击该按钮时,会触发另一个意图,将其引导至DelPair.java'。

在DelPair.java中,我修改了数据库中的表,但后来我需要Main.java的UI来反映这一变化。我在Main.java中创建了两个函数,名为updateArrayFromDB()和updateUIFromArray(),为我这样做:

  • updateArrayFromDB()会将Main.java中创建的ArrayList同步到 DB中的某个表。
  • updateUIFromArray()将更改UI Main.java表示刚刚更改的ArrayList。

问题是我无法从DelPair.java中调用这两个函数(它们不存在于该空间中)。我遇到了Serializables试图找到答案,但我不知道他们是否在这里应用或者确切地知道如何在AlarmManager NotificationManager中实现它们。

如何从DelPair.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);
}

在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);

然后在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 
            }
        });
    }
}

2 个答案:

答案 0 :(得分:1)

为什么不使用广播?在onHandleIntent中只发送一个广播

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

广播接收器:

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
        }
    }
}

在您的活动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();
    }

答案 1 :(得分:0)

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