好的,所以我有一个名为"main.java"的主活动。这个主活动启动一个AlarmManager,它触发一个指向"AlarmReceiver.java"的意图。然后,这个"AlarmReceiver.java"创建了一个有两个按钮的通知。其中一个按钮是删除按钮,因此当用户单击该按钮时,会激发另一个意图,导致它进入"DelPair.java"。
在DelPair.java中,我修改了数据库中的一个表,但我需要Main.java的UI来反映这一变化。我在Main.java中创建了两个函数,分别名为updateArrayFromDB()和updateUIFromArray()
- updateArrayFromDB()将在Main.java中创建的ArrayList同步到数据库中的某个表
- updateUIFromArray()将更改的UIMain.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
}
});
}
}
为什么不使用广播?在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
}
}
}
在"恢复-注册新的IncomingReceiver"活动中,在"暂停"活动中注销
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。